LEASES & FENCING TOKENS

A lock the holder is sure it still has

L215 MINDISTRIBUTED SYSTEMS
PREDICT

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.

▶ SIMULATION
CLIENT 1
believes lease: none
actually valid: —
idle
LOCK SERVICE + STORAGE
clock: t=0s
lease: free
value = —
fencing: off
CLIENT 2
believes lease: none
actually valid: —
idle
✓ storage never applies a write from an expired lease✓ tokens applied at storage are non-decreasing
SCHEDULE
01[C1]C1: acquire lease on /orders/42
02[C1]C1: stop-the-world GC pause begins
03[LOCK]LOCK SERVICE: clock reaches t=12s — a lease granted at t=0 has expired
04[C2]C2: acquire lease on /orders/42
05[C2]C2: write value B to storage (with its token)
06[C1]C1: GC pause ends — still believes it holds the lease
07[C1]C1: write value A to storage (with its token)
Start of no fencing scenario. 7 steps.
// client
lease = lock.acquire('/orders/42')
// ... long GC pause; lease expires meanwhile ...
storage.write(data)   // storage cannot tell this is stale

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.

BREAK IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

A lease tells the lock service when to move on. It tells the holder nothing it can trust, because a stalled holder cannot see the clock. So the check cannot live in the client. Put the lock service’s monotonic counter in every write and let the resource refuse anything older than what it has already seen. Anyone who says “the lock expired, so it is safe” has not asked who enforces the expiry.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: PACELC & CONSISTENCY MODELS →