In this lesson · topic 2 of 7
Topic 2
Leases and fencing tokens
6 min read
The lease
A client asks the lock service for the lock. If it is free, the service grants a lease: the lock is yours for a fixed time, after which it is free again unless you renewed it. The service keeps one holder and one expiry time on its own clock. When the expiry passes, it hands the lock to the next waiter.
The client keeps its own idea of when the lease ends, on its own clock, starting from when the grant arrived. That idea is already late by the network delay, and it drifts further if the client’s clock runs at a different rate from the service’s. A careful client subtracts a margin before acting. A margin shrinks the window. It cannot close it, because the stall can be longer than any margin.
The token
The fix is a number. Every time the service grants the lock it also issues a token, one higher than the last. The client sends the token with every write it makes under that lease. Storage keeps the highest token it has seen and rejects any write carrying a lower one.
Step through the scenario from the previous topic twice, once with storage that trusts the lock and once with storage that checks the token.
A lease, a stall, and a late write
Step 1 / 6- holder
- A
- token
- 33
- expires
- 1020 ms
Client A takes the lock
A asks the lock service. It is free, so the service grants a lease of 1000 ms and, with it, token 33. Every grant gets the next number.
A does its work
A reads, computes and prepares its write. It checks its own clock: 400 ms of a 1000 ms lease used, plenty left. It is about to send the write.
A stalls
Between the check and the send, A stops. A garbage collection pause, a page swapped to disk, a frozen virtual machine. It will resume as if no time passed. Meanwhile B asks for the lock and waits.
The lease expires
At 1020 ms the lock service sees A’s lease run out. It cannot tell a stalled A from a dead one, and waiting forever would block everyone, so it hands the lock to B with token 34.
B writes
B does its work and writes. Storage records the write. With fencing it also records token 34 as the highest it has seen.
A wakes and corrupts
A resumes and sends the write it prepared 1500 ms ago. Storage has no idea the lease is gone, accepts it, and A’s stale data lands on top of B’s. The lock did everything right and the data is still wrong.
- Client A takes the lock. A asks the lock service. It is free, so the service grants a lease of 1000 ms and, with it, token 33. Every grant gets the next number. holder: A; token: 33; expires: 1020 ms.
- A does its work. A reads, computes and prepares its write. It checks its own clock: 400 ms of a 1000 ms lease used, plenty left. It is about to send the write. A thinks: 600 ms left; service agrees: 620 ms left.
- A stalls. Between the check and the send, A stops. A garbage collection pause, a page swapped to disk, a frozen virtual machine. It will resume as if no time passed. Meanwhile B asks for the lock and waits. A: stalled for 1500 ms; B: queued.
- The lease expires. At 1020 ms the lock service sees A’s lease run out. It cannot tell a stalled A from a dead one, and waiting forever would block everyone, so it hands the lock to B with token 34. holder: B; token: 34; A still believes: it holds the lock.
- B writes. B does its work and writes. Storage records the write. With fencing it also records token 34 as the highest it has seen. storage: last write from B.
- A wakes and corrupts. A resumes and sends the write it prepared 1500 ms ago. Storage has no idea the lease is gone, accepts it, and A’s stale data lands on top of B’s. The lock did everything right and the data is still wrong. write from A: accepted; data: B’s write overwritten.
With fencing, the stalled client’s late write carries token 33. Storage has seen 34. The write is rejected and the client learns, from the rejection, that it lost the lock. The newer holder’s data is untouched.
Where the check has to live
The token must be checked by the thing being protected. Checking in the client does not help, because the client checks and then stalls. Checking at the lock service does not help, because the write never goes through the lock service. Only storage sees the write and the token together at the moment that matters.
That has a consequence for design. A lock can only protect a resource that can compare tokens. A database with conditional writes can. A file system with no version check cannot, and neither can a third-party API that just takes a request. For those, the token has to be turned into something they do understand: a version to compare, an idempotency key to deduplicate, or a new object named by the token with an atomic pointer swap at the end.
The lock service itself
A lock service on one machine is one crash away from losing every lock. Real lock services, in the style of Burrows’s Chubby, are small replicated state machines over a consensus protocol like the previous lesson’s Raft. Consensus gives them one agreed sequence of grants and expiries, so the token counter stays unique and increasing across failovers. The lease model covers clients dying. Consensus covers the service’s own nodes dying.