ZooKeeper: Comprehensive Guide to Distributed Coordination Service
ZooKeeper, Apache’s distributed coordination service, offers a highly available in‑memory hierarchical file system with leader‑follower‑observer clustering and the ZAB protocol, guaranteeing sequential consistency, atomicity and a single view while supporting publish/subscribe, configuration management, distributed locks, master election and queueing for robust distributed applications.
ZooKeeper is a distributed coordination service maintained by Apache, functioning as a highly available file system for distributed applications. It provides essential features including publish/subscribe, load balancing, command service, distributed coordination/notification, cluster management, Master election, distributed locks, and distributed queues.
Core Concepts: ZooKeeper's data model is a tree-structured file system where nodes are called znodes. The root node is /, and each znode stores its own data and node information. Znodes have a maximum data size limit of 1MB and support two types: ephemeral (deleted when client session ends) and persistent (remains until explicitly deleted). ZooKeeper implements a Leader-Follower-Observer cluster architecture where all write operations must go through the Leader, while read operations can be handled by any server directly from local memory.
Key Features: ZooKeeper guarantees sequential consistency (transactions are applied in the order they are submitted), atomicity (all transaction results are consistently applied across the cluster), single view (clients see the same data model regardless of which server they connect to), high performance (data is stored in memory), and high availability (replication mechanism with fault recovery).
ZAB Protocol: ZooKeeper uses ZAB (ZooKeeper Atomic Broadcast) instead of Paxos. ZAB defines two infinite loops: Leader Election (for fault recovery) and Atomic Broadcast (for master-slave synchronization). The Leader election uses a majority voting mechanism, requiring an odd number of cluster nodes (N+1 alive nodes minimum).
Applications: ZooKeeper is widely used for naming services (generating globally unique IDs), configuration management (using Watch mechanism for dynamic updates), distributed locks (using temporary nodes and Watcher), cluster management (heartbeat detection via ephemeral nodes), Master election, and queue management (synchronous and FIFO queues).
Code Example - Watch Trigger:
Set<Watcher> triggerWatch(String path, EventType type, Set<Watcher> supress) {
WatchedEvent e = new WatchedEvent(type, KeeperState.SyncConnected, path);
Set<Watcher> watchers;
synchronized (this) {
watchers = watchTable.remove(path);
}
for (Watcher w : watchers) {
w.process(e);
}
return
vivo Internet Technology
Sharing practical vivo Internet technology insights and salon events, plus the latest industry news and hot conferences.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.