NOTE
3.3 Distributed Consensus Algorithm: Raft
1. What Is Raft - A distributed consensus algorithm invented by Diego Ongaro. 2. Why Raft Is Needed - To solve the complexity of implementing Paxos. 3. Raft Algorithm Process 3.1. Roles - Leader - Follower - Candidate 3.2. Three Phases 3.2.1. Phase 1: Leader Election
This is a historical learning note and may contain outdated or incomplete understanding.
1. What Is Raft?
- A distributed consensus algorithm invented by Diego Ongaro.
2. Why Is Raft Needed?
- To solve the complexity of implementing Paxos.
3. Raft Algorithm Process
3.1. Roles
- Leader
- Follower
- Candidate
3.2. Three Phases
3.2.1. Phase 1: Leader Election
- Rule: whoever has the larger term is elected leader; if the terms are the same, whoever has the larger index is elected leader.
- Vote: (term, index)
- term: which Leader term it is.
- index: index of the latest log entry.
- All Nodes are in the Follower state.
- If a Follower does not receive a heartbeat from the Leader for a certain amount of time, it switches itself to the Candidate state and starts an election.
- If more than half of the Nodes return true, it is elected Leader.
- If no more than half of the Nodes return true, it waits for a while and starts another election.
- If, during the election, it receives a request from a Leader whose term > its own term, it gives up the election and becomes a Follower.
- After the election ends, it becomes either a Leader or a Follower.
3.2.2. Phase 2: Normal Replication
- The Leader receives a client write request.
- The Leader makes RPC calls to replicate it to the other Followers.
- If more than half of the Followers return true, replication succeeds and the Leader commits the local log.
- If no more than half of the Followers return true, replication fails and the Leader does not commit the local log.
- The Leader returns write success or failure to the client.
3.2.3. Phase 3: Crash Recovery
- The old Leader crashes, a new Leader takes office, and the other Followers switch to the new Leader and begin synchronizing data.
- Leader election is the same as Phase 1.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub