AlgoViz

Apache ZooKeeper

medium 30 min

ZooKeeper 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 hearSayBecause
Exactly one scheduler may runLeader election with a session, and fencingTakeover is automatic and stale leaders are stopped
Instances come and goEphemeral registration, or the platform's discoveryDead ones vanish
Shared settings change at run timeSmall values and watchesNo polling
A lock for 50,000 requests a secondNot ZooKeeper; change the designEach lock is a majority write
We already run KubernetesLease objectsNo 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.