ZooKeeper

Summary: A distributed coordination and configuration service that uses a consensus algorithm (Zab) to provide linearizable operations and other primitives.

Sources: chapter9

Last updated: 2026-04-17


ZooKeeper (and its alternatives like etcd) is a “distributed key-value store” or “coordination and configuration service” (source: chapter9, p. 370). It is designed to hold small amounts of data that can fit in memory and provide strong consistency guarantees.

Key Features

ZooKeeper provides several primitives that are useful for building distributed systems:

  • Linearizable atomic operations: Using a consensus protocol, ZooKeeper ensures that operations like compare-and-set are linearizable (source: chapter9, p. 370).
  • Total ordering of operations: ZooKeeper totally orders all operations and gives each a monotonically increasing transaction ID (zxid) and version number (source: chapter9, p. 370).
  • Failure detection: Clients maintain a session with ZooKeeper; if the connection is lost, ZooKeeper can declare the session dead and trigger an action (source: chapter9, p. 371).
  • Change notifications (Watches): Clients can subscribe to changes on a key, avoiding the need for frequent polling (source: chapter9, p. 371).
  • Ephemeral nodes: Key-value pairs that automatically disappear when the client’s session ends (source: chapter9, p. 371).

Use Cases

  • Leader Election: Using ephemeral nodes and atomic operations to ensure only one node is the leader (source: chapter9, p. 371).
  • Allocating work to nodes: Deciding which partition to assign to which node (source: chapter9, p. 371).
  • Service Discovery: Finding the IP address of a service (source: chapter9, p. 372).
  • Membership Services: Keeping track of which nodes are currently active in a cluster (source: chapter9, p. 372).

Implementation

ZooKeeper uses the Zab (ZooKeeper Atomic Broadcast) consensus protocol to replicate its data across a cluster of nodes (source: chapter9, p. 366).