Imagine five friends planning a trip over a patchy phone network. Messages arrive late, some get lost, and two people book different hotels because each thought they were the one in charge. Without someone everyone trusts to keep the “official” plan, chaos follows.
Groups of servers have the same problem. They need to agree on things: which server is the leader, which servers are alive, what the current settings are, who holds a lock. Getting this right across an unreliable network is one of the hardest problems in distributed systems. Apache ZooKeeper is a well-known tool built to solve it. This guide explains what it does, in plain words.
What is ZooKeeper?
Apache ZooKeeper is an open-source coordination service: a small, very reliable store that a group of servers uses to share and agree on a few important facts.
It was originally built at Yahoo for its large data systems and became an Apache project. (The name is a joke: the Hadoop world was full of projects named after animals, and someone had to keep the zoo in order.)
You can think of it as a tiny shared notebook with three special powers:
- Everyone sees the same thing. All changes happen in one agreed order, so servers don’t get conflicting answers.
- It notices when someone leaves. Entries can disappear automatically when the server that wrote them crashes or disconnects.
- It tells you when something changes. Servers can ask to be notified (“watch”) when an entry changes, instead of checking constantly.
ZooKeeper is not for storing your application’s data. It holds small pieces of coordination information, kilobytes rather than gigabytes.
How ZooKeeper stores things: znodes
Data in ZooKeeper is organised like a file system tree. Each entry is called a znode and has a path, like /app/config/db-url or /app/workers/worker-7.
Two kinds of znode make most of the magic possible:
- Persistent znodes stay until someone deletes them. Good for settings.
- Ephemeral znodes are tied to the session of the server that created them. If that server crashes or loses its connection for too long, ZooKeeper deletes the znode automatically.
Znodes can also be sequential: ZooKeeper adds an increasing number to the name (lock-0000000041, lock-0000000042), which gives a fair order to who came first.
What ZooKeeper is used for
- Leader election. Pick exactly one server to be in charge (for example, the only one allowed to accept writes), and pick a new one quickly if it dies.
- Membership (who’s alive). Each server creates an ephemeral znode under
/workers. The list of children is the list of live servers. Crash, and you vanish from the list. - Configuration. Store shared settings in one place. Servers watch them and pick up changes without a restart.
- Distributed locks. Make sure only one server at a time does a particular job, like running a nightly report.
- Service discovery. Services register where they can be reached; clients look them up.
Leader election, step by step
A common recipe:
- Each server that wants to lead creates an ephemeral, sequential znode under
/election, such asn_0000000041. - The server with the lowest number is the leader.
- Every other server watches the znode just before its own (not the leader’s), so only one server is woken up at a time.
- If the leader crashes, its session ends and its znode is deleted automatically. The next server is notified, sees it now has the lowest number, and becomes leader.
No human steps in, and only one server believes it is the leader as long as each one checks the shared truth, which is exactly what you need for safe failover without split brain. (Real systems also add fencing, so a stale former leader can’t keep writing after it lost its role.)
How ZooKeeper stays reliable: the ensemble
ZooKeeper itself must not be a single point of failure, so it runs as a small group of servers called an ensemble, usually 3 or 5.
- One ZooKeeper server is its own internal leader; the others follow. Every change is agreed by a majority (quorum) before it counts, using ZooKeeper’s own agreement protocol, called ZAB.
- With 3 servers, the ensemble survives 1 failure. With 5, it survives 2. That’s why odd numbers are used: a fourth server doesn’t let you survive any more failures than three.
- If a majority can’t be reached (for example, the network splits it in half), ZooKeeper stops accepting changes rather than risk two different truths. In CAP theorem terms, it chooses consistency over availability.
| Ensemble size | Majority needed | Failures it survives |
|---|---|---|
| 3 | 2 | 1 |
| 5 | 3 | 2 |
| 7 | 4 | 3 |
ZooKeeper today: Kafka, etcd and friends
ZooKeeper has been used by many well-known systems, including Hadoop, HBase, Solr and, for many years, Apache Kafka. Kafka has since moved to its own built-in coordination mode, called KRaft, and from Kafka 4.0 (released in 2025) it no longer uses ZooKeeper at all.
Other coordination stores do the same job with different designs:
- etcd: a key-value store using the Raft agreement protocol. It’s the brain of Kubernetes, holding the state of the whole cluster.
- Consul: combines coordination with service discovery and health checks.
The ideas you learn from ZooKeeper (ephemeral entries, watches, leader election, quorums) carry over directly to all of them.
Common misconceptions
“ZooKeeper is a database.” It stores small coordination data, not application data. Putting large or busy data in it slows down everything that depends on it.
“More ZooKeeper servers means faster.” More servers improve fault tolerance, but every write needs a majority to agree, so writes can get slower as the ensemble grows.
“Coordination services remove the need to think about failure.” They make agreement possible, but your app must still handle losing its lock or leadership at any moment.
Key takeaways
- ZooKeeper is a coordination service: a small, highly reliable shared store for facts servers must agree on.
- Data lives in znodes; ephemeral znodes vanish when their server’s session ends; watches notify on changes.
- It’s used for leader election, membership, configuration, locks and service discovery.
- It runs as an ensemble (3 or 5) and needs a majority to make changes.
- etcd (behind Kubernetes) and Consul fill the same role; Kafka now coordinates itself with KRaft.
Frequently asked questions
What is ZooKeeper in simple words?
ZooKeeper is a service that helps a group of servers agree on shared facts, such as which server is the leader, which servers are alive and what the current settings are.
What is ZooKeeper used for?
Leader election, keeping track of live servers, storing shared configuration, distributed locks and service discovery in distributed systems.
What is an ephemeral node in ZooKeeper?
An ephemeral node, or znode, exists only while the server that created it stays connected. If that server crashes or its session times out, ZooKeeper deletes the node automatically, which signals to others that it’s gone.
Why does ZooKeeper use an odd number of servers?
Because changes need a majority to agree. Three servers tolerate one failure, and adding a fourth still only tolerates one, so odd sizes like 3 or 5 give the best fault tolerance for the number of servers.
Does Kafka still need ZooKeeper?
No. Kafka replaced ZooKeeper with its own built-in KRaft mode, and since Kafka 4.0, ZooKeeper is no longer supported.
What is the difference between ZooKeeper and etcd?
Both are coordination stores that use majority agreement. ZooKeeper uses a tree of znodes and its own ZAB protocol; etcd is a key-value store built on the Raft protocol and is the main data store behind Kubernetes.
Comments