Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the modules of the consistency algorithm Raft

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "which modules is the consistency algorithm Raft divided into". In the daily operation, I believe many people have doubts about which modules the consistency algorithm Raft is divided into. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions of "which modules are divided into the consistency algorithm Raft". Next, please follow the editor to study!

One: consistency algorithm Raft

Raft algorithm: Raft is a consistent algorithm for managing replication logs. Raft provides the same function and performance as Paxos algorithm, but its algorithm structure is different from Paxos.

Raft breaks down the consistency algorithm into three modules:

Leader election

Log replication

Security.

Two stages: first, the election process, and then under the leadership of the elected leaders to carry out normal operations, such as log copying and so on.

1.1 leadership election

In Raft, a server can play one of the following roles at any time:

Leader (leader): handle client interaction, log replication, etc. Generally, there is only one leader at a time

Candidate: a candidate is the entity that nominates himself during the election process and becomes the leader once the election is successful

Follower (follower): a completely passive role like a voter, such a server waiting to be told to vote

Raft uses a heartbeat mechanism to trigger elections. When server starts, the initial state is follower. Each server has a timer with a timeout of election timeout (usually 150-300ms). If a server receives any message from a leader or candidate without a timeout, the timer restarts, and if it times out, it starts an election.

In the initial state, all nodes in the cluster are in the follower state.

At some point, one of the follower initiated an election because it did not receive the heartbeat of the leader to take the lead in election timeout.

As long as more than half of the nodes in the cluster accept the vote, the candidate node will switch to the leader state.

After becoming a leader node, leader will periodically synchronize logs and send heartbeat to the follower node.

1.2 Node exceptions that may be encountered 1) leader is not available

In general, leader nodes regularly send heartbeat to follower nodes. Due to some exceptions, leader no longer sends heartbeat, or follower cannot receive heartbeat.

When election timeout (timeout mechanism, during which no heartbeats are received) occurs in a follower, its status changes to candidate and votes are initiated to other follower.

When more than half of the follower accepts the vote, this node becomes the step number of the new leader,leader plus 1 and starts synchronizing the log to the follower.

After a period of time, if the previous leader joins the cluster again:

The two leader compare each other's steps, and the leader with a low number of steps will switch its state to follower.

Inconsistent logs in the earlier leader will be cleared and consistent with the logs in the existing leader.

2) follower node is not available

The situation where the follower node is not available is relatively easy to resolve. Because the log content in the cluster is always synchronized from the leader node, as long as the node rejoins the cluster, you can re-copy the log from the leader node.

3) multiple candidate

Multiple candidate are more likely to appear in the "chaotic" period when the leader has not been selected at the initial stage of cluster node startup.

In the initial state, all the nodes in the cluster are in the follower state, and at this time both nodes become candidate to initiate the election.

Both candidate received only a small number of follower acceptance votes.

Candidate continues to ask other follower, because some follower have already voted, so they all return to reject.

Candidate may also ask another candidate for a vote, and candidate will refuse to accept another candidate's request with the same number of steps.

Because the leader,candidate is not selected for the first time, it will randomly choose a waiting interval (150ms ~ 300ms) to initiate voting again. If accepted by more than half of the follower in the cluster, this candidate will become a leader.

Later, another candidate will also initiate a vote again, because the leader,candidate has been selected in the cluster will receive a rejected vote.

After being rejected by most nodes and knowing that leader already exists in the cluster, the candidate node terminates the voting request, switches to follower, and synchronizes logs from the leader node.

1.3 Log replication (ensuring data consistency)

The process of log replication

After the Leader is selected, it starts to receive requests from the client. Leader adds the request to its log as a log entry (Log entries) and then initiates AppendEntries RPC replication log entries to other servers in parallel. When the log is copied to most servers, Leader applies the log to its state machine and returns the execution result to the client.

The following figure shows the following process when a client sends a request to a leader, and then the leader copies it to a follower:

Each request from the client contains instructions executed by the replicated state machine.

Leader adds this directive to the log as a new log entry, and then initiates RPC to other servers in parallel, asking them to copy this information.

The follower responds to the ACK. If the follower goes down or runs slowly or the packet is lost, the leader will retry again and again until all the follower finally copies all the log entries.

Notify all Follower to submit the log, while the leader submits the log to their own state machine and returns it to the client.

As you can see, the whole transaction will not be completed until the fourth step. Failure of any of the intermediate steps will not affect log consistency.

At this point, on the "consistency algorithm Raft divided into which modules" study is over, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report