Skip to main content
Lexicon
Lexicon

ZAB (ZooKeeper Atomic Broadcast Protocol)

Difficulty: Advanced
3 min read

In plain English

Plain definition

ZAB is the leader-based, crash-recovery atomic broadcast protocol that ZooKeeper uses internally to totally order and replicate every write across its ensemble.

ZAB is not a general-purpose consensus algorithm like Raft or Multi-Paxos in the academic sense; it was purpose-built for ZooKeeper’s specific workload: a single, primary-backup style replicated log where a distinguished leader assigns monotonically increasing zxid (ZooKeeper Transaction ID) values to every write. A zxid is a 64-bit value split into a high 32-bit epoch and a low 32-bit counter. The epoch increments on every leader election, and the counter resets to zero, giving every proposal a globally comparable, monotonically increasing identifier without a physical or hybrid clock.

The protocol operates in two interleaved phases across three conceptual modes: Discovery (leader election and epoch establishment via FLE, the Fast Leader Election algorithm), Synchronization (the new leader brings a quorum of followers up to date with its transaction history before accepting new writes), and Broadcast (steady-state two-phase commit-like propagation: leader sends PROPOSAL, followers persist to their local WAL and reply ACK, leader commits once a quorum acknowledges and broadcasts COMMIT). Like Raft, ZAB applies transactions in the order of the leader’s log; its distinguishing design point is that it explicitly guarantees primary order: if a leader broadcasts transaction T1 before T2, every server processes T1 before T2, even across leader changes.

  • Epoch-based recovery: On leader failure, FLE favours the server with the most up-to-date history (compared by epoch, then zxid, then server id), and the Synchronization phase then completes recovery so that committed transactions are preserved during failover.
  • Synchronization on recovery: After election, the new leader’s synchronization step directs each follower to truncate or apply proposals (DIFF, TRUNC or SNAP) so that its history matches the leader’s.
  • Read scalability tradeoff: Followers serve reads locally (possibly stale) unless a client issues a sync() call, which makes the connected server catch up with the leader before the next read, giving up-to-date reads at the cost of some latency. It is not a quorum read and does not strictly guarantee linearizable reads.
  • Single-writer bottleneck: All writes funnel through one leader, capping write throughput to a single node’s disk fsync rate, which is why ZooKeeper is explicitly positioned as a coordination substrate, not a general data store.

The most common operational failure mode is misunderstanding ZAB’s read consistency model: engineers assume ZooKeeper reads are always linearizable, but by default they are only guaranteed FIFO client order and eventual consistency across followers, which can produce stale-read bugs in leader-election or config-watch logic unless sync() is used deliberately. Another subtle edge case is the classic “zxid overflow” issue: when the 32-bit counter rolls over after about 2^32 writes, the leader triggers a new election. Operators should monitor election counts and leader changes, along with session expiry metrics.

Architecturally, ZAB’s design choices explain why systems like Kafka (pre-KRaft), Hadoop HDFS HA, and HBase historically depended on ZooKeeper purely for leader election and small, low-throughput metadata coordination rather than as a general consensus substrate for application data. Understanding ZAB’s primary-order guarantee and its distinction from majority-quorum consensus like Raft is essential when reasoning about failover latency, split-brain avoidance during network partitions, and why ZooKeeper ensembles are deliberately kept small (typically 3, 5, or 7 nodes) since every write must be durably persisted and acknowledged by a quorum before the client receives success.

About this page

Credited to

Dates

Published

Corrections

  • : Corrected during a review of published articles. Details

Was this useful?

Reports go to the site owner and are used to correct pages. Corrections are listed at /corrections.

What Is ZAB (ZooKeeper Atomic Broadcast Protocol)?