ZooKeeper Explained: How Distributed Systems Stay Coordinated

ZooKeeper explained in plain terms means understanding how a group of computers can agree on who is in charge, what the current settings are, and which machines are still alive. The central question is how a distributed system, made of many separate servers that can fail independently, can still act as one coherent whole, and the answer is a shared coordination service that every part of the system trusts.

Apache ZooKeeper is a centralised coordination service for distributed applications, providing a small, reliable store of shared data that services use to agree on configuration, leadership, and membership. It is not a database for application data, but a place where the facts that the whole system must agree on are kept, in a form that every service can read and watch for changes.

The idea is simple: rather than each service keeping its own copy of the truth and drifting apart, all of them look to one service. When a leader fails, when a setting changes, or when a new machine joins, ZooKeeper is where the change is recorded, and every interested service is told at once. This article explains what ZooKeeper is, how it works, and where it is used.

Key takeaways

  • Apache ZooKeeper is a coordination service for distributed systems.
  • It stores shared data as znodes in a small, tree-shaped hierarchy.
  • It uses the Zab protocol to keep replicas in agreement.
  • Services use it for leader election, configuration, and membership.
  • Kafka, HBase, and Solr are among its best-known users.

What ZooKeeper is

ZooKeeper is a service that runs as a small cluster of servers, called an ensemble, and presents a simple, shared data store to its clients. Clients connect to the ensemble, read and write small pieces of data, and set watches that notify them when a piece of data changes, so that they can react immediately without constantly polling.

ZooKeeper began at Yahoo, as part of the Hadoop ecosystem, and later became an Apache top-level project. It was built to solve a recurring problem: every distributed system needs the same handful of coordination tasks, such as choosing a leader and tracking which machines are alive, and building those correctly from scratch is difficult and error-prone.

ZooKeeper is therefore a shared building block, used by many systems rather than built into each one. A service that needs coordination can rely on ZooKeeper instead of writing its own consensus logic, and the same ensemble can serve several applications at once. Our guide to Kafka explains the messaging system that has long relied on ZooKeeper, and our guide to etcd explains a newer coordination store that is often compared with it.

How ZooKeeper works

The data in ZooKeeper is organised as znodes, arranged in a tree much like a file system, with paths such as a root, a folder, and a leaf. Each znode holds a small piece of data, usually no more than a megabyte, together with metadata such as a version number, and clients read and write znodes by path, creating and deleting them as the state of the system changes.

Some znodes are ephemeral, meaning they exist only while the client that created them stays connected. When that client disconnects, whether deliberately or because it has crashed, the ephemeral znode is deleted automatically, which is how other services learn that a machine has gone away, without anyone having to check on it directly.

Other znodes are sequential, meaning ZooKeeper appends a number to their name as they are created. A group of services that want to elect a leader can each create a sequential ephemeral znode under the same parent, and the one with the lowest number becomes the leader, while the others watch the znode ahead of them and take over if it disappears.

Agreement across the ensemble is kept by Zab, the atomic broadcast protocol that ZooKeeper uses to replicate writes. One server in the ensemble is the leader, and it proposes each write to the others, which acknowledge it before it is committed, so that a majority of servers always agree on the order of changes, and a failed leader can be replaced without losing committed data.

Watches are how clients learn about change without polling. A client can set a watch on a znode and be notified once, when the data changes, a child is added or removed, or the znode is deleted. The client then reads the new state and sets a fresh watch, which keeps services up to date with configuration changes and membership events as they happen.

A dark server room with glowing green lights on racks of machines, one rack highlighted as the leader among the rest
A dark server room with glowing green lights on racks of machines, one rack highlighted as the leader, shows how ZooKeeper works: one shared source of agreement for the whole cluster.

Comparing ZooKeeper with other stores

ZooKeeper is one of several stores used for coordination and shared state, and each was built with a different emphasis. Etcd is its closest modern rival, while Consul adds service discovery, and a conventional database can hold configuration but lacks the same guarantees about ordering and watches.

The table below sets out the main contrasts between the leading approaches.

Aspect ZooKeeper etcd Consul
Main use Coordination Coordination Discovery and config
Data model Tree of znodes Key-value pairs Key-value pairs
Consensus Zab protocol Raft protocol Raft protocol
Change alerts One-shot watches Long-lived watches Blocking queries
Best for Hadoop-era systems Kubernetes clusters Service discovery

The practical result is that ZooKeeper remains the right choice for the systems built around it, etcd is the usual choice for newer cloud-native work, and Consul suits teams that want discovery and configuration in a single service.

Where ZooKeeper is used

Kafka is the best-known user of ZooKeeper. For many years Kafka stored its cluster metadata in ZooKeeper, including which brokers were alive, which broker led each partition, and the current configuration, and although newer Kafka versions can run without it, a large number of existing clusters still depend on it.

HBase, the distributed database built on Hadoop, uses ZooKeeper to track the state of its region servers and to elect a master. When a region server fails, its ephemeral znode disappears, the master notices at once, and the regions it was serving are reassigned to servers that are still alive.

Solr, the search platform, uses ZooKeeper to hold the shared configuration of a search cluster. The schema, the collection definitions, and the state of each shard are stored as znodes, so that every node in the cluster works from the same configuration, and a change made once is visible everywhere.

Configuration management is a use in its own right. Many teams store feature flags, connection details, and service settings in ZooKeeper, so that a change can be made in one place and every service that watches the relevant znode picks it up immediately, without a restart or a redeploy.

Leader election and distributed locking are the other common uses. Any system that must have exactly one active writer, one scheduler, or one coordinator can use ZooKeeper to choose that leader, and to make sure that a replacement is chosen cleanly if the leader fails.

Strengths and limits

The greatest strength of ZooKeeper is that it is simple, well tested, and hard to get wrong in use. Its data model is small, its guarantees about ordering and consistency are clear, and it has been running in production in some of the largest systems in the world for well over a decade.

It is also a strength that ZooKeeper is a shared service, not a library. Several applications can use the same ensemble, the coordination logic is maintained in one place, and teams do not have to build, test, and maintain their own consensus code, which is one of the easiest parts of a distributed system to get wrong.

The limits are real, though. ZooKeeper is not a general-purpose database: znodes are meant to be small, writes go through a single leader, and very large numbers of watches can strain the ensemble. For newer cloud-native work, many teams now choose etcd instead, and Kafka itself is moving away from ZooKeeper in its latest versions.

ZooKeeper checklist

  • Run an odd number of servers, so a majority can always be formed.
  • Keep znodes small, and store large data somewhere else.
  • Use ephemeral znodes to track which machines are alive.
  • Use sequential znodes for leader election and fair locking.
  • Handle a watch firing once, then set a fresh watch.
  • Check the version number before writing, to avoid lost updates.
  • Watch ensemble health, and alert on a lost quorum.
  • Back up the data directory, and test restoring it.
  • Plan for the client session expiring, not only for a clean disconnect.

Frequently asked questions

What is ZooKeeper in simple terms?

It is a shared coordination service that distributed systems use to agree on the facts they must share, such as who the leader is, what the configuration is, and which machines are still alive, keeping that shared state consistent across the whole system.

What is a znode?

It is a node in ZooKeeper’s tree-shaped data store, identified by a path like a file, holding a small piece of data and metadata such as a version number. Znodes can be persistent, or ephemeral, meaning they disappear when the client that created them disconnects.

What is the Zab protocol?

It is the atomic broadcast protocol ZooKeeper uses to keep its servers in agreement. A leader proposes each write, the other servers acknowledge it, and the write is committed once a majority agrees, so that committed data is never lost when a leader fails.

What is a watch in ZooKeeper?

It is a one-time notification a client sets on a znode, which fires when the data changes, a child is added or removed, or the znode is deleted. The client then reads the new state and sets a fresh watch, which is how services learn about changes without polling.

How does ZooKeeper elect a leader?

Each candidate creates a sequential ephemeral znode under a shared parent, and the candidate with the lowest number becomes the leader. The others watch the znode ahead of them, so if the leader’s znode disappears, the next candidate takes over.

When should I not use ZooKeeper?

When the data is large, when writes are heavy, or when the system is new and cloud-native, where etcd may be a better fit. ZooKeeper is a coordination store, not a database, and it works best for small, shared state that many services must agree on.

The bottom line

ZooKeeper explained in one line is this: it is the small, trusted store where a distributed system keeps the facts it must agree on, and where it learns at once when those facts change.

It is the reason that systems such as Kafka, HBase, and Solr can run across many machines and still behave as one, with a single leader, a single configuration, and a clear view of who is still alive.

This article is general information about how ZooKeeper works, not advice about choosing a coordination service for a particular system.

Latest articles

Scroll to Top