LESSON 05 · Consensus in practice

Agree through a majority log

Raft-style consensus keeps one agreed log by electing a leader per numbered term and treating an entry as committed once a majority stores it.

SYSTEM VIEW

The agreement path

Diagram view
Term—
Stored on—
Votes—

Watch five nodes commit entry 42 on a majority, lose their leader to a partition, and elect one that still holds it.

  1. Five nodes keep one log. N1 leads term 7: it appends each command and copies it to the followers. A client’s write becomes entry 42, and N2 and N3 store it and reply. N4 and N5 lag behind and don’t have it yet.

    What changed

    • Entry 42 on N1
    • N2 and N3 store it
  2. N1, N2 and N3 now hold entry 42: three of five, a majority. Only now does N1 apply it and acknowledge the client. Any later majority must include one of these three, so the entry is safe.

    Majority of 5
    3
    Holding 42
    N1, N2, N3

    What changed

    • Entry 42 committed
    • Client acknowledged
  3. A partition now isolates N1 and N2 from the other three. N1’s retry of entry 42 to N4 is lost, so N4 and N5 still lack it. N1 still thinks it leads term 7, but it can’t reach a majority, so nothing new commits.

    What changed

    • N1 and N2 cut off
    • Retry to N4 lost
  4. Hearing from no leader, N4 stands in term 8 and votes for itself. N5 votes yes, but N3 refuses: N4’s log lacks entry 42, so it is behind N3’s. That refusal is the protection working. Two votes of five aren’t a majority.

    N4’s votes
    N4, N5
    N3 refuses
    N4 lacks 42

    What changed

    • N4 stands in term 8
    • N3 refuses N4
  5. N3 stands in term 9. N4 and N5 vote yes, since N3’s log is at least as complete as theirs, and with its own vote N3 has three: a majority. N3 leads term 9 and holds entry 42, so the committed entry survives.

    N3’s votes
    N3, N4, N5
    Entry 42
    kept

    What changed

    • N3 leads term 9
    • Entry 42 kept
  6. The partition heals, and N1, still sure it leads term 7, sends to N3. N3 has seen term 9, so it refuses and replies with its term. That term check is the protection working: N1 learns of term 9 and steps down to follow N3.

    N1’s term
    7 < 9

    What changed

    • N1 and N2 cut off
    • N3 refuses term 7
    • N1 follows N3
  7. Say N4 and N5 crash. N1, N2 and N3 are still three of five, so entry 43 commits, paying what every write pays: a majority round trip and a disk flush. A sixth node would add no tolerance, as the majority rises to four.

    5 or 6 nodes
    survive 2 failures
    3 nodes
    survive 1
    Elections
    writes pause ~1 s

    What changed

    • N4 and N5 down
    • Entry 43 committed

Space plays · ← → step · Home resets

THE IDEA

How it works

Leases and fencing depend on something that cannot disagree with itself. Consensus provides it: several nodes agree on one sequence of decisions even when some crash. Raft, used by etcd and CockroachDB, organizes this as a replicated log. A leader appends each command and copies it to followers. An entry the leader created in its own term counts as committed once a majority stores it, and only then is it applied and acknowledged.

Time is divided into numbered terms, each with at most one leader. A node votes once per term and a candidate needs a majority, and any two majorities share a node, so two candidates cannot win the same term. Nodes reject messages from a lower term than they have seen, and a stale leader steps down on learning of a higher one. Voters also refuse a candidate whose log is behind their own.

Take five nodes with N1 leading term 7. Entry 42 reaches N1, N2, and N3, a majority, so the client gets its acknowledgment. A partition then isolates N1 and N2. N4 and N5 lack entry 42. N4 asks for votes in term 8, but N3 refuses, so N4 gets only two. N3 then stands in term 9 and wins with three votes, and entry 42 survives.

Each write costs a majority round trip and a disk flush, and writes pause during elections, often around a second. More nodes do not raise write throughput. Five nodes tolerate two failures and three tolerate one; a sixth adds no tolerance, because the majority rises to four.

DECISION POINT

A team runs three etcd nodes in one availability zone, and writes are fine. Leadership wants more resilience, and someone proposes going to seven nodes in that same zone. What do you recommend?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • An entry is safe once a majority stores it, because any later majority overlaps that one.
  • Terms let nodes detect and reject a stale leader.
  • Even-sized groups add cost without adding fault tolerance.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

A team grows a consensus group from five nodes to six to improve fault tolerance. What changes?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.