Answer
Zookeeper is an open-source distributed coordination service maintained by the Apache Foundation, primarily used to solve coordination problems in distributed applications.
Core Features
- Consistency Guarantee: Zookeeper provides strong consistency guarantees, ensuring all clients see the same data view
- Reliability: High availability and durability guaranteed through the ZAB protocol
- Simplicity: Provides a hierarchical namespace similar to file systems, easy to understand and use
- High Performance: Excellent read operation performance, suitable for read-heavy scenarios
Data Model
Zookeeper uses a tree-like structure similar to file systems to store data:
- ZNode: Data node in Zookeeper, each node is called a ZNode
- Path: Path identifier using slash-separated paths, such as
/app/config - Data: Each ZNode can store a small amount of data (typically not exceeding 1MB)
- Version: Each node maintains multiple version numbers (dataVersion, cversion, aversion)
ZNode Types
- Persistent Node: Node persists after creation until explicitly deleted
- Ephemeral Node: Bound to client session, automatically deleted when session ends
- Persistent Sequential Node: Automatically adds sequence suffix on top of persistent node
- Ephemeral Sequential Node: Automatically adds sequence suffix on top of ephemeral node
Application Scenarios
- Configuration Center: Centralized application configuration management with dynamic updates
- Service Registration and Discovery: Service governance in microservice architecture
- Distributed Lock: Mutual access control across processes
- Distributed Coordination: Leader election, Barrier, and other coordination mechanisms
- Naming Service: Unique identifier generation in distributed environments
Working Principle
Zookeeper cluster consists of multiple Servers, typically using odd numbers (3, 5, 7, etc.):
- Leader: Handles write requests, coordinates data consistency
- Follower: Handles read requests, participates in Leader election
- Observer: Only handles read requests, does not participate in election (improves read performance)
Clients connect to any Server, communicate through TCP long connections, and support Watcher mechanism for event notifications.