System Design Academy

Saving…

Lesson 9 of 10 in Module 50 of 54 lessons complete

Consensus with Raft

Module 5, lesson 9Concept12 min

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

  1. Wait

    Followers receive regular heartbeats; randomized election timeouts reduce simultaneous candidates.

  2. Ask for votes

    A timed-out follower increases its term, becomes a candidate, and requests votes.

  3. Check the candidate

    Each node votes at most once per term and only for a candidate whose log is sufficiently up to date.

  4. Elect

    A candidate with a majority becomes leader; nodes step down when they learn of a higher term.

  1. Append

    The client sends a change, and the leader saves it in its log.

  2. Copy

    Followers check the preceding log position, save the new entry, and acknowledge it.

  3. Commit

    The leader counts a majority, including itself, for an entry in its current term.

  4. 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 current-term entry needs saved copies on a majority.

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.

Terms let outdated candidates and leaders step down.

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.

Three votes form a majority of five.

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.

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

In a 5-node Raft cluster, how many nodes must store an entry before it's committed?
A partition leaves 2 of 5 nodes on one side. Can that side accept new writes?

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.

Try the quick check above whenever you feel ready.

Previous: Quorums & Leader Election