Hard · 12 steps · ~75 min
Design a distributed lock and leader election
Mutual exclusion that survives crashes, pauses and failovers.
Many machines must agree that exactly one of them does a job at a time: run the nightly report, own a shard, or be the leader. Design a lock and leader-election service that stays correct when holders crash, freeze for 40 seconds, get partitioned or when its own storage fails over, while handling tens of thousands of lease renewals a second.
Same problem as ZooKeeperetcdGoogle Chubby
How it works
- Acquire: a client gets a lease and a fencing token if the lock is free.
- Renew: the holder heartbeats before the lease expires.
- Elect: a leader is just the holder of a well-known lock, with a term that increases.
What you'll build
- The API (acquire, renew, release) and the lock schema
- A replicated, consensus-style store that survives losing the leader
- A sharded namespace for 30,000 requests a second
- A limiter and a breaker against herds and cold caches
- Reasoning about leases, fencing tokens and durability
Steps
- 1Design the API
- 2Design the data model
- 3Handle 500 lock requests/sec
- 4Why a lease and not a plain lock?
- 5Replicate the store for $1,100
- 6Lose the leader
- 7Fencing tokens
- 8Scale writes to 30,000 requests/sec for $1,600
- 9Why a consensus-backed store?
- 10Retry storm after a release for $1,500
- 11Write-behind caching for lease renewals?
- 12Cold cache after a restart