Try
↑ ↓ move↵ openesc close
In this lesson · topic 2 of 7
  1. 1. The lock that lies
  2. 2. Leases and fencing tokens
  3. 3. Trusting storage against token storage
  4. 4. What a 5-second lease and a clock check buy
  5. 5. When the lock's safety depends on the clock
  6. 6. Sessions, jeopardy and sequencers in a production lock service
  7. 7. Check the stale write

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
client Aasksclient Bidlelock servicefreestoragetoken 32acquirelease, token 33t = 0 msstorage trusts that whoever writes holds the lock
holder
A
token
33
expires
1020 ms
  1. 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.

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.

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