TOPIC #86Advanced 9 min read

Distributed Locks: Redlock & ZooKeeper

CSD
CompleteSystemDesign Editorial
Report an issue
Key takeawayCore Architecture Summary

Prevent concurrent resource mutation across servers: Redis single-instance locks, Redlock algorithm, ZooKeeper recipes, and fencing tokens.

Key Glossary Concepts in this TopicAll Glossary Terms

01.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):

bash
SET 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:

  1. Client 1 acquires the lock with a 10-second TTL.
  2. Client 1 immediately hits an unexpected 15-second Stop-the-World JVM Garbage Collection pause, hypervisor CPU freeze, or disk paging stall.
  3. While Client 1 is frozen, the 10-second TTL expires on Redis, and Redis deletes the lock key.
  4. Client 2 requests the lock, successfully acquires it, writes an update to the database, and commits.
  5. 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 🛡️

PRO Architecture Blueprint

The GC Pause Lock Hazard & Fencing Token Solution 🛡️

How Garbage Collection pauses break distributed locks without fencing tokens.

The GC Pause Lock Hazard & Fencing Token Solution 🛡️
100%
Touchpad: Pinch to zoom • Drag to pan
Rendering visual architecture flowchart...
PRO & LIFETIME CURRICULUM

Unlock Topic #86: Distributed Locks: Redlock & ZooKeeper

You are viewing a preview. The full in-depth engineering deep dive, interactive simulators, architecture flowcharts for this topic, along with self-assessment quizzes, are available with Pro or Lifetime Access.

Production Deep Dive

Failure modes, high-throughput bottlenecks, and real FAANG implementation decisions.

Interactive Blueprints

Interactive system topology diagrams, live parameter simulators, and downloadable SVG charts.

Knowledge Assessment

Staff-level multiple-choice quiz questions with instant feedback and answer explanations.

Cross-Device Progress Sync

Firebase Google authentication automatically syncs your completed topics and quiz scores.

Rate This Architecture ChapterFeedback & Rating

How clear and actionable was this distributed systems breakdown?

Interactive Engineering Workbenches: