Small, highly reliable stores that other systems use for locks, leaders, config and who-is-alive.
ZooKeeper and etcd are small, very reliable stores that a whole cluster of other machines can trust. They keep a little data (settings, who the leader is, who holds a lock) and they never lose it, because every change is agreed by a majority of their own servers.
Think of a distributed system with 100 machines. They all need to answer questions such as "who is the leader?", "which servers are alive?" and "what is the current setting?". If each machine guessed on its own, they would disagree. So we put the answers in one tiny, trusted place: a coordination service.
The coordination service is itself a small cluster (usually 3 or 5 servers). It uses a consensus protocol to keep the copies the same: ZAB (ZooKeeper Atomic Broadcast) in ZooKeeper, and Raft in etcd. A write is only accepted when a majority has stored it, so one or two failed servers do not lose anything.
The two tools offer a few simple ideas that can be combined into many recipes. Ordered, versioned data (every change gets a growing number: zxid in ZooKeeper, revision in etcd). Temporary data that disappears if its owner stops being alive (ephemeral nodes, leases). And watches, which tell you when something changes, so you do not have to keep asking.
| Idea | ZooKeeper | etcd |
|---|---|---|
| Data model | A tree of znodes, like folders and files | A flat key-value space (keys can look like paths) |
| Consensus | ZAB | Raft |
| Temporary data | Ephemeral znode, tied to a client session | Key attached to a lease with a TTL |
| Getting news | Watch (fires once, then you set it again) | Watch (a stream of events from a revision) |
| Ordering number | zxid | revision |
| Ordered names | Sequential znodes (n_0000000007) | Use the revision or a lease key |
| Transactions | multi() operations, version checks | Txn with compare-and-swap conditions |
| API and language | Own protocol, Java client | gRPC and HTTP, written in Go |
| Recipe | How it works |
|---|---|
| Leader election | Everyone creates a sequential ephemeral node. The lowest number leads. Each follower watches the one just below it. |
| Lock | Same idea. The lowest number holds the lock. The rest wait by watching their predecessor. |
| Config | Store settings in a node. Clients watch it and reload on change. |
| Service discovery | Each server creates an ephemeral node with its address. Clients list and watch the folder. |
| Group membership | Same as service discovery. The folder is the list of who is alive. |
| Barrier | Wait until N nodes exist, then everyone continues. |
A simplified model. The real stores replicate every change to a majority of their own servers (ZAB in ZooKeeper, Raft in etcd) before saying yes.
concurrency package already implement locks and elections. Writing your own is easy to get subtly wrong.| Term | Meaning in easy words |
|---|---|
| Coordination service | A small, reliable cluster that other systems use for agreement and shared facts |
| znode | A node in ZooKeeper's tree. It holds a small piece of data |
| Ephemeral node | A znode that is deleted when the session that made it ends |
| Sequential node | A znode whose name gets an increasing number added by ZooKeeper |
| Session | A client's connection to ZooKeeper, kept alive with heartbeats |
| Watch | A request to be told when a node or key changes |
| Lease | In etcd, a timer. Keys attached to it are deleted if it is not renewed |
| ZAB | ZooKeeper Atomic Broadcast, the protocol that keeps ZooKeeper servers in step |
| Raft | The consensus protocol that etcd uses |
| zxid / revision | The growing number given to every change |
| Quorum | A majority of the servers (3 of 5). Needed for every write |
| Herd effect | Everyone waking up for one event, when only one needed to |
| Choice | We gain | We lose |
|---|---|---|
| Short session timeout / lease | Fast failover | False alarms on pauses and slow networks |
| Long session timeout / lease | Fewer false alarms | Slow failover |
| 5 servers instead of 3 | Survives 2 failures | Slower writes, more to run |
| Read from a follower | More read capacity | May be slightly old (ask for a linearizable read if needed) |
| Watch one key | No herd | A little more code |
| Spread over 3 zones | Survives a zone loss | Higher write latency |