LESSON 04 · Failover

Replace a failed leader safely

Promoting a new leader is routine, but it can lose acknowledged writes and leave two nodes that both accept them.

SYSTEM VIEW

The replication path

Diagram view
Leaders—
Lost writes—

Watch Patroni replace a stalled leader with a follower 8 writes behind, then see the old leader wake up still sure it leads.

  1. The leader, in term 7, has acknowledged every write up to position 5,000, so clients saw each one confirmed. It copies writes to the follower asynchronously, after answering, so the follower has reached only 4,992.

    Acknowledged
    up to 5,000
    Follower has
    up to 4,992

    What changed

    • Acked up to 5,000
    • Follower at 4,992
  2. Patroni hears the leader’s heartbeats until the leader stalls and they stop. After 5 s of silence it declares the leader gone; it can’t tell a stall from a crash. This 5 s timeout can fail over on a brief blip; 60 s would mean a minute without writes.

    5 s timeout
    a blip can fail over
    60 s timeout
    a minute without writes

    What changed

    • Leader stalls
    • 5 s without a heartbeat
    • Patroni times out
  3. Patroni promotes the most caught-up follower with a new term, 8, and points the app servers at it. The new leader takes writes from here on. Patroni automates this for Postgres; MongoDB replica sets hold their own elections.

    New leader
    term 8
    Its history
    up to 4,992

    What changed

    • Follower promoted, term 8
    • App servers point at it
  4. The new leader’s history ends at 4,992, so writes 4,993 to 5,000, which clients saw acknowledged, are missing, and the next write takes 4,993. Reused IDs make it worse: order 88,213 can now name a different order than a cache remembers.

    Missing
    4,993 to 5,000
    Order 88,213
    can name another order

    What changed

    • Next write takes 4,993
    • 8 acknowledged writes
  5. The old leader wakes 30 s after it stalled. It never saw term 8, so it still believes it leads term 7, and accepts 5,001 from an app server still connected to it. Two nodes now take writes that can’t be merged automatically: split brain.

    Stalled for
    30 s
    Leading
    term 7 and term 8

    What changed

    • Old leader wakes
    • It accepts 5,001
    • Two leaders
  6. The old leader ships 5,001 stamped with its term, 7. The new leader has seen term 8, so it refuses the write and answers with its term, and the old leader steps down. Fencing works only if something checks: followers, shared storage or a proxy.

    Its write
    7 < 8: refused
    Checked by
    followers, storage, proxy

    What changed

    • Write stamped 7 refused
    • Old leader steps down
  7. The old leader rejoins as a follower and drops everything after 4,992, since term 8 took those positions: the 8 acknowledged writes and the one it took while stale. Under Raft, a majority must elect and commit, so a cut-off minority can do neither; some teams keep failover manual.

    Raft
    a minority can’t elect or commit
    Manual failover
    when loss beats downtime

    What changed

    • 4,993 to 5,001 dropped
    • Old leader follows term 8

Space plays · ← → step · Home resets

THE IDEA

How it works

Failover has three steps: detect that the leader is gone, usually through missed heartbeats; promote a follower, normally the most caught-up one; and point clients and other followers at it. Tools like Patroni automate this for Postgres, and MongoDB replica sets hold built-in elections. The detection timeout is a tradeoff. At 5 seconds a brief network blip can trigger a needless failover; at 60 seconds a real failure means a minute without writes.

With asynchronous replication, promotion can lose writes. Say the leader acknowledged up to position 5,000 and the best follower has reached 4,992. Those 8 writes are missing from the new history, and when the old leader returns, its extra writes usually get discarded, since new writes took those positions. Reused auto-increment IDs make this worse: order 88,213 can now mean a different order than a cache remembers.

Split brain is the worse failure. A leader that was only paused or cut off can still believe it leads, so two nodes accept writes that have no automatic way to merge. The defense is fencing. Each new leadership gets a higher number, called a term or epoch, and whatever checks writes refuses older ones. If the term 7 leader stalls for 30 seconds and term 8 is elected, its later writes carry a 7 and are rejected. Fencing only works if something checks: followers, shared storage, or a proxy. Raft requires a majority to elect and to commit, so a cut-off minority can do neither. Some teams keep failover manual when lost writes are worse than downtime.

DECISION POINT

Your Postgres cluster uses asynchronous followers and fails over after 5 seconds of missed heartbeats. A 6-second network blip on the leader’s rack caused a failover and about 200 writes were lost, even though the old leader was healthy. What do you change, and what do you give up?

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

Keep in mind

  • Promoting an asynchronous follower can discard writes that clients saw acknowledged.
  • A term number lets the rest of the system reject a stale leader that still believes it is in charge.
  • A short failure timeout cuts downtime but raises the chance of a needless failover.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
DiagnoseQUESTION 01

After a failover, two nodes both claim to be leader, and customer balances differ depending on which one a request reaches. What went wrong?

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.