You acquired the lock. You did the work. You released the lock. Two clients still wrote to the same file, and one of them clobbered the other. Nobody crashed. No error logged. The data is just wrong.
That is the distributed locking problem, and it is not the problem most people think it is. Acquiring a lock is the easy 5% — one atomic command, SET key value NX PX 30000, and any key-value store will do it. The hard 95% is everything after acquisition. Networks pause. Processes freeze. Clocks drift. The holder can lose the lock without knowing it lost the lock, and that gap is where the corruption lives. This post is about the gap — and about the one mechanism that actually closes it.
The ELI5 version
A distributed lock is a bathroom key on a hook. One key, one person inside at a time. In a single building that works: you take the key, you lock the door, you return the key. Now imagine the building is a city and the key is a text message that says "you have the key." You get the text, you walk to the bathroom, and halfway there you fall asleep for an hour. Meanwhile the building decides you must have died — you have not answered in 30 seconds — and texts the key to someone else. You wake up, still holding your text that says you have the key, and walk in while the other person is inside. The lock did exactly what it was designed to do, and two people are in the bathroom anyway.
The fix is not a better key. It is a number on the door. Every time the building hands out the key, it hands out a bigger number: 41, then 42, then 43. The bathroom door remembers the biggest number it has seen and refuses anyone carrying a smaller one. You wake up with number 41, the door has already seen 42, and it does not open for you. That number is a fencing token, and it is the only part of distributed locking that does not depend on time behaving itself.
Why a lease alone doesn't save you
The standard fix for a dead holder is a lease: the lock has a TTL, so if the holder dies, the lock expires and someone else can take it. Walk the timeline that breaks it:
- Client A acquires the lock with a 30-second lease.
- Client A hits a stop-the-world GC pause. Or its host gets descheduled. Or the network partitions it.
- 30 seconds pass. The lease expires. The lock service hands the lock to Client B.
- Client B does its work and writes to storage.
- Client A wakes up from its pause. It still believes it holds the lock. It writes to storage too.
Now both A and B wrote. The lease handled the crash case. It did nothing for the pause case, because a paused process cannot tell it was paused. A 30-second JVM stop-the-world pause is not exotic. Neither is a hypervisor freezing a VM to migrate it. This is the GC-pause split-brain, and it is the heart of Martin Kleppmann's critique of using locks for correctness: safety cannot be derived from timing assumptions, because a holder can go quiet without dying, and no TTL can be tuned against an unbounded pause.

The notebook quantifies it with a toy model: pause durations exponential with an 8-second mean. At a 30-second TTL, 2.35% of lock holds end in a false expiry — the holder was paused, not dead. At 1,000 lock holds a day, that is ~24 split-brain windows a day. Shorten the TTL to 10 seconds and the false-expiry rate jumps to 28.7%. Lengthen it to 60 seconds and it falls to 0.06% — but now a genuinely dead holder blocks everyone for a full minute. That is the TTL dilemma, and no value of the TTL resolves it: short leases evict the living, long leases wait on the dead.

Redlock: the quorum answer, and its limits
The single-node TTL lock has a second failure mode: the lock store itself. If you hold a lock on one Redis master and that master fails over to a replica that has not yet received the lock — Redis replication is asynchronous — the promoted replica has no record of it, and a second client takes the same lock. Redlock, proposed by Redis creator Salvatore Sanfilippo, removes the single point of failure by spreading the decision across five independent masters with no replication between them. A client acquires the lock on a majority (at least 3 of 5) within the TTL, and the effective validity is the TTL minus the time spent acquiring, with a small allowance for clock drift. Any two majorities of five must overlap by at least one node, so two clients cannot both hold a majority at once.
The availability arithmetic is the payoff. A single Redis node at 99% uptime leaves the lock unavailable 5,256 minutes a year. A five-node Redlock quorum, with each node still only 99% up, leaves it unavailable 5.18 minutes a year — a 1,000x improvement from the same mediocre nodes, because the quorum only needs three of them. A three-node etcd quorum at 99.9% per node is down 1.58 minutes a year.

But Redlock does not fix the pause problem. Kleppmann's critique applies to it directly: Redlock's correctness leans on clock behavior — the validity window is computed from elapsed wall-clock time, and a node's clock jumping forward (a bad NTP sync, a VM pause-and-resume) can expire a lock early on one node while the others still believe it is held. Sanfilippo published a rebuttal, and the disagreement is largely about which system model is fair to assume. The practical rule survives the debate, and it is a distinction every interviewer is listening for: efficiency locks versus correctness locks. If losing the lock costs only efficiency — two workers both regenerate the same cache entry, some CPU wasted, no harm done — a single Redis key with a TTL is fine, and Redlock is plenty. If losing the lock costs correctness — a double payment, a corrupted ledger, two writers to the same file — a TTL key is not enough, and neither is Redlock on its own.
Fencing tokens: the part that actually closes the hole
The fix does not live in the lock service. It lives at the resource being protected. The lock service hands out a monotonically increasing number with every grant. Every write to the protected resource carries the number its holder was granted. The resource remembers the highest number it has accepted and rejects anything lower. The paused holder's late write arrives with a stale token and is refused — no clock, no TTL, no trust in timing required.

The notebook replays it: A writes with token 41 (accepted), B takes over with 42 (accepted), A wakes from its pause and writes with 41 again (rejected), B writes with 43 (accepted). The lock service's job is now only to hand out increasing numbers — which is a consensus problem, and consensus is exactly what etcd and ZooKeeper are built for. That is why the strong design is two pieces, not one: consensus makes the lock state correct, fencing makes the critical section correct. A design needs both. ZooKeeper's sequential ephemeral nodes and etcd's leases with revision numbers give you the monotonic token almost for free — the fencing token is the lock's version number, and the resource checks it on every write.
Three details that complete the picture:
- Renewal, not just expiry. A lease that is never renewed is a countdown to a false expiry. etcd renews at TTL/3 — a 9-second lease heartbeats every 3 seconds, and three missed heartbeats end it. The renewal interval, not the TTL, is the number that decides how fast a dead holder is detected, and it is bounded and observable.
- Detection speed is a separate axis. Gossip-based failure detection (Consul, SWIM) spots a dead node in ~2 seconds at 10,000 nodes; Eureka's 30-second heartbeat plus 90-second eviction leaves a stale entry directing traffic for 90 seconds — a 45x gap in how long a dead service keeps receiving requests. The lock's TTL and the detector's speed are different knobs, and both show up in your stale-entry math.
- Release must be owner-checked. Releasing a lock must verify the stored value still matches the releaser's unique token, atomically — a Lua script on Redis, a compare-and-swap elsewhere. A client that deletes a key it no longer owns has just handed the next holder's lock to a stranger.
Why it breaks: the failure modes, ranked
- The pause, not the crash. Crashes are the failure mode everyone designs for; pauses are the one that corrupts data, because the holder survives and keeps writing. Any design whose safety argument contains the words "within 30 seconds" is betting against GC, scheduling, and hypervisors.
- The clock jump. NTP corrections, VM suspend-resume, and manual clock changes can move a node's clock by more than the TTL. Redlock's validity window is computed from wall-clock elapsed time, and a jump breaks the arithmetic on that node alone — silently.
- The async replica. A single-master lock with asynchronous replication hands the same lock to two clients across a failover, in a window measured in milliseconds — small enough to pass every test, large enough to corrupt production data once a year.
- The stale release. Releasing without checking ownership deletes someone else's lock. It is a one-line bug with a one-line fix (check-and-delete atomically), and it shows up in production more often than any of the exotic ones.
- The wrong lock for the job. An efficiency lock used for correctness is the most common failure of all: the system works for months, because the failure needs a pause and a contested write in the same window, and then it fails in the way that cannot be retried — the data is already wrong.
Takeaways
- Acquisition is the easy 5%.
SET key value NX PX 30000is one command. Everything after acquisition — pauses, clock drift, failover — is the other 95%. - A lease cannot survive a pause. Toy model: at a 30 s TTL with 8 s mean pauses, 2.35% of holds end in false expiry — ~24 split-brain windows a day at 1,000 holds. No TTL value fixes it; short leases evict the living, long leases wait on the dead.
- Quorums fix the lock store, not the holder. Five-node Redlock with 99% nodes is down 5.18 min/yr versus 5,256 for a single node — a 1,000x availability win — but it still leans on wall-clock time, which is Kleppmann's critique.
- Fencing tokens close the hole. A monotonically increasing token, checked by the protected resource on every write, rejects the paused holder's late write with no clock and no TTL involved.
- Two pieces, not one. Consensus (etcd, ZooKeeper) makes the lock state correct; fencing makes the critical section correct. Efficiency locks can be a TTL key; correctness locks need both.
- Renew at TTL/3, release with check-and-delete. Detection speed is its own axis — gossip ~2 s versus Eureka's 90 s stale window — and an unchecked release deletes someone else's lock.
Follow for the next part — Season 2, Part 10 closes the season with timeouts, retries, and circuit breakers — on dev.to/danielsamfdo.
Honesty note: all simulation numbers are this notebook's toy-model measurements, not production telemetry — pause durations exponential with an 8-second mean, node uptimes as stated. Field figures cited: Raft/Paxos quorum logic (Parts 1–2 of this season), Redlock's five-node majority and validity arithmetic (Sanfilippo), Kleppmann's 2016 analysis and the Sanfilippo rebuttal, Consul gossip detection ~2 s at 10k nodes, Eureka 30 s heartbeat / 90 s eviction, etcd TTL/3 renewal. No Meta content per the standing exclusion.
Companion notebook: distributed_locking.ipynb — quorum availability, split-brain Monte Carlo, fencing-token replay, Redlock acquire simulation, all exec-tested.