interviewpath
  • 0day streak
  • 10total XP
  • Level 1
0/7

Unit 5 · Distributed & cloud · Lesson 3 of 14

Replication, quorums and consensus (Raft)

Replication keeps data available and durable when machines fail; consensus lets replicas agree on one order of updates despite crashes and partitions. Leader-based, multi-leader and leaderless replication, the limits of quorums, and how Raft elects leaders and commits entries are core knowledge for distributed-systems roles and research.
About 5 hFocus mode

Step 1: Learn the ideas

  • Synchronous versus asynchronous replication
  • Single-leader replication and failover
  • Multi-leader replication and write conflicts
  • Leaderless replication, quorums, read repair and anti-entropy
  • Sloppy quorums and hinted handoff
  • Replicated state machines and the consensus problem
  • Raft terms, leader election and randomized timeouts
  • Log replication, log matching and conflict repair
  • Commitment rule and leader completeness
  • Membership changes and snapshots (awareness)
  • Majorities, 2f + 1 replicas and quorum placement

Why it matters

Every durable, highly available service keeps several copies of its data, and many subtle distributed bugs live in how those copies are updated and how a new leader takes over. Consensus also underlies the coordination services (etcd, ZooKeeper) that other systems depend on. For distributed-systems roles you should be able to explain Raft from memory; for other roles, know what each replication style guarantees and where it breaks.

Core ideas

Three replication styles.

  • Single leader: all writes go to one node, which ships a log to followers. Ordering is simple; failover and stale follower reads are the hard parts. With asynchronous replication, failover can lose acknowledged writes.
  • Multi-leader: several nodes accept writes (often one per region) and exchange them asynchronously. Good for local write latency and offline clients; conflicts are inevitable and need a resolution rule.
  • Leaderless: clients write to and read from several replicas directly; read repair and background anti-entropy converge them.

Quorums. With N replicas, writes waiting for W acknowledgments and reads querying R replicas, R + W > N means every read set intersects every successful write set, and that is all it promises. It does not give linearizability: a sloppy quorum may write outside the usual N nodes; concurrent writes resolved by timestamps can silently drop one; a write that reached fewer than W nodes is reported as failed but not rolled back, and may be read later; a read concurrent with a write can return the new value to one client and the old value to the next; restoring a replica from a stale copy breaks the counting argument.

Consensus makes a group agree on one sequence of commands, which turns a deterministic service into a replicated state machine. With 2f + 1 replicas it tolerates f crashes, because any two majorities overlap.

Raft in five rules (the paper's Figure 2 has the exact version):

  1. Terms are numbered epochs with at most one leader each. Any message carrying a higher term makes the receiver adopt it and step down.
  2. Election: a follower that hears nothing for a randomized timeout becomes a candidate, increments its term and requests votes. A server votes at most once per term, and only for a candidate whose log is at least as up to date as its own (compare last log term, then last index). Randomized timeouts make repeated split votes unlikely.
  3. Log matching: AppendEntries carries the index and term of the preceding entry. A follower rejects it unless it holds a matching entry; the leader retries from earlier positions until they agree, and the follower then replaces its conflicting suffix with the leader's entries. So if two logs share an entry's index and term, they are identical up to it.
  4. Commitment: a leader marks an entry committed once a majority stores it and it belongs to the leader's current term; earlier entries are committed indirectly. Counting replicas of an older-term entry is not enough, because a different leader could still overwrite it.
  5. Persistence: the current term, the vote and the log reach stable storage before a server answers, or a restarted server could vote twice or forget acknowledged entries.

The election restriction plus the commitment rule give leader completeness: every future leader already holds every committed entry.

Ad hoc failover is dangerous. A monitor that promotes a follower when the leader stops answering can create two leaders if the old one was only slow. Terms or epochs, a majority vote and fencing of the old leader prevent split brain.

Common misconceptions

  • "R + W > N gives strong consistency." It gives overlap, not linearizability, for the reasons listed above.
  • "Replication protects data from mistakes." A bad write or delete replicates as faithfully as a good one; replicas protect against machine loss, not corruption or operator error.
  • "Raft commits an entry as soon as a majority has it." Only entries of the current term; older ones are committed once a later current-term entry is.
  • "More replicas are faster and safer." They raise durability but enlarge the quorum and the commit latency; an even count adds cost without tolerating another failure.
  • "An up-to-date follower can serve linearizable reads." It cannot know it is up to date without contacting the leader or relying on a time-based lease.

How to approach it

Read DDIA Ch. 5 for the styles and quorums, then the Raft paper through Section 5 with Figure 2 beside you. Before coding, trace scenarios in the RaftScope visualization: kill the leader, partition a minority, restart a stale node. If you target distributed-systems roles, implement the 6.5840 Raft lab; nothing else exposes the rules as quickly. Graduates should explain elections and majorities. Mid-level candidates should walk through log repair, the commit rule and quorum pitfalls. Senior candidates should discuss read paths, membership changes, placement across zones and how consensus latency shapes the rest of the design.

Suggested order

  1. 1Read DDIA Ch. 5 (Replication): leaders and followers, multi-leader replication, and the leaderless section including "Limitations of Quorum Consistency" and sloppy quorums.
  2. 2Trace elections and log repair in the RaftScope visualization on raft.github.io, predicting each step before running it.
  3. 3Read the Raft paper through Section 5 with Figure 2 open, then the 6.5840 Raft lecture notes; rewrite the five rules from memory.
  4. 4Read DDIA Ch. 9, section "Distributed Transactions and Consensus", and SRE book Ch. 23 for how consensus is deployed and what it costs.
  5. 5Read the Students' Guide to Raft, then implement 6.5840 Lab 3 parts 3A and 3B (leader election and log); continue with 3C and 3D if you target distributed-systems roles.
  6. 6Explain with a drawn example why a leader may not commit an entry from an earlier term by counting replicas (the scenario in the paper's Figure 8).

Step 2: Check off what you can do

Tap each one you can genuinely do or explain. Each earns 10 XP.

Step 3: Go deeper

  • In Search of an Understandable Consensus Algorithm (Extended Version)Start here

    raft.github.io

    Section: Section 5: The Raft consensus algorithm (with Figure 2)

    Authoritative description of terms, elections, log replication and the safety argument.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • Designing Data-Intensive ApplicationsStart here

    O'Reilly Media (author's book site)

    Section: Ch. 5: Replication (and the consensus sections of Ch. 9)

    Comparison of replication styles, the limits of quorums and the role of consensus.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • MIT 6.5840 Distributed Systems

    MIT PDOS

    Section: Lab 3: Raft (with the LEC 6 and LEC 7 notes)

    Implement Raft against tests that inject crashes, partitions and message loss.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • Students' Guide to Raft

    Jon Gjengset's blog

    Implementation details and debugging advice that make the Raft lab tractable.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • Site Reliability Engineering (Google SRE book)

    Google (free online edition)

    Section: Ch. 23: Managing Critical State: Distributed Consensus for Reliability

    How consensus is used in production, its latency costs and replica placement.

    Open(opens in a new tab)Link responded, 5 Oct 2026

Step 4: Practice

Begin with the RaftScope tracing exercise until you can predict elections and log repair. Then implement the 6.5840 Raft lab in order (3A election, 3B log, 3C persistence, 3D snapshots) and run each test many times, because a rare failure is still a bug. Without coding time, write out by hand the logs of five servers through two leader changes and mark which entries are committed. Finally, read one Jepsen analysis of a replicated store and identify which replication rule was violated.

Record attempts on each problem’s page (+15 XP, +10 more when solved on your own).

Step 5: Test yourself

Answer each one out loud or on paper before you finish the lesson.

  1. Question 1Why does a candidate need a log at least as up to date as the voter's to win its vote?
  2. Question 2What must a Raft server persist before replying to RequestVote, and what goes wrong if it does not?
  3. Question 3How does a new leader bring a follower with a conflicting log suffix back in line?
  4. Question 4Why is a cluster of four no more fault tolerant than a cluster of three?
  5. Question 5How can asynchronous replication lose an acknowledged write during failover?
  6. Question 6When is multi-leader replication worth its conflict-handling cost?

You’re done when you can

  • Explain Raft election, log matching and the commit rule from memory in under five minutes, with a drawn example.
  • List three ways quorum reads with R + W > N can still return stale or inconsistent results.
  • Pass the 6.5840 Raft 3A and 3B tests repeatedly, or predict five RaftScope scenarios correctly before replaying them.
  • Size a replica group that survives two simultaneous failures across three zones and justify the placement.

Finish the lesson

How confident do you feel?

Lesson complete!

+50 XP

Reviews will come up on the Practice tab so it sticks.

Unlocks next: Multi-region design, durability and disaster recovery, Invariants, safety, liveness and distributed correctness, Systems paper route: MapReduce, Dynamo and Raft

Your notes and study time

Log study time