Skip to main content
distributed systems

Distributed locks — Redlock, ZooKeeper, Chubby

11 min read
Fully authored

The 3 industry patterns for mutual exclusion across machines.

Multiple servers, one shared resource. Only one may act at a time. This is mutual exclusion across a network — and it's much harder than it looks. The three industry patterns each accept a different failure mode.

Redlock (Salvatore Sanfilippo, 2016)

Acquire the same key on N=5 independent Redis nodes with SET NX PX timeout. If (N/2+1) succeed within a small time budget, you hold the lock.

  • Wins: simple, no consensus infra needed.
  • Loses: Martin Kleppmann's famous critique (2016) — clock drift + GC pauses can violate mutex.
  • Verdict: good enough for non-critical locks. Not for money.

The correctness question you can't avoid

What if the lock holder pauses for 30s (GC, VM migration)? The lease expires, another node takes the lock, both write. Data corruption. The fix: fencing tokens. Every lock acquisition returns a monotonic counter. The resource-owner (DB, storage) checks the token; older tokens are rejected. This is why fine-grained locks require DB cooperation.

Practice what you just read

Every foundation concept has a companion quiz to close the loop.