ZooKeeper and etcd
ZooKeeper and etcd are coordination services: small, replicated, strongly consistent stores that distributed systems use to agree on things. Which node is the leader? Which server owns shard 17? Is worker 42 still alive? What's the current configuration? These are questions where every node in a cluster must get the same answer, even while machines crash and networks partition, and getting them wrong causes split-brain, double-processing and data corruption.
Apache ZooKeeper came out of Yahoo and became the coordination layer of the Hadoop ecosystem: HBase, HDFS high availability, Solr, and for many years Kafka. etcd came out of CoreOS and is best known as the database behind every Kubernetes cluster. They have different APIs but the same core properties:
- Replicated with consensus: an odd-sized cluster (usually 3 or 5 servers) replicates every write through a consensus protocol (ZAB for ZooKeeper, Raft for etcd). A write is committed only once a majority has it.
- Strongly consistent: writes are linearizable, applied in one global order, and every client sees them in that order.
- Small data, kept in memory: they're built for kilobytes per key and megabytes to a few gigabytes in total, not for application data.
- Liveness tracking: ZooKeeper sessions and etcd leases tie keys to a client's heartbeats, so entries vanish automatically when the client dies.
- Watches: clients subscribe to changes instead of polling, so a change propagates to the whole cluster within milliseconds.
You'll rarely be asked to design ZooKeeper itself, but coordination comes up constantly: designing a distributed lock, a job scheduler that needs exactly one active scheduler, a sharded database that needs a shard map, a configuration service, service discovery, or a message queue's partition leadership. Interviewers want to see that you know when you need a consensus-backed store, what recipes you build on top of it (leader election, locks, membership), and why a lock alone isn't safe without fencing tokens.
Why a Coordination Service?
Suppose we run three instances of a job scheduler for availability, but only one should be scheduling at a time, or jobs run twice. We need leader election. The naive approaches all fail in instructive ways:
- "The instance with the lowest IP is the leader." If the network partitions, each side sees a different set of instances and each elects its own leader. Two leaders: split-brain.
- "Write the leader's name into a row in Postgres." Better, but who notices when the leader dies? And Postgres itself is now a single point of failure, unless you replicate it, at which point you've moved the agreement problem into the database's failover.
- "Each instance pings the others and decides." Heartbeats over an unreliable network can't distinguish a dead node from a slow one, so nodes disagree about who's alive.
Solving this correctly requires consensus: a group of machines agreeing on a sequence of values in a way that stays correct under crashes and partitions, and that keeps working as long as a majority is reachable. Consensus is subtle to implement (see /system-design/consensus), so almost no one implements it per application. Instead, you run one small consensus-backed service and build simple recipes on top of it. Google's Chubby lock service established the pattern; ZooKeeper and etcd are its open-source descendants.
The key insight to say out loud: a coordination service holds metadata about your system, not your system's data. Leaders, shard assignments, membership, configuration, locks. If you find yourself storing user records or high-volume counters in ZooKeeper or etcd, it's the wrong tool.