Apache ZooKeeper Deep Dive (Consensus & Coordination)
Apache ZooKeeper Deep Dive (Consensus & Coordination)
ZooKeeper provides centralized coordination services for large distributed systems (Kafka, Hadoop, HBase), managing leader elections, service discovery registries, and distributed locks.
1. Data Model & ZNode Types
ZooKeeper structures data like an in-memory hierarchical Unix filesystem:
- Persistent ZNodes: Remain stored until explicitly deleted.
- Ephemeral ZNodes: Automatically deleted by the cluster when the client session disconnects or heartbeats time out. Perfect for leader election and service presence.
- Sequential ZNodes: Append a monotonically increasing 10-digit number to the path (e.g.
/lock/node-0000000001). Used for FIFO distributed locking.
2. The ZAB Protocol (ZooKeeper Atomic Broadcast)
ZooKeeper runs the proprietary ZAB consensus protocol:
- Leader Election Phase: Nodes discover each other and elect the candidate with the highest epoch and transaction ID ($zxid$).
- Atomic Broadcast Phase: All state updates flow through the leader, which proposes transactions to follower nodes via two-phase commit over TCP.
- Quorum Rule: Writes succeed once a simple majority of nodes ($Q = \lfloor N/2 \rfloor + 1$) acknowledge the write to disk.
Key Architecture Trade-Offs & Decision Matrix
| Dimension | Option A: Asynchronous / Decoupled | Option B: Synchronous RPC | Trade-off & Recommendation |
|---|---|---|---|
| Communication Pattern | Message Queues (Kafka / SQS) | Direct gRPC / REST Calls | Message queues prevent cascading failures and provide backpressure buffering. |
| Failure Handling | Dead Letter Queue (DLQ) with Exponential Backoff | Immediate Circuit Breaker trip | DLQ preserves unparseable messages for manual operational inspection. |
| Ordering Guarantees | Partition-level Strict FIFO | Unordered Parallel Execution | Partition-key routing guarantees chronological order per entity without global lock contention. |
Related Topics
Expand your knowledge by learning about adjacent concepts in system design.
Event-Driven Architecture & Scaling with Apache Kafka
Design resilient, event-driven distributed architectures using Apache Kafka: topic partitioning, consumer group rebalancing, idempotence, and exactly-once semantics.
Kafka
Learn about how you can use Kafka to solve a large number of problems in System Design.
API Gateway
Master API Gateway architecture for distributed systems & FAANG interviews: Reverse Proxy vs Gateway vs Service Mesh, dynamic routing, edge security, JWT validation, distributed rate limiting, circuit breakers, and BFF patterns.
Cheat Sheet Utility
View and print a concise system design reference card.