Course contents

09. Reliability and Resilience Lesson 8 of 10

Failover

Watch an active-passive database fail over: detection, promotion and redirection, the error window, lost async writes, split brain and fencing.

16 minIntermediate7 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • Failover = detect, decide, promote, redirect; detection alone changes nothing.
  • Writes fail for the whole failover window; size it from each stage.
  • With asynchronous replication the standby may lack writes that were already acknowledged.
  • Split brain happens when two nodes accept writes; stale client views and unfenced restarts cause it.
  • Fencing and a single, quorum-backed leader make sure only one primary exists.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

Failover moves the work of a failed component onto a standby that takes over its role. For stateless servers behind a load balancer it is just ejection: the others pick up the traffic. For components with a single writer, such as a primary database, a leader in a cluster or a stateful appliance, failover means promoting a standby to the role and redirecting every client to it.

Why it exists

Redundancy does not help until something switches to it. A standby database that replays the primary's log is a full copy, but clients keep sending writes to the dead primary until someone decides it is dead, promotes the standby and tells everyone. Failover is that procedure, ideally automatic and always rehearsed.

How it works

Active-passive. One node is active (takes writes), one or more are passive (replicate, take no writes). Variants: hot standby is running and current, warm standby is running but may need catching up or scaling, cold standby must be started or restored first.

Stages.

  1. Detect: health checks or heartbeats miss for long enough (interval × threshold).
  2. Decide: a controller confirms the failure, ideally from more than one vantage point, so that a network blip does not trigger a failover. Patroni, Orchestrator, Pacemaker and managed database services hold the leader role in a consensus store (etcd, ZooKeeper, Consul) or a quorum.
  3. Promote: the chosen standby stops replaying, applies what it has, and opens for writes. The controller picks the most up-to-date standby.
  4. Redirect: clients reach the new primary through a proxy, a virtual IP that moves, a DNS record with a short TTL, or a service-discovery update.
  5. Fence: the old primary must not accept writes again (see below), and later rejoins as a standby.

Failover time is the sum of the stages. Clients that cache DNS answers or hold pooled connections to the old address add to it. Real database failovers usually take from tens of seconds to a few minutes.

Data loss. With asynchronous replication the primary acknowledges a write before the standby has it. Whatever has not been shipped when the primary dies is missing after promotion: that is the failover's recovery point (RPO). Synchronous replication (commit waits for a standby) or semi-synchronous replication (at least one standby confirmed) brings it to zero at the cost of a round trip per write and a dependency on standby health.

Split brain and fencing. If the old primary keeps running, because the failure was a partition, or because it restarts still configured as primary, and some clients still reach it, two nodes accept writes and the data diverges. Prevention: a single leader decided by quorum, so a minority side cannot promote; and fencing, which makes the old primary harmless: power it off or cut its network through the platform API (STONITH), revoke its storage, or use fencing tokens (a monotonically increasing epoch on every write, rejected by storage when it is older than the latest).

When to use it

For every single-writer component on the critical path: primary databases, message broker leaders, stateful gateways, NAT or firewall pairs. Use automatic failover when the outage cost of waiting for a human is higher than the risk of a false failover, and only with fencing in place.

When not to

  • Stateless tiers: run several active instances behind a balancer instead.
  • When a false failover is more expensive than a short outage and you cannot fence reliably; a manual, scripted failover is safer then.
  • When the data store already replicates through consensus (Raft-based databases elect a new leader per shard on their own); do not stack a second failover mechanism on top.

Trade-offs

Choice You gain You pay
Automatic failover Short outages at any hour Risk of false failovers and split brain without quorum and fencing
Short detection timeouts Faster failover More false failovers on blips
Synchronous replication No acknowledged write lost Write latency; writes stall if the standby is slow or gone
DNS-based redirection Works across networks and regions Clients cache answers; switching takes TTL or longer
Proxy / virtual IP redirection Near-instant switch Another component to make redundant; usually one network

Alternatives

  • Active-active: several nodes take writes (multi-primary, or sharding so each key has one owner). No promotion step, but conflict handling or partitioning becomes your problem.
  • Consensus-replicated stores (Raft or Paxos per range or shard): leader election and log safety are built in.
  • Restore from backup: the cold path for disasters; minutes to hours.

Failure modes

  • No promotion: detection works, but nothing promotes the standby; the outage lasts until a human acts.
  • Lost writes: async lag at the moment of failure is lost after promotion.
  • Split brain: two primaries accept writes; repairing diverged data is manual and slow.
  • Stale clients: connection pools and DNS caches keep using the old address long after the switch.
  • Standby not really ready: it was lagging hours behind, or was never sized for full load, or its configuration drifted.
  • Flapping failover: aggressive timeouts fail over back and forth on a flaky network.

In production

Rehearse failovers regularly (planned switchovers during working hours are the cheapest test). Monitor replication lag and alert when it exceeds what you are willing to lose. Make clients reconnect on failure and keep DNS TTLs for failover records short. Decide failback in advance: usually the old primary rejoins as a standby, and you switch back only in a planned window if at all.

Common misconceptions

  • "We have a replica, so we have failover." A replica is a copy. Failover needs detection, a decision, promotion, redirection and fencing.
  • "Failover is instant." Each stage takes time, and clients add their own.
  • "Acknowledged means durable everywhere." With async replication, acknowledged means durable on the primary only.
  • "Split brain is a theoretical problem." Partitions and unfenced restarts make it routine without quorum and fencing.

Interview angle

When a design has a primary database, say how it fails over: who detects, who decides, how clients find the new primary, how long it takes, and how much data async replication can lose. Interviewers often follow with "what if the network partitions?": answer with quorum-based leadership and fencing. Giving the failover window in numbers and mentioning rehearsals stands out.

Model assumptions

The engine runs the traffic, crashes, health checks, replication and the promotion. Two app tiers each write 80 times per second (Poisson, writes only) through their own proxy; each database has 20 connections and a 15 ms mean write. Proxies probe every 500 ms; 3 failures eject, 2 successes restore. DB 1 replicates asynchronously, about 100 ms behind (plus the 10 ms hop); changes it has not delivered when it crashes are lost. Promotion happens at a fixed time (detection and the controller's decision are not modelled): DB 2 takes over DB 1's replication settings, the proxies that wrote to DB 1 write to DB 2, and the acknowledged writes DB 2 never received are counted as lost. Split brain is modelled as one proxy keeping the old target while the old primary restarts as a primary that follows nobody; fencing as that proxy being repointed and DB 1 rejoining as DB 2's standby. The lost-writes step is a scripted slow-motion replay of two orders with traffic paused (the engine has no message contents). Times are compressed; real failovers are slower. Numbers are illustrative, not benchmarks.

The lesson as textEvery step of the simulation, written out.
  1. 1. One active database, one waiting

    Two app tiers write 80 times per second each. Their database proxies send every write to DB 1, the primary. DB 2 is a standby: it takes no traffic and replays every change DB 1 ships to it (purple packets), about 100 ms behind.

    This is active-passive: you pay for a second database that does nothing visible, so that there is something up to date to switch to.

    Scene: App A and App B write through two database proxies to DB 1, the primary. DB 2 receives replicated changes and serves no traffic. Every write succeeds.

    Key idea: A standby is only useful if it is current, and only becomes useful once something promotes it.

  2. 2. The primary crashes and nobody takes over

    At 1 s DB 1 crashes. Every write fails at once. The proxies’ health checks notice after three failed probes (about 1.5 s later) and stop sending to DB 1.

    That does not help: the proxies are configured to send writes only to the primary, and the primary is gone. Writes keep failing, now with “no backend”. Detection is the first part of failover. Someone still has to decide to fail over, promote the standby and redirect the clients.

    Scene: DB 1 goes offline. Every write fails; after about 1.5 seconds the proxies mark DB 1 unhealthy, but they have no other primary, so writes keep failing.

    Key idea: Detecting a dead primary is not failover. Without promotion and redirection, a standby just watches the outage.

  3. 3. What the standby never received

    Before promoting DB 2, ask what it has. This step is a scripted replay of the last second before the crash, with normal traffic paused.

    App A writes order #1041: DB 1 commits it, acknowledges it, and a little later ships it to DB 2. Then App B writes order #1042. DB 1 commits it and tells App B “saved”, but replication is asynchronous: the record is still waiting to be shipped when DB 1 dies at 1.5 s.

    Promote DB 2 now and #1042 is gone, although App B was told it was saved. The amount you can lose is the replication lag at the moment of failure: at 160 writes per second and about 110 ms of lag, about 15 orders. The next step runs the real failover and counts them. Synchronous or semi-synchronous replication (commit only after a standby has the record) closes that gap, at the cost of a network round trip on every write.

    Scene: Scripted replay with traffic paused: order 1041 is written, acknowledged and replicated to DB 2. Order 1042 is written and acknowledged to App B, but DB 1 crashes at 1.5 seconds before shipping it, so DB 2 never receives it.

    Key idea: With asynchronous replication, failover can lose writes that were already acknowledged. The lag at the moment of failure is your data loss.

  4. 4. Detect, decide, promote, redirect

    Traffic is back and DB 1 crashes again at 1 s. This time a failover controller acts: the proxies eject DB 1 at about 2.5 s, the controller waits a moment to be sure (a brief network blip should not trigger a failover), promotes DB 2 to primary and points both proxies at it. At 4 s writes flow to DB 2.

    At the moment of promotion the simulation compares what DB 1 had acknowledged with what DB 2 had applied: about 15 writes are gone, the ones still in DB 1’s log when it crashed (the replay in the previous step, at full speed). Those customers were told their orders were saved.

    The outage lasted about 3 s: detection (≈1.5 s) + decision and promotion (≈1.5 s). Here the times are compressed. Real database failovers usually take from tens of seconds to a few minutes, and clients that cache DNS or keep pooled connections to the old address take longer still.

    Scene: DB 1 crashes at 1 second. Writes fail until 4 seconds, when DB 2 is promoted and both proxies send writes to it; after that every write succeeds. About 15 writes acknowledged just before the crash never reached DB 2 and are lost.

    Key idea: Failover time = detection + decision + promotion + redirection. Each part has its own knob and its own risk.

  5. 5. The old primary comes back: two primaries

    DB 2 is the primary now. But App B never got the switch: its proxy still uses the old address (a cached DNS answer or a stale config file), so its writes have been failing.

    At 1 s DB 1 restarts. Nothing told it that it was replaced, so it comes back as a primary, not as DB 2’s replica, and accepts writes. App B’s proxy sees it healthy again and sends its writes there; App A keeps writing to DB 2. Both databases accept writes, nobody replicates between them, and the data diverges. This is split brain, and it is worse than the outage: merging two diverged databases by hand can take days.

    Scene: App B’s proxy still points at DB 1. When DB 1 restarts at 1 second it accepts App B’s writes while DB 2 accepts App A’s. Two primaries take writes and their data diverges.

    Key idea: Split brain: two nodes both act as primary. It comes from stale views of who the primary is, and corrupts data silently.

  6. 6. Fence the old primary, rejoin it as a standby

    The fix is to make sure a replaced primary cannot accept writes, whatever any client believes. That is fencing: power it off or cut its network through the cloud API (STONITH, “shoot the other node in the head”), revoke its storage, or attach an epoch number to every write so that writes from an older term are refused.

    The failover controller holds the leader role through a lease in a consensus store (etcd, ZooKeeper, Consul) and publishes the new primary to every client. Here: DB 1 is demoted and rejoins as a standby of DB 2, replication now runs the other way, and both proxies write to DB 2 only.

    Scene: Both proxies write only to DB 2. DB 1 is a standby again and receives replicated changes from DB 2. Every write succeeds.

    Key idea: Promotion must come with fencing: exactly one node may accept writes, enforced by the infrastructure, not by hoping clients notice.

  7. 7. Failover is a procedure, not a component

    Roles are swapped and the system is back to active-passive, with DB 2 active.

    A failover design answers: how failure is detected and confirmed; who decides (one controller holding a lease, never two); how the standby is promoted and how much data it may lack (async lag vs. synchronous commit); how every client learns the new address (proxy, virtual IP, DNS with a short TTL); how the old primary is fenced; and when and how to fail back. Then you practise it regularly: an untested failover path tends to fail when you need it.

    Scene: DB 2 is the primary and receives every write through both proxies; DB 1 is the standby and replays DB 2’s changes. Every write succeeds.

    Key idea: Detection, decision, promotion, redirection, fencing: a failover is only as reliable as its weakest step, and only trustworthy once rehearsed.