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

Leases and their timing assumptionsProcess pause and lease-expiry hazardsFencing tokens and generation checksLease and fencing implementation
1

Read

In this order, only the parts listed

0 of 2 done

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

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

2

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

Try it yourself

Closed-book, before checking anything

Closed-book, walk tokens 33 and 34: client 1 holds 33 and pauses past expiry; client 2 takes 34 and writes; client 1 resumes and writes with 33, which storage rejects as backward. State the storage invariant: the largest token seen wins, and anything lower is refused.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

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

5

Practice

  • Implement a Lease Lock with Fencing Tokens

    Implement acquire, renew and release: a lease at now expires at now + ttl and is active while now < 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 8

    After 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