Distributed Locks: Redlock & ZooKeeper
Prevent concurrent resource mutation across servers: Redis single-instance locks, Redlock algorithm, ZooKeeper recipes, and fencing tokens.
01.1. Why Distributed Locks are Fragile (Martin Kleppmann's Critique)
In distributed architectures, multiple worker services frequently need mutual exclusion on shared resources—such as ensuring a single cron job runner processes payroll, or preventing two concurrent checkout threads from double-spending a customer's gift card balance.
A common pattern is acquiring a distributed lock in Redis with a Time-To-Live (TTL):
bashSET lock:resource_123 "client_A_uuid" NX PX 10000
The Stop-the-World GC Hazard:
As distributed systems expert Martin Kleppmann famously demonstrated, TTL-based locks without fencing tokens are inherently unsafe:
- Client 1 acquires the lock with a 10-second TTL.
- Client 1 immediately hits an unexpected 15-second Stop-the-World JVM Garbage Collection pause, hypervisor CPU freeze, or disk paging stall.
- While Client 1 is frozen, the 10-second TTL expires on Redis, and Redis deletes the lock key.
- Client 2 requests the lock, successfully acquires it, writes an update to the database, and commits.
- Client 1 wakes up from its GC pause. Believing it still holds the lock (since it executed no release logic), it blindly executes its database write, silently corrupting or overwriting Client 2's data!
The GC Pause Lock Hazard & Fencing Token Solution 🛡️
The GC Pause Lock Hazard & Fencing Token Solution 🛡️
How Garbage Collection pauses break distributed locks without fencing tokens.
Unlock Topic #86: Distributed Locks: Redlock & ZooKeeper
You are viewing a preview. The full in-depth engineering deep dive, interactive simulators, architecture flowcharts, and self-assessment quizzes for this topic are available with Pro or Lifetime Access.
Failure modes, high-throughput bottlenecks, and real FAANG implementation decisions.
Interactive system topology diagrams, live parameter simulators, and downloadable SVG charts.
Staff-level multiple-choice quiz questions with instant feedback and answer explanations.
Firebase Google authentication automatically syncs your completed topics and quiz scores.
How clear and staff-actionable was this system breakdown?