【Blockchain Technology 03】Consensus Mechanism: RAFT


Raft was originally a consensus algorithm for managing replicated logs. It is a protocol built for real-world applications, focusing on the implementation and understandability of the protocol.

Raft is a strong consensus protocol that achieves consensus under non-Byzantine faults.

Algorithm understanding

The core idea of ​​RAFT is easy to understand, roughly as follows:

If the initial states of multiple databases are consistent, as long as subsequent operations are consistent, subsequent data consistency can be guaranteed.

Process overview

In the blockchain system, the process of using the Raft algorithm to achieve consensus accounting is as follows: first, a leader is elected, and then the leader is given full power to manage accounting.

Leader receives accounting requests from clients, completes accounting operations, generates blocks, and copies them to other accounting nodes.

Having a leader simplifies the management of bookkeeping operations. The leader may fail or lose contact with other nodes. At this time, the system will elect a new leader.

problem decomposition

Raft decomposes the consensus problem into three relatively independent sub-problems:

1. Leader election

When the existing leader fails, a new leader must be elected.

2. Bookkeeping

Leader receives transaction records from clients, replicates them in nodes participating in consensus bookkeeping, and makes other bookkeeping nodes recognize the blocks corresponding to the exchange.

3. Safety

If an accounting node applies a particular block item to its state machine, other servers cannot apply different commands to the same block index.

RAFT Basics

RAFT divides the server into three roles: Leader , Follower and Candidate , and the three roles can be converted to each other.

A Raft cluster consists of at least 5 servers , allowing the system to have two failed servers , each in one of 3 roles .

  • Leader: Only one in normal operation; handles all client requests.

  • Follower: A passive node that responds to requests from the leader and candidate (if the client contacts the follower, the follower forwards the request to the leader).

  • Candidate: This state is used to elect the leader.

Operation process

1. Election of Leader

Follower automatically increments the current term, converts it to Candidate, votes for itself, and initiates RequestVoteRPC, waiting for the following three situations to occur;

  • a)  Get more than half of the votes of the servers, win the election, and become the leader

  • b)  Another server wins the election and receives the corresponding heartbeat to become a Follower

  • c)  The election times out, no server wins the election, the current term is incremented, and the election is re-initiated

2. Consensus bookkeeping

Leader accepts client requests, Leader updates logs, and sends Heartbeats to all Followers to synchronize logs.

All follers have ElectionTimeout. If the Headbeats from the leader are not received within the ElectionTimeout time, the leader is considered invalid and the leader is re-elected.

3. Troubleshooting

If a network communication failure occurs during this process, so that the current leader cannot access most of the followers, then the current leader can only update those follower servers that it can normally access.

Since most follower servers have no leader, they will re-elect a candidate as the leader, and then the new leader will deal with the outside world as a representative. If the outside world requires it to add a new transaction record, the new leader will be notified according to the above steps. Most followers.

If the network failure is restored at this time, the original leader becomes a follower. During the disconnection phase, any updates made by the old leader cannot be considered confirmations, and are rolled back to receive new updates from the new leader.

Security Guarantee

1.日志的流向只有Leader到Follower,并且Leader不能覆盖日志

2.日志不是最新者不能成为Candidate

RAFT应用

在超级账本的Fabric项目中,共识算法被设计成可插拔的模块,支持像PBFT、Raft等共识算法。


参考资料

动画:http://thesecretlivesofdata.com/raft/

官网:https://raft.github.io/

论文:https://raft.github.io/raft.pdf

本文作者:魏红心,链块学院执行院长,清华大学电子系博士

链块学院:专注于区块链技术研发与教育



--------------END--------------

本文完,获取更多资讯,敬请关注区块链工程师。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324424244&siteId=291194637