Try
↑ ↓ move↵ openesc close

Interview prep / Locks, leases and fencing

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

A client checks its own clock before each write to make sure its lease is still valid. Why is that not sufficient, and how does clock drift make it worse?

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

A strong answer

The check happens before the write and the write happens after. Anything between the two, a pause or a slow network hop, can carry the write past the expiry even though the check passed. A check cannot be atomic with an action that happens somewhere else.

Even without a pause, the client's clock is not the lock service's clock. If the client's clock runs slow, it believes the lease has more time than it does. A client that starts its own lease timer when the grant arrives is also already late by the network delay. Both errors push the client to write after the service has moved on.

A careful client subtracts a safety margin that covers its expected drift and delay. That shrinks the window but cannot close it, because the pause can be longer than any margin. The margin is a mitigation. A fencing token is the guarantee.

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