Try
↑ ↓ move↵ openesc close

Interview prep / Locks, leases and fencing

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

A service computes lease expiry by adding 10 seconds to the current wall-clock time and compares against the clock on every check. An operator corrects the machine's time forward by two minutes. What breaks, and what do you change?

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

A strong answer

Every lease on that machine expires at once. The stored expiry times were computed from the old time, and after the step they all lie in the past. A lock service that did this would hand every lock to a new client while the old holders are running, so a large number of clients would think they hold the same locks.

A step backward is also harmful. Leases would last two minutes longer than intended, and a crashed holder would block everyone for that long.

The fix is to measure durations with a monotonic clock, one that never jumps and is not adjusted by time synchronisation, and compare elapsed time against the lease length. Wall-clock time is for timestamps people read, not for deadlines. The service should also be told about its own clock changes and treat a big step as a reason to distrust its leases, for example by refusing to grant new ones for a lease length.

A monotonic clock fixes the jump, but not rate differences between machines, so the holder still keeps a safety margin, and the resource still checks a token.

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