Unit 11 of 12 · about 5 hours · explain, implement
Leases, process pauses, fencing tokens and a lease implementation
A lease is authority with an expiry date. Pauses forge the date, so the storage layer must check the token, not the clock.
You'll be able to
- State the lease contract and the timing assumptions a client-side validity check needs.
- Construct a pause scenario that lets an expired holder write, and show fencing blocking it.
- Implement lease acquire, renew and release with monotonic fencing tokens, tested for expiry, pause and partition cases.
Covers
Read
In this order, only the parts listed
0 of 2 done
- Pauses, leases and fencingDDIA 2e - Chapter 9 The Trouble with Distributed Systems
Chapter 9, Process Pauses (pp. 366-371): the lease-renewal loop with its two flaws and the pause catalog; Distributed Locks and Leases (pp. 373-377): the HBase zombie, fencing tokens 33 and 34 with Figure 9-6, and fencing across replicas with Figure 9-7
- Fencing tokens and the pause raceHow to Do Distributed Locking (Kleppmann, 2016)
Making the lock safe with fencing (tokens 33 and 34 with storage-side rejection); the check-then-write pause race; the Redlock synchrony assumptions
Treat the Redlock critique as dated to 2016. The lasting lesson is the token rule, not the product verdict.
Focus on
- A lease grants authority until expiry. A timing-only client needs bounded clock error or drift and a bound on delay from its validity check through the protected operation. Delayed renewal must not extend authority without confirmation. A pause or delayed request can invalidate a client's check; fencing closes that gap at storage.
- The book's loop fails twice: expiry set by one machine is read against another's clock, and a 15-second pause between the validity check and the request lets the lease die mid-act unnoticed.
- Fencing moves enforcement to storage: every grant carries a higher token, here 33 then 34, and storage rejects any write whose token moved backward. The lock service grants; the storage service refuses.
- Tokens must rise across grants and survive failover. DDIA Figure 9-7 separately assumes a fixed leaderless replica set, intersecting read and write quorums, and last-write-wins comparison with the token dominating the timestamp. Only under those rules can a stale write on a missed replica lose at the next quorum read and later be repaired; this is not protection for arbitrary external side effects.
Common mistakes · 3
- Renewing early and then pausing. The renewal only proves liveness at renewal time; a pause after it still expires the lease under the holder.
- Releasing or renewing without matching owner and token. Any holder that can release another's lease can steal authority without waiting.
- Generating tokens from a counter that resets on lock-service failover. Tokens must be monotonic across crashes too, or the new service reissues 33 after 34.
Try it yourself
Closed-book, before checking anything
Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Why must the fencing check live on the storage service rather than the lock service?Answer
The paused holder never contacts the lock service again; it goes straight to storage believing its lease is live. Only storage sees the stale write and can refuse it.
2.Client 1 holds token 33 with 30 seconds left, then pauses 60 seconds. Client 2 holds 34 and writes twice. Client 1 resumes and writes. Which writes survive?Answer
Client 2's two writes survive. Client 1's write carries 33 against a seen 34 and is rejected, even though 33 was valid when issued.
3.The lock service crashes and restarts, reissuing token 33 to a new holder while 34 exists. What breaks?Answer
Monotonicity breaks: storage has seen 34, so it rejects the legitimate new holder's 33. Token state needs the same durability as the lease.
4.Client 2 reaches only two of three replicas with token 34. Where can client 1's token-33 write still land, and why is that safe?Answer
On the missed third replica. It stays safe because reads go to a quorum, which always includes a replica holding 34, and repair then overwrites the stale copy.
Practice
- Implement a Lease Lock with Fencing Tokens
Implement
acquire,renewandrelease: a lease atnowexpires atnow + ttland is active whilenow < expiresAt, tokens rise by one per grant and are never reused; the examples map Alice and Bob through expiry at time 10 and release at time 8After it passes, add the storage-side token check the page omits: a write with a backward token must be rejected even from the recorded holder.
You're done when
You walk the 33 and 34 case with the invariant stated, and your lock passes the problem's expiry examples plus your own pause-and-stale-write test.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 12 more in the section
- DDIA 2e - Chapter 9 The Trouble with Distributed SystemsMartin Kleppmann and Chris Riccomini · primary
Read clocks, pauses, unreliable networks, partial failures and knowledge limits. For each failure, record what a node observes, what is true globally and what action is safe under that uncertainty.
- How to Do Distributed Locking (Kleppmann, 2016)Martin Kleppmann · required
Read the process-pause, network-delay and fencing-token sections with DDIA 2e Chapter 9 "Distributed Locks and Leases". Treat the Redlock critique as dated to 2016; the lasting lesson is that the storage service must reject a write that carries an older token.
Also in this section
- Time, Clocks, and the Ordering of Events in a Distributed System (Lamport, 1978)Leslie Lamport · required
Read the partial ordering and logical clocks sections: happens-before, the Clock Condition, rules IR1 and IR2, and the total order. Its ties are broken arbitrarily, so the total order is not causality.
- Logical Physical Clocks and Consistent Snapshots (Kulkarni et al., 2014)University at Buffalo · support
Read the hybrid logical clock algorithm and its bounds; the paper reports the logical counter staying well below its proved bound in practice, so measure your own.
- SWIM: Scalable Weakly-consistent Infection-style Process Group Membership (Das et al., 2002)Cornell University · support
Read the direct and indirect probing detector, infection-style dissemination, the suspicion mechanism and incarnation numbers. Suspicion timeouts depend on the workload.
- The phi Accrual Failure Detector (Hayashibara et al., 2004)JAIST · support
Read how phi is computed from the heartbeat inter-arrival history and how a threshold trades detection time for false suspicion. Its normal approximation misleads under bursty delay, so test with pause traces too.
- Cambridge Concurrent and Distributed Systems Notes (Kleppmann) - Time, Clocks and OrderingUniversity of Cambridge (Martin Kleppmann) · support
Use Chapter 3 (physical clocks, NTP and monotonic clocks, happens-before) and Section 4.1 (Lamport and vector clocks with worked examples) as the derivation workbook: work Exercises 7 to 13. Each section marks where its public lecture video starts.
- Building Distributed Systems - The TimekeeperBuild Distributed Systems · optional
Only the first three tasks are free (clock reads and backward jumps, a monotonic wrapper, a split-brain lease). Tasks are graded in the browser after login, so treat a pass as a drill and keep your local harness as the evidence.
- Building Distributed Systems - The GossiperBuild Distributed Systems · optional
Only the first three tasks are free (basic broadcast, tree topology, random gossip). Tasks are graded in the browser after login, so treat a pass as a drill and keep your local harness as the evidence.
- HLD Handbook - Clocks, Failure Detection and Consistent HashingHLD Handbook · required
Read Part 3 clocks and ordering, failure detection and consistent hashing. Use its exercises to compare assumptions, then verify claims against your own histories.
- Gossip Glomers - Unique IDs and BroadcastFly.io and Jepsen · required
Each challenge runs locally under Maelstrom (v0.2.4; the pages were tested on 0.2.3) with its checker and, where the page says so, the partition nemesis; keep the run directory as the history. Do challenge 2 (unique IDs under partition with total availability) and 3a to 3c (broadcast, then with partitions). A pass shows uniqueness or convergence in that run, not ordering or recency.
- MIT 6.5840 - Failure and Paper Discussion LensMIT PDOS · support
Use the RPC, GFS, Raft and consistency lectures to identify which timing assumptions affect progress and which invariants must not depend on time. Do not start the Raft lab yet.
- Replicated Log and Stream Processor - Time and Membership SliceRoadmap-Final · support
Add incarnation IDs, logical timestamps, gossip membership, suspicion metrics and fenced ownership. Retain seeds and minimized traces for every false-failure or stale-actor case.
- AlgoMaster System Design practice - clock, gossip and lease drillsAlgoMaster · support
Use only the named problems: Advance a Lamport Logical Clock, Compare Two Vector Clocks, Update a Vector Clock on Receive, Advance a Hybrid Logical Clock, Simulate Gossip Rumor Spread and Implement a Lease Lock with Fencing Tokens. Each states its function signature and checks small deterministic cases.