Course map

Topic 137 / Advanced

Distributed locks and leases

Chapter 8.5 · Locks, Transactions, and Sagas

What and why

Pessimistic locking (→ 71) prevents a conflicting access by making a second transaction wait — but that only works when both contenders share one database that can hold the lock state and detect a crashed holder. Across independent machines, nothing automatically notices if a lock holder crashes without releasing it, so every distributed lock is really a lease: a lock that expires on its own after a bounded time, so the system recovers even if the holder never calls "release." Builds on: → 71 (concurrency control: MVCC, optimistic and pessimistic locking).

Diagram

t0: Process A acquires lease (TTL 10s), begins work
t1: A pauses (GC pause, 15s) — lease silently expires at t0+10s
t2: Lease expires; Process B acquires the same lease, begins work
t3: A wakes up, still believes it holds the lease, writes to shared resource
t4: B also writes to shared resource  ← TWO holders acting at once

How it works

  1. A client acquires a lease by writing a key (Redis, etcd, or a database row) with a time-to-live; the lease store — not the client — is the authority on who holds it.
  2. The holder must finish its critical work before the TTL expires, or periodically renew the lease (extend the TTL) while still working.
  3. If the holder crashes or stalls without renewing, the lease expires on its own and another client can acquire it — no external failure detector is needed to recover from a dead holder.
  4. The dangerous case is a holder that is not dead, merely paused (a GC pause, a scheduling delay, a brief partition → 126, or clock drift → 135) longer than the TTL: the lease store correctly hands the lease to someone else, but the original holder has no way of knowing this once it resumes, unless it checks.
  5. A fencing token (→ 133 — the same mechanism, applied here to lease expiry instead of leader election) fixes exactly this: the protected resource refuses any write carrying a token lower than the highest it has already seen, so even a holder that doesn't know it lost the lease has its late writes refused.
  6. Without a fencing check at the resource the lease protects, the TTL alone only reduces the probability of two holders acting at once — it does not prevent it.

Practical example

(illustrative). Process A acquires a 10-second lease and immediately pauses for 15 seconds (a GC pause). At 10 seconds the lease store expires A's lease; process B acquires it at 10.1 seconds with fencing token 2 (A's acquisition had token 1). A wakes at 15 seconds, still believing it holds the lease, and writes with token 1 — a resource checking fencing tokens rejects it because 1 < 2; without that check, both A's and B's writes would succeed, corrupting whatever the lease protected.

When to use it

  • Use when: a single-writer or single-worker guarantee is needed across processes that may crash (a reservation hold — Projects 7, 14, 16; duplicate-run prevention — Project 11) — and always pair it with fencing at the protected resource, not just at acquisition.
  • Avoid when: relying on TTL alone as sufficient protection for anything where a double-holder causes real damage (double-charging a payment, double-booking a seat).
  • Failure scenarios: a TTL set too short relative to realistic pause lengths, causing frequent unnecessary handoffs; a TTL set too long, delaying recovery from a genuinely dead holder; fencing tokens generated but never checked, which silently defeats the whole mechanism.
  • Simpler alternative: a database row lock (→ 71) when every contender already shares one database — no lease, TTL, or fencing token needed, because the database itself detects a crashed connection and releases the lock.

Practice

(Pattern kind — implement minimal, spot the misuse; 20–30 min, lab Redis). Implement lease acquisition as SET lock_key holder_id NX EX 10 and release as a check-then-delete. Simulate the GC-pause failure: acquire as "A," let it expire, acquire as "B," then have "A" attempt a protected write using only its original acquisition as proof of ownership — confirm the write isn't actually prevented. Add a monotonically increasing token returned at acquisition and a check at the "protected resource" (a second key storing the highest token seen) that rejects "A"'s stale token. Done when the misuse is reproduced first, then shown fixed by the token check.

Shared lab setup

Key takeaway

Every distributed lock is a lease that can expire out from under its holder; a fencing token at the protected resource, not the TTL alone, is what actually prevents two holders from acting. Done when you can: reproduce the GC-pause double-holder failure and fix it with a fencing-token check. Used next in → 138; Projects 7, 11, 14, 16.

References

Kleppmann and Riccomini, Designing Data-Intensive Applications — see references.md.

Source directory

Search the guide