DISTRIBUTED SYSTEMS & ARCHITECTURE · SUBTOPIC
Distributed Locks & Fencing Tokens
Avoid deadlocks and zombie writers using leases and monotonic fencing tokens.
IntermediatePart of Distributed Locks & Unique IDs
Subtopics in Distributed Locks & Unique IDs (2)
In plain words
Giving someone a temporary hotel keycard with a version number: if they take too long and the front desk issues Card #34 to the next guest, the door lock automatically rejects Card #33.
Think of it like…
Hotel keycard encoder invalidating older card serial numbers.
Loading interactive diagram
Loading interactive exercise
Theory & how it works
Efficiency locks vs. Correctness locks
If you just want to avoid sending a reminder email twice ("efficiency"), a simple Redis SET NX PX is fine. If duplicate action loses money ("correctness"), use a consensus-backed lock (etcd/ZooKeeper) with fencing tokens or database uniqueness constraints.
Key points & practical guide
Summary checklist
- Always attach a TTL lease to distributed locks
- Check ownership value inside a Lua script before deleting
- Use fencing tokens when correctness matters
When to use it
- Preventing double-runs of cron jobs, inventory holds, leader coordination
Watch out for
- Using
DEL lockunconditionally - Worker A might delete Worker B’s newly acquired lock
Quick check
Loading quiz