Consensus and Raft

Distributed systems Senior Python 3.13+ PostgreSQL 17+ Reviewed By Best Latest Apps

Predict the output

How many node failures can each cluster size tolerate while keeping a majority?

Python 3.13
for n in (3, 4, 5):
    print(n, (n - 1) // 2)

Options
Hint 1 of 1

Majority of n is n // 2 + 1.

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A 3 1, 4 1, 5 2

Correct. A majority of 4 is 3, so a 4-node cluster still only survives one failure.

Raft commits only with a majority, floor(n/2) + 1, so it survives n minus that many failures. A 4-node cluster needs 3 votes and tolerates one failure, the same as 3 nodes.

Why not the others?
  • B 3 1, 4 2, 5 2. Losing 2 of 4 leaves 2, which isn't a majority of 4.
  • C 3 2, 4 2, 5 3. Those leave a minority behind.
  • D 3 1, 4 1, 5 1. 5 nodes keep a majority (3) after 2 failures.

30-second answer

Consensus lets a group of nodes agree on one ordered log of decisions even when some crash, so every replica applies the same commands in the same order. Raft elects a leader for a numbered term by majority vote. The leader appends entries and replicates them; an entry is committed once a majority has stored it. A majority is needed for both, so two leaders can't commit conflicting entries in the same term.

Practise saying it · 45 s. No recording, just a timer

Explain Raft in a minute.

Say your answer out loud, then check which of these points you covered:

  • Agree on one ordered log
  • Leader per term by majority vote
  • Committed when a majority stores it
  • 2f + 1 nodes for f failures

Deep dive

With 2f + 1 nodes Raft tolerates f failures: 3 nodes survive 1, 5 survive 2.

Even node counts add cost without adding tolerance: 4 nodes still tolerate only 1.

etcd, Consul and many databases' metadata layers use Raft; ZooKeeper uses a similar protocol (Zab).

Consensus doesn't handle lying nodes; that's Byzantine fault tolerance.

Common mistakes

  • Running consensus clusters with 2 or 4 nodes.
  • Spreading a 3-node cluster across 2 data centers, so losing the bigger one loses the majority.
  • Using consensus for high-volume data instead of small, critical metadata.

Senior take & follow-ups

Say what problem it solves, describe terms, majority votes and majority commit, and give the 2f + 1 rule.

Follow-up questions

Think of your answer first, then open the hint.

  1. Why is 5 nodes slower than 3 for writes?

    Show hint

    Each write waits for more acknowledgements (3 instead of 2).

  2. What stops an old leader from committing?

    Show hint

    Followers reject entries from a lower term.

Suggest a fix if something here is wrong or out of date.