Apache ZooKeeper
medium 30 minZooKeeper is a small, replicated, strictly ordered tree of notes that programs use to coordinate: who is the leader, who is alive, who holds a lock, what the settings are.
When it comes up
The moments this idea belongs in a design interview, and the moments it does not.
Signals
When to bring it up
| You hear | Say | Because |
|---|---|---|
| Exactly one scheduler may run | Leader election with a session, and fencing | Takeover is automatic and stale leaders are stopped |
| Instances come and go | Ephemeral registration, or the platform's discovery | Dead ones vanish |
| Shared settings change at run time | Small values and watches | No polling |
| A lock for 50,000 requests a second | Not ZooKeeper; change the design | Each lock is a majority write |
| We already run Kubernetes | Lease objects | No second quorum system |
Two copies of a billing job must never run at once. How do you ensure it?
BreaksRun it on one machine
- How
- A cron entry on a single server.
- Limit
- If the machine dies, billing stops. If someone starts a second, both run.
- Choose when
- A job that may be late and is harmless twice.
WorksA ZooKeeper lock
- How
- Ephemeral sequential node, lowest wins.
- Limit
- A paused holder can continue after its session expired.
- Choose when
- Duplicates are wasteful, not wrong.
StrongLock plus fencing plus idempotent charges
- How
- Each charge carries the lock number and an idempotency key; the database refuses an older number or a repeated key.
- Limit
- The storage must check.
- Choose when
- Money or data loss is at stake.