LESSON 01 · One leader

Send every write through one leader

Replication keeps copies of changing data, and the acknowledgment rule decides what a crash can lose.

SYSTEM VIEW

The replication path

Diagram view
Commit waits—
Copies when acked—

Watch one leader put every payment in a single log, then see what each rule for confirming a write costs.

  1. The leader is the only node that accepts writes. It records the client’s $25 payment at position 204 of its replication log, right after 203, which both followers have already applied.

    Takes writes
    the leader only

    What changed

    • $25 at position 204
  2. Follower 1 and Follower 2 apply the log in the leader’s order, 204 after 203, so all three copies converge on the same state. The cost: every write still passes through one node.

    Every write
    through one node

    What changed

    • Both followers at 204
    • Three copies agree
  3. With asynchronous replication the leader answers as soon as it commits. The $60 and $15 payments are confirmed at 205 and 206 while both followers still sit at 204, to catch up later.

    What changed

    • $60 at 205, $15 at 206
    • Both confirmed at once
    • Followers still at 204
  4. The leader crashes before shipping 205 and 206. The client was told both payments succeeded, but no follower has them, so whichever follower takes over has never seen them.

    What changed

    • Leader crashes
    • $60 and $15, confirmed but lost
  5. Replay it with synchronous replication: the leader answers only after both followers confirm. Follower 1 confirms the $30 payment at 205 within 1 ms, but Follower 2 is 90 ms away, so every commit waits at least 90 ms.

    What changed

    • $30 at 205
    • Both followers confirm
    • The answer waits for Follower 2
  6. Follower 2’s disk stalls. The $18 payment at 206 reaches both followers and Follower 1 confirms it, but the leader must also wait for Follower 2, so this commit hangs, and every one after it.

    What changed

    • Follower 2 stalls
    • $18 at 206
    • No answer for the client
  7. Switch to semi-synchronous: the leader waits for any one follower. Follower 1 confirmed 206 within 1 ms, which is now enough, so the leader confirms the payment while Follower 2 stays stalled. It survives the leader’s death as long as Follower 1 survives.

    What changed

    • Waits for any one follower
    • OK for 206
    • Follower 2 no longer blocks writes
  8. With Follower 2 still stalled, Follower 1 drops off the network. The $9 payment at 207 waits out a timeout, then MySQL’s semi-synchronous mode falls back to asynchronous and confirms it with only the leader’s copy. Monitor for that switch.

    What changed

    • $9 at 207
    • No follower confirms
    • Back to asynchronous
    • Monitoring alerts

Space plays · ← → step · Home resets

THE IDEA

How it works

Teams replicate data for three reasons: to survive a machine failure, to serve more reads, and to put data closer to users. Making a copy is easy; keeping it current as data changes is the hard part. In single-leader replication, one node, the leader (or primary), accepts all writes and records them in a replication log. Followers (or replicas) apply the log in the same order, so they converge on the same state. Postgres, MySQL, and MongoDB replica sets work this way. The cost is that every write still passes through one node.

The key decision is when the leader tells a client that a write succeeded. With synchronous replication it waits for followers to confirm. With asynchronous replication it answers after its own commit, and followers catch up later. Semi-synchronous replication sits between: it waits for some followers, often one.

Take an example leader with one follower 1 ms away and another 90 ms away. Asynchronous commits wait for neither, but a leader crash loses whatever has not shipped. Waiting for both adds at least 90 ms to every commit, and if either follower stalls, all writes stall. Waiting for any one of them adds about 1 ms, and a write survives the leader’s death as long as the follower that confirmed it also survives. One caveat: MySQL’s semi-synchronous mode falls back to asynchronous after a timeout, so the guarantee can lapse quietly unless you monitor for it.

DECISION POINT

A payments ledger has a leader, two followers in the same region (1 ms away), and one follower in another region (90 ms away). The requirements: no acknowledged payment is lost if the leader’s machine dies, and p99 write latency stays under 20 ms. Which acknowledgment rule do you choose, and what do you tell the business it does not cover?

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

Keep in mind

  • One leader gives every change a single order, but all writes still pass through one node.
  • Waiting for followers trades latency and availability for a smaller chance of losing an acknowledged write.
  • Waiting for any one of several followers guarantees an extra copy without letting one slow follower block commits.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

A leader waits for all three of its followers on every commit. One follower’s disk stalls for a minute. What happens during that minute?

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.