A consensus protocol in a distributed system
A naive explanation of Raft is as follows:
- Replicated logs
The leader serves writes and the client gets an ack after reliably storing the write with followers.
Each follower expects a heartbeat from the leader. If no heartbeat, a follower chooses to be a leader and ask for majority votes.
Each change to the system goes through the leader. There are three phases of a commit:
- The leader appends the change in its log. A log entry has a term number & an index.
- It then sends the change to followers.
- A follower applies the change(not commit) in its log and ack the leader.
- If the leader gets acknowledgment from the majority of the followers, leader commits the change and acknowledges the client.
- In the next heartbeat, the leader notifies followers.
- Followers apply the change.
- Each leader gets a term. The term remains a unique, non-decreasing number in the cluster. The term number increases with each successful leader election.
- A node can vote for only one candidate node in a term.
- A node votes for a candidate whose log is at least the same index.
- The candidate with maximum vote wins.
The leader with a higher term always wins. If there was a leader in a partition, the leader steps down and sync its log with the winner leader.