Distributed Locking
Difficulty: Medium | Topic #17
What to Learn
Why distributed locks are needed, Redis-based locks (SET NX EX), Redlock algorithm, fencing tokens to handle lock expiry races, lock contention and its impact on throughput.
Resources
- Hello Interview: Dealing with Contention ↗
- Gaurav Sen: Distributed Locks ↗
- Ex-Google SWE: Distributed Locking ↗
Covered by Problems
| Problem | Difficulty | Link |
|---|---|---|
| Ticketing System (Ticketmaster) | Medium | → |
| Online Auction | Medium | → |
| Job Scheduler | Medium | → |
| Online Chess | Hard | → |
Key Concepts to Master
- Why network partitions make distributed locking hard
- Redis SET NX EX for simple single-node locks
- Redlock algorithm across 5 nodes for higher correctness
- Fencing tokens to prevent stale lock holder actions
- Lock-free alternatives (optimistic locking, CAS) as a performance improvement