Technology Sharing

Zookeeper underlying principle

2024-07-08

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

ZooKeeper is a highly available distributed coordination service that is widely used in distributed systems to solve the consistency problem of distributed systems. The following will introduce the underlying principles of ZooKeeper in detail, including its architecture, data model, core mechanism, and consistency protocol.

1. Architecture

ZooKeeper adopts a master-slave architecture, which is usually composed of a Leader and several Followers, as well as Observer nodes as non-voting nodes to share the pressure of read requests.

  • Leader: Responsible for handling all write requests (transaction requests) and coordinating consistency protocols.
  • Follower: Participate in voting and sync the Leader status, and process read requests.
  • Observer: Does not participate in voting, only processes client read requests and forwards write requests.

2. Data Model

ZooKeeper stores data in a tree structure similar to a file system, and each node is called a znode.

  • znode: Data node, with path, data and status information.
  • Persistent znode: A node persists after a client disconnects unless it is explicitly deleted.
  • Ephemeral znode: The node is automatically deleted when the client disconnects.
  • Sequential znode: A node is automatically attached with an increasing sequence number when it is created.

3. Core Mechanics

3.1. Session Management

A session is established between each client and the ZooKeeper cluster to maintain the connection status and manage temporary nodes. ZooKeeper detects the client's connection status through a heartbeat mechanism. If no heartbeat is received within a certain period of time, the session is considered disconnected.

3.2. Data Synchronization

In order to ensure data consistency of each node, ZooKeeper adopts a data synchronization mechanism. When the Leader node processes a write request, it will send the change operation to all Follower nodes. After accepting the change operation, the Follower node confirms it. Ultimately, the Leader node decides whether the change operation is successful.

3.3. Notification Mechanism

ZooKeeper supports the Watch mechanism. The client can register a Watch on a specified znode. When the data or child nodes of the znode change, ZooKeeper will notify the client.

4. Consistency Protocol

ZooKeeper uses a consistency protocol called ZAB (Zookeeper Atomic Broadcast) to ensure data consistency. ZAB consists of two phases: election phase and broadcast phase.

4.1. Election Phase

When the ZooKeeper cluster is started, or when the leader node fails, an election is required to determine the new leader. The election phase is mainly divided into the following steps:

  1. vote: All nodes vote for themselves and send their votes to other nodes.
  2. Receive votes:Each node receives votes from other nodes and counts the voting results.
  3. Update Voting: If there is a candidate with higher votes than yourself among the received votes, update the vote to that candidate.
  4. Determine the Leader:When a candidate receives more than half of the votes, he or she becomes the new Leader.
4.2. Broadcasting Phase

After the leader is determined, the broadcast phase begins. The leader is responsible for processing the client's write request and broadcasting the change operation to all follower nodes. The specific steps are as follows:

  1. proposal:After receiving the write request, the Leader generates a proposal and sends it to all Follower nodes.
  2. confirm:After receiving the proposal, the Follower node records it in the log and sends a confirmation message to the Leader.
  3. submit:After the Leader node receives more than half of the confirmation messages, it submits the proposal and notifies all Follower nodes of the submission result.
  4. Apply changes:After receiving the submission notification, the Follower node applies the change operation.

5. Sample Code

The following is a simple example of using ZooKeeper, showing how to create a node, read node data, and register a Watch.

5.1. Introducing Dependencies

exist pom.xml Introduce ZooKeeper client dependency.

 

xmlCopy code