Lesson 25 / 33
ZooKeeper and the Zab Protocol
Describe ZooKeeper's data model and guarantees and how Zab compares with Raft.
By Rahul Agarwal · 15 years in web development
Coordination as a file tree
Apache ZooKeeper is a coordination service used by systems such as older Kafka versions, HBase and many Hadoop-era tools. It stores small data nodes called znodes in a tree like a file system. A znode can be ephemeral (deleted when the client's session ends) and/or sequential (ZooKeeper appends a monotonically increasing counter to its name). Clients can set watches to be notified of changes; classic watches fire once and must be re-registered (persistent watches were added in ZooKeeper 3.6). ZooKeeper replicates with Zab (ZooKeeper Atomic Broadcast), a leader-based protocol in the same family as Raft and Multi-Paxos: a leader orders all writes, identified by a zxid that combines an epoch (like a Raft term) and a counter, and commits them once a quorum acknowledges. Its guarantees are linearizable writes and FIFO order per client; ordinary reads are served by whichever server the client is connected to and may be slightly stale, unless the client first issues a sync.
Leader election recipe with ephemeral sequential znodes
Each candidate creates an ephemeral sequential child; the lowest sequence number is the leader. Each other candidate watches only the node just before its own to avoid a 'herd' of notifications.
# In zkCli.sh
create /election ""
create -e -s /election/n_ "node1" # -> /election/n_0000000000
create -e -s /election/n_ "node2" # -> /election/n_0000000001
ls /election
stat -w /election/n_0000000000 # node2 sets a watch on its predecessor
# node2 now waits; when node1's session dies, that
# ephemeral znode is deleted and node2 becomes the lowest, so it leads.The queue at a ticket counter
Everyone takes a numbered slip (sequential znode). The lowest number is served (leader). You only watch the person directly ahead of you; when they leave the queue (session ends, ephemeral znode deleted), you know it is your turn.
Quick check: What happens to an ephemeral znode when the client session that created it expires?
- It is deleted automatically
- It becomes persistent
- It is moved to the end of the sequence
- It is kept but marked read-only
Answer
It is deleted automatically — Ephemeral znodes live only as long as their session, which is what makes them useful for liveness-based recipes like locks and leader election.