Brother even go language training blockchain consensus algorithm (1) Distributed consistency algorithm raft

Source: Internet
Author: User
Tags ack zookeeper
Brother even Go language training course System Design framework includes the basic language of the blockchain go language, blockchain backend technology system, blockchain public chain, blockchain distributed application development and other content, as well as to the final interview guidance and project combat. The course was carefully built by Tsinghua University's Microsoft Google Teacher team, which lasted half a year and developed together. # The distributed consistency algorithm Raftpaxos has become synonymous with distributed consistency algorithms for quite a long time since it was proposed in 1990. But because it is difficult to understand and realize, the current well-known implementation of only Chubby, Zookeeper, Libpaxos several, of which Zookeeper used Zab made a lot of improvements to Paxos. To this end, in 2013, Stanford's Diego Ongaro, John Ousterhout, proposed a new and easier to understand and implement the consistency algorithm, namely raft. Raft and Paxos are just to ensure that the n/2+1 node normal, can serve. Compared to Paxos, its advantage is easy to understand and realize. RAF decomposed the algorithm into: Select leader, log copy, security and several sub-issues. Its process is: at the beginning of the cluster in the election of leader responsible for the management of log replication, leader receive transaction requests from the client (log), and copy them to other nodes in the cluster, and then notify other nodes in the cluster to submit logs, Leader is responsible for ensuring that the other nodes and its log synchronization. When leader is down, the other nodes in the cluster re-sponsor the election and elect the new leader. # # # Role raft involves three roles: * Leader: The leader, who handles requests from clients, manages log replication, and maintains a heartbeat with follower to maintain its leadership position. * Follower: A follower who responds to a log copy request from leader in response to an election request from candidate. All nodes are follower at the beginning. * Candidate: The candidate, responsible for initiating an election vote, raft after the start or leader down, a node from follower to candidate, and the election, after the successful election, from candidate to leader. The following is the Raft role state transition diagram: # # # term (tenure) the concept of term (tenure) was used in raft, and the election was a term (tenure), and only one leader could be produced in a period. Term uses a sequential increment of numbers, and all follower are initially 1. One of the follower timer expires to trigger the election, its status is converted to candidate, when the term plus 1 becomes 2, and then start the election, there are several possible: * 1, if the current period of 2 of the terms of office did notThe election of leader or anomalies, the term is incremented by 3, and the beginning of a new round of elections. * 2, this round term for 2 of the tenure of the election after leader, if leader down, at this time the other follower to Candidate,term increment, and launch a new election. * 3, if leader or candidate found their term than the other follower hours, leader or candidate to follower,term increments. * 4, if follower found his term than other follower hours, update term and other follower consistent. Each term increment will take place a new round of elections, in the normal operation of raft, all nodes are consistent. If a node does not fail, a term (tenure) will persist until a node receives a request that has a period that is less than the current terms hour. # # # All nodes are follower at the beginning of the election, and the timer time is different. After a node timer triggers an election, the term increments, the node is converted from follower to candidate, and a poll request (Requestvote RPC) is initiated to the other nodes. There are several possible: * 1, received a majority of nodes (N/2+1) vote, from candidate to leader, send heartbeat to other nodes to maintain the leader status. * 2, if the Appendentries RPC request sent by another node is received, and the node is more than the current node term, that is, found a new effective leader, converted to follower, otherwise remain candidate deny the request. * 3. The election expires, the term increases, and the election is re-launched. Each of the term periods, each node can only vote 1 times, if more than a majority of candidate are not received by more than half of the vote, then each candidate period increments, restart the timer and re-launch the election. Because the timer time is random, there will not be multiple candidate at the same time to initiate the poll issue. # # # Log Replication guarantees the consistency of the nodes, and it is necessary to ensure that all nodes perform the same sequence of operations sequentially, which is the purpose of log replication. * 1, Leader received a client transaction request (that is, log), the log is appended to the local log, and through appendentries RPC replication to the other follower. * 2, follower received the log, append to the local log, and send an ACK message to the leader. * 3, leader received more than a majority of follower ACK message, the log is submitted and formally submit the log, notify the client, and send appendentries RPC pleaseRequest notification follower submit the log. # # # Security * 1, each term period can only elect a leader. * 2, leader will not delete or overwrite existing log entries, will only append. * 3. If the log entries at the same index position have the same term number, it is assumed that the index is the same from the beginning to the same location. * 4. If a log entry is submitted during a term, the log entry must appear in all leaders of the larger term number. * 5, if the log entry for leader at an index is committed, then the same index location of the other nodes does not commit different log entries. # # # Requestvote RPC and appendentries rpcraft node communication uses two types of RPC, namely Requestvote RPC and Appendentries rpc:requestvote RPC: That is, request a poll, Initiated by candidate during the election period. Appendentries RPC: The additional entry RPC, initiated by leader, is used for log replication and heartbeat mechanisms. # # # Reference Document * [Find an easy-to-understand consistency algorithm (extended version)] (HTTPS://GITHUB.COM/MAEMUAL/RAFT-ZH_CN/BLOB/MASTER/RAFT-ZH_CN.MD) * [Consistency algorithm raft detailed] ( http://www.solinx.co/archives/415?utm_source=tuicool&utm_medium=referral) * [Why Raft is a more understandable distributed consistency algorithm] (https:// www.cnblogs.com/mindwind/p/5231986.html) # # # PostScript This article summarizes the raft, and the previous article Paxos, 2PC, 3PC are based on non-Byzantine fault-tolerant distributed consistency algorithm, that is, in addition to consider the loss of messages, timeouts, disorderly sequence , but does not consider that the message was tampered with. From the next article, we will summarize the distributed consistency algorithm based on Byzantine fault tolerance, which is widely used in Bitcoin, Ethereum, and other blockchain products. 128 reads  ∙  1 likes  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.