Consensus with Raft
Definition
Consensus
- In plain English
- Consensus is a group of machines agreeing on the same sequence of decisions even when some fail.
- Think of it like
- It is like a committee keeping identical minutes even if members step out.
Replicated log
- In plain English
- A replicated log is an ordered list of changes saved on several machines.
- Think of it like
- It is like several clerks keeping copies of the same numbered instruction book.
Raft
- In plain English
- Raft is a consensus algorithm where an elected leader appends decisions to a log and a decision is final once a majority has stored it.
- Think of it like
- It is like a chairperson reading out each motion and it passing once most members have written it down.
Term
- In plain English
- A term is a numbered period with at most one leader, and a higher term always wins.
- Think of it like
- Like numbered sessions of a council.
Heartbeat
- In plain English
- A heartbeat is a regular message from the leader saying it's still alive.
- Think of it like
- Like a lighthouse flashing on schedule.
Commit
- In plain English
- In Raft, an entry is committed once a majority of nodes have stored it, and then it's never lost.
- Think of it like
- Like a motion recorded in most members' notebooks.
Candidate
- In plain English
- A candidate is a node asking the other nodes to elect it as leader.
- Think of it like
- It is like a team member asking for votes to become captain.
Why it exists
A routing-rule update must survive a leader crash. Every surviving machine must eventually apply the same accepted changes in the same order.
A leader can commit an entry by counting saved copies only when the entry belongs to its current term.
That also commits earlier entries in the log. These guarantees assume correct Raft behaviour, saved logs that survive restarts, and machines that do not lie.
How it works
- Wait
Followers receive regular heartbeats; randomized election timeouts reduce simultaneous candidates.
- Ask for votes
A timed-out follower increases its term, becomes a candidate, and requests votes.
- Check the candidate
Each node votes at most once per term and only for a candidate whose log is sufficiently up to date.
- Elect
A candidate with a majority becomes leader; nodes step down when they learn of a higher term.
- Append
The client sends a change, and the leader saves it in its log.
- Copy
Followers check the preceding log position, save the new entry, and acknowledge it.
- Commit
The leader counts a majority, including itself, for an entry in its current term.
- Apply and reply
Apply committed entries in order, then confirm the result to the client.
The leader sends a routing update to two followers and confirms after itself and follower one have saved it and it is applied.
A follower becomes a candidate on timeout, wins leadership with majority votes, or returns to follower after seeing a higher term.
Examples
Replace a failed leader
Timing plans for 400 intersections live in a five-node Raft store. The leader reboots.
A term-eight candidate counts its own vote and two follower votes to become leader in a five-node cluster.
The candidate must have a sufficiently up-to-date log to win. Recovery may take seconds in this setup, but network delays can make it longer.
The insight
Raft can replace a failed leader when a majority can communicate.
Keep one agreed routing history
A network partition separates two of five routing-store nodes from the other three.
The connected three-node side can elect and commit routing changes, while the isolated two-node side cannot commit new writes.
After communication returns, followers copy the leader’s log. Conflicting entries that were never committed may be replaced; committed entries remain.
Raft orders writes, but fresh reads still need a safe leader check. Reading any follower does not automatically give strong consistency.
The insight
A majority can progress after election; the minority cannot create a competing committed history.
Trade-offs
Pros
- Keeps committed decisions in one order.
- Replaces failed leaders automatically.
- Continues with a connected majority.
Cons
- Writes wait for a majority.
- Leader capacity can limit throughput.
- Minority partitions cannot commit writes.
Use when
- Configuration changes need one agreed history.
- Coordination metadata must survive minority failures.
Avoid when
- Independent high-volume updates can tolerate conflicts.
- An uncoordinated follower read is being treated as fresh.
Quick check
Takeaways
- Raft combines an elected leader, a replicated log, and majority-based commitment.
- A leader directly commits its current-term entries by majority counting.
- A minority cannot commit new writes, even when it contains an old leader.