Try
↑ ↓ move↵ openesc close

Interview prep / Locks, leases and fencing

Senior · Locks, leases and fencing · System design interview question

Raft leaders and lock holders both rely on leases to act without a round trip. What assumption does that make about clocks, and what breaks if the assumption fails?

Sketch your own answer first, then compare it with the one below.

A strong answer

A leader lease says: I heard from a majority less than one election timeout ago, so nobody else can have been elected since, so I may act alone. That is a statement about elapsed time measured on different machines. It assumes that the leader's clock and the followers' clocks advance at nearly the same rate, within a bound, over the lease length.

If a follower's clock runs fast or the leader's runs slow, the follower can time out and elect a new leader while the old one still believes its lease is valid. Two leaders act at once for the overlap. For reads that means a stale answer, for lock grants it means two holders.

The mitigations are a margin that covers the maximum rate difference, leases much shorter than the election timeout, and refusing to serve from the lease when the margin is exceeded. The alternative that does not depend on clocks is a round trip to a majority per operation, which is slower but safe under any clock behaviour. Pick one deliberately and write down why.

Sources

Learn it by running it

The locks, leases and fencing lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms