Table of Contents
1. Overview of Raft" >1. Overview of Raft
2.1 Role" >2.1 Role
3.1 Database server" >3.1 Database server
Database Server" >Now let’s imagine, There is a single-node system. This node serves as a database server and stores a value of X. Database Server
4. Leadership election process" >4. Leadership election process
4.1 Initial state" >4.1 Initial state
4.2 Become a candidate" >4.2 Become a candidate
4.3 Vote" >4.3 Vote
4.4 Term " >4.4 Term
4.5 Election Rules" >4.5 Election Rules
4.6 Most" >4.6 Most
4.7 Heartbeat Timeout" >4.7 Heartbeat Timeout
5. Leader failure" >5. Leader failure
Summary" >Summary
Home Technology peripherals AI A consensus algorithm that distributed systems must know: Raft

A consensus algorithm that distributed systems must know: Raft

Apr 07, 2023 pm 05:54 PM
algorithm distributed

1. Overview of Raft

​Raft algorithm​​​is the first choice for distributed system development​ Consensus algorithm​​. For example, Etcd and Consul are popular now.

If you master this algorithm, you can easily handle the fault tolerance and of most scenarios. #​Consistency​​Requirements. For example, distributed configuration systems, distributed NoSQL storage, etc. can easily break through the single-machine limitations of the system. The Raft algorithm achieves consensus on a series of values ​​and consistency of logs of each node through all methods based on the leader.

2. Raft Role

2.1 Role

Follower :​

​Ordinary people​

​, silently receive messages from the leader. When the leader’s heartbeat message times out, they will take the initiative to stand up and recommend themselves as candidates. Candidate (Candidate):​

​Candidate​

​will request voting RPC messages from other nodes to notify other nodes to vote if they win the majority Vote and be promoted to leader. Leader:​

​Overbearing President​

​, everything is subject to me. Process write requests, manage log replication, and continuously send heartbeat information to inform other nodes that "I am the leader, I am still alive, and you don't want to" initiate a new election without having to find a new leader to replace me. As shown in the figure below, three types of figures are used to represent followers, candidates and leaders. Role

3. Single-node system

3.1 Database server

Now let’s imagine, There is a single-node system. This node serves as a database server and stores a value of X. Database Server

3.2 Client

The solid green circle on the left is the client, and the solid blue circle on the right is Node a (Node a ). Term represents the term of office, which will be discussed later.

Client

A consensus algorithm that distributed systems must know: Raft

3.3 The client sends data to the server

The client sends data to the single-node server An update operation sets the value stored in the database to 8. In a stand-alone environment (single server node), the value the client gets from the server is also 8. Consistency is very easy to ensure.

The client sends data to the server

A consensus algorithm that distributed systems must know: Raft

3.4 How to ensure consistency among multiple nodes?

But if there are multiple server nodes, how to ensure consistency? For example, there are three nodes: a, b, c. As shown below. These three nodes form a database cluster. When the client performs update operations on these three nodes, how to ensure that the values ​​stored in the three nodes are consistent? This is a distributed consistency issue. The Raft algorithm is here to solve this problem. Of course, there are other protocols that can also guarantee this. This article only focuses on the Raft algorithm.

A consensus algorithm that distributed systems must know: Raft

In a multi-node cluster, how does the Raft algorithm ensure that there is only one leader in the cluster at the same time under abnormal circumstances such as node failure or partition error? Let’s begin to explain the process of leader election by the Raft algorithm.

4. Leadership election process

4.1 Initial state

In the initial state, all nodes in the cluster are followers status.

As shown in the figure below, there are three nodes (Node) a, b, and c, and the term (Term) is 0.

A consensus algorithm that distributed systems must know: Raft

Initial state

4.2 Become a candidate

Raft algorithm implements the characteristics of random timeout, each time The timeout interval for each node to wait for heartbeat information from the leader node is random. For example, the waiting timeout interval of node A is 150 ms, that of node B is 200 ms, and that of node C is 300 ms. Then a times out first. First, it times out because it does not wait for the leader's heartbeat information. As shown in the figure below, the timeout timers for the three nodes start running.

Timeout time

When the timeout time of node A expires, node A becomes a candidate and increases its term number. The Term value is updated from 0 to 1. And cast a vote for myself.

  • Node A: Term = 1, Vote Count = 1.
  • Node B: Term = 0.
  • Node C: Term = 0.

A consensus algorithm that distributed systems must know: Raft

Become a candidate

4.3 Vote

Let’s see how a candidate can become a leader of.

A consensus algorithm that distributed systems must know: Raft

Leader election

  • Step 1: After node A becomes a candidate, send a request to vote RPC to other nodes message and ask them to elect themselves as leaders.
  • Step 2: After node B and node C receive the voting request information sent by node A, they will vote without voting during the term numbered 1. The vote is cast to node A and its term number is increased.
  • The third step: Node A received 3 votes, got the votes of the majority of nodes, and became the new leader during this term from the candidate.
  • Step 4: Node A, as the leader, sends heartbeat information to node B and node C at fixed intervals, telling node B and C that I am the leader and organizes others to follow. to initiate a new election.
  • Step 5: Node B and node C send response information to node A, telling node A that I am normal.

4.4 Term

The English word is term, and leaders have terms of office.

  • Automatic increase: After the follower times out waiting for the leader's heartbeat information, it recommends itself as a candidate and increases its term number. As shown in the figure above, the term of node A is 0. When nominating yourself as a candidate, the term number increases to 1.
  • Update to a larger value: When a node finds that its term number is smaller than that of other nodes, it will update to a larger number value. For example, node A's term is 1 and requests a vote. The voting message contains node A's term number, and the number is 1. After node B receives the message, it will update its term number to 1.
  • Revert to follower: If a candidate or leader finds that its term number is smaller than that of other nodes, it will immediately revert to follower status. This scenario occurs after partition error recovery. If the leader with term 3 receives a heartbeat message with term 4, the leader will immediately return to the follower state.
  • Rejection message: If a node receives a request for a smaller term number value, it will directly reject the request. For example, node A with term number 6 receives the term number. For node B's request to vote for 5 RPC messages, node A will reject the message.

4.5 Election Rules

  • During a term, the leader will always be the leader until there is a problem (such as downtime) or the network problem (delay), other nodes initiate a new round of elections.
  • In an election, each server node will cast at most one vote for a term number, and it will be gone after the vote is completed.

4.6 Most

Assuming a cluster consists of N nodes, then the majority is at least N/2 1. For example: for a cluster of 3 nodes, most are 2.

4.7 Heartbeat Timeout

In order to prevent multiple nodes from initiating voting at the same time, each node will be assigned a random election timeout. During this time, the node cannot become a candidate and can only wait until timeout. For example, in the above example, node A times out first and becomes a candidate first. With this clever design, in most cases only one server node initiates the election first instead of initiating the election at the same time, which reduces the number of election failures due to vote division.

A consensus algorithm that distributed systems must know: Raft

Become a candidate

5. Leader failure

If the leader node fails, it will Trigger a new round of elections. As shown in the figure below, if leader node A fails, node B and node C will re-elect the leader.

A consensus algorithm that distributed systems must know: Raft

Leader Failure

  • Step 1 : Node A failure , Node B And node C does not receive the heartbeat information from the leader node A, and the wait times out.
  • Second step: Node C times out first, and node C becomes the candidate.
  • Step 3: Node C initiates a request for voting information to node A and node B.
  • Step 4: Node C responded to the vote and voted for C, but node A was unable to respond to C’s voting request because of a failure.
  • Step 5: Node C receives two votes (majority of votes) and becomes the leader.
  • Step 6: Node C sends heartbeat information to nodes A and B. Node B responds to the heartbeat information. Node A does not respond to the heartbeat information because A is faulty.

Summary

The Raft algorithm uses the following methods to conduct leadership elections, ensuring that there is only one leader in one term, greatly reducing the chance of election failure. Condition.

  • Term
  • Leader heartbeat information
  • Random election timeout
  • First come, first served voting principle
  • Big Majority Vote Principle

This article uses animated graphics to explain how the Raft algorithm elects leaders, making it easier to understand and digest.

The above is the detailed content of A consensus algorithm that distributed systems must know: Raft. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CLIP-BEVFormer: Explicitly supervise the BEVFormer structure to improve long-tail detection performance CLIP-BEVFormer: Explicitly supervise the BEVFormer structure to improve long-tail detection performance Mar 26, 2024 pm 12:41 PM

Written above & the author’s personal understanding: At present, in the entire autonomous driving system, the perception module plays a vital role. The autonomous vehicle driving on the road can only obtain accurate perception results through the perception module. The downstream regulation and control module in the autonomous driving system makes timely and correct judgments and behavioral decisions. Currently, cars with autonomous driving functions are usually equipped with a variety of data information sensors including surround-view camera sensors, lidar sensors, and millimeter-wave radar sensors to collect information in different modalities to achieve accurate perception tasks. The BEV perception algorithm based on pure vision is favored by the industry because of its low hardware cost and easy deployment, and its output results can be easily applied to various downstream tasks.

Implementing Machine Learning Algorithms in C++: Common Challenges and Solutions Implementing Machine Learning Algorithms in C++: Common Challenges and Solutions Jun 03, 2024 pm 01:25 PM

Common challenges faced by machine learning algorithms in C++ include memory management, multi-threading, performance optimization, and maintainability. Solutions include using smart pointers, modern threading libraries, SIMD instructions and third-party libraries, as well as following coding style guidelines and using automation tools. Practical cases show how to use the Eigen library to implement linear regression algorithms, effectively manage memory and use high-performance matrix operations.

Explore the underlying principles and algorithm selection of the C++sort function Explore the underlying principles and algorithm selection of the C++sort function Apr 02, 2024 pm 05:36 PM

The bottom layer of the C++sort function uses merge sort, its complexity is O(nlogn), and provides different sorting algorithm choices, including quick sort, heap sort and stable sort.

Can artificial intelligence predict crime? Explore CrimeGPT's capabilities Can artificial intelligence predict crime? Explore CrimeGPT's capabilities Mar 22, 2024 pm 10:10 PM

The convergence of artificial intelligence (AI) and law enforcement opens up new possibilities for crime prevention and detection. The predictive capabilities of artificial intelligence are widely used in systems such as CrimeGPT (Crime Prediction Technology) to predict criminal activities. This article explores the potential of artificial intelligence in crime prediction, its current applications, the challenges it faces, and the possible ethical implications of the technology. Artificial Intelligence and Crime Prediction: The Basics CrimeGPT uses machine learning algorithms to analyze large data sets, identifying patterns that can predict where and when crimes are likely to occur. These data sets include historical crime statistics, demographic information, economic indicators, weather patterns, and more. By identifying trends that human analysts might miss, artificial intelligence can empower law enforcement agencies

Improved detection algorithm: for target detection in high-resolution optical remote sensing images Improved detection algorithm: for target detection in high-resolution optical remote sensing images Jun 06, 2024 pm 12:33 PM

01 Outlook Summary Currently, it is difficult to achieve an appropriate balance between detection efficiency and detection results. We have developed an enhanced YOLOv5 algorithm for target detection in high-resolution optical remote sensing images, using multi-layer feature pyramids, multi-detection head strategies and hybrid attention modules to improve the effect of the target detection network in optical remote sensing images. According to the SIMD data set, the mAP of the new algorithm is 2.2% better than YOLOv5 and 8.48% better than YOLOX, achieving a better balance between detection results and speed. 02 Background & Motivation With the rapid development of remote sensing technology, high-resolution optical remote sensing images have been used to describe many objects on the earth’s surface, including aircraft, cars, buildings, etc. Object detection in the interpretation of remote sensing images

Application of algorithms in the construction of 58 portrait platform Application of algorithms in the construction of 58 portrait platform May 09, 2024 am 09:01 AM

1. Background of the Construction of 58 Portraits Platform First of all, I would like to share with you the background of the construction of the 58 Portrait Platform. 1. The traditional thinking of the traditional profiling platform is no longer enough. Building a user profiling platform relies on data warehouse modeling capabilities to integrate data from multiple business lines to build accurate user portraits; it also requires data mining to understand user behavior, interests and needs, and provide algorithms. side capabilities; finally, it also needs to have data platform capabilities to efficiently store, query and share user profile data and provide profile services. The main difference between a self-built business profiling platform and a middle-office profiling platform is that the self-built profiling platform serves a single business line and can be customized on demand; the mid-office platform serves multiple business lines, has complex modeling, and provides more general capabilities. 2.58 User portraits of the background of Zhongtai portrait construction

Add SOTA in real time and skyrocket! FastOcc: Faster inference and deployment-friendly Occ algorithm is here! Add SOTA in real time and skyrocket! FastOcc: Faster inference and deployment-friendly Occ algorithm is here! Mar 14, 2024 pm 11:50 PM

Written above & The author’s personal understanding is that in the autonomous driving system, the perception task is a crucial component of the entire autonomous driving system. The main goal of the perception task is to enable autonomous vehicles to understand and perceive surrounding environmental elements, such as vehicles driving on the road, pedestrians on the roadside, obstacles encountered during driving, traffic signs on the road, etc., thereby helping downstream modules Make correct and reasonable decisions and actions. A vehicle with self-driving capabilities is usually equipped with different types of information collection sensors, such as surround-view camera sensors, lidar sensors, millimeter-wave radar sensors, etc., to ensure that the self-driving vehicle can accurately perceive and understand surrounding environment elements. , enabling autonomous vehicles to make correct decisions during autonomous driving. Head

News recommendation algorithm based on global graph enhancement News recommendation algorithm based on global graph enhancement Apr 08, 2024 pm 09:16 PM

Author | Reviewed by Wang Hao | Chonglou News App is an important way for people to obtain information sources in their daily lives. Around 2010, popular foreign news apps included Zite and Flipboard, while popular domestic news apps were mainly the four major portals. With the popularity of new era news recommendation products represented by Toutiao, news apps have entered a new era. As for technology companies, no matter which one they are, as long as they master the sophisticated news recommendation algorithm technology, they will basically have the initiative and voice at the technical level. Today, let’s take a look at a RecSys2023 Best Long Paper Nomination Award paper—GoingBeyondLocal:GlobalGraph-EnhancedP

See all articles