LEASES & FENCING TOKENS
A lock the holder is sure it still has
A lease is a lock that expires after 10s unless renewed. Client 1 takes it, then pauses for 12s. Client 2 acquires the lease and writes. Client 1 wakes and writes. Storage just applies writes as they arrive. What does it hold?
A lease is a lock with an expiry: hold it, renew it, lose it if you stop renewing. The expiry protects the system from a dead holder. It does not protect the resource from a holder that is merely slow. A client that stalls (GC pause, swapped out, a network hiccup) cannot see time passing. The lease expires, the next client takes it and writes, then the first client wakes and writes over it. A fencing token fixes this at the resource: every grant carries a strictly increasing number, and storage refuses any write with a token lower than the highest it has already applied.
// client
lease = lock.acquire('/orders/42')
// ... long GC pause; lease expires meanwhile ...
storage.write(data) // storage cannot tell this is stale// lock service: every grant carries a strictly increasing token
lease = lock.acquire('/orders/42') // -> { token: 34, expiresAt }
storage.write(lease.token, data)
// storage
if (token < highestSeen) reject(STALE)
highestSeen = token
apply(data) Watch the second invariant, tokens applied at storage are non-decreasing. In NO FENCING it turns red at the last step, when token 33 lands after token 34. In FIX: FENCING TOKEN the same step is refused and the value stays B. The first invariant stays green there too, but only because of the order: fencing compares tokens, not clocks. FENCED, BUT C1 WRITES FIRST shows the other order: C1 wakes and writes before C2 has written anything, storage applies token 33 although its lease has expired, and the first invariant turns red. Fencing promises the second invariant, not the first: once token 34 has been applied, nothing older can follow.
Fencing is off. Every step must run. Order them so storage ends up holding a write from a lease that had already expired.
GOAL: storage applied a write whose lease had expired
Implement the lock service and the storage. lock.acquire(clientId, now) must hand out a strictly increasing token with each grant and refuse while a lease is live; storage.write(token, data) must reject any token below the highest it has applied. The grader replays the pause scenario and a case where writes arrive out of order.
Distributed locks from ZooKeeper, etcd or Redis guarding a job scheduler, a leader-only writer, a file in S3 or a row in a database. The symptom is a stale overwrite with a clean audit trail. Any system that pauses (every JVM, every VM that can be live-migrated) is the slow holder.
“How would you implement a distributed lock?” Expect the follow-up “what happens if the holder pauses past the lease?” Say that expiry alone is unsafe, describe the fencing token, and say where it is checked: in the storage, on every write, by comparing against the highest token seen.
USED IN
Chapters that build on this lesson.
- Failover, split brain and fencingDatabases
- Project: shard the shopDatabases