System Design · Concept Library · Free article
Database Failover — Promoting a Follower Before Anyone Notices
A follower keeps your data alive when the leader dies — but only failover keeps the shard *serving*: detect the death, promote a follower, repoint the traffic, and fence the old leader so it can’t return as a second writer. From manual-vs-automatic and the RTO budget, up through heartbeat detection, split-brain, and fencing — for interns through principal engineers.
Failover is the process of replacing a dead leader with one of its followers so the shard keeps serving: detect the death, promote a caught-up follower to leader, repoint clients at it, and fence the old leader so it can never write again. Replication gave you a live copy; failover is what turns that copy into an actually-serving database when the leader dies — and how fast it happens is your RTO (recovery time objective). Done by a human paged at 4 a.m. it can be thirty-plus minutes of downtime; done by an automatic controller it is seconds. The hard parts are all about being wrong: detect too eagerly and you fail over a leader that merely paused; promote a laggy follower and you lose more recent writes (a higher RPO); and worst of all, if the old leader was only partitioned (not dead) and comes back, you have two leaders accepting writes and the data diverges — split-brain. The fix is fencing — STONITH (forcibly kill the old node) or a fencing token the storage rejects — so the deposed leader can’t corrupt anything. The one thing failover can’t safely do alone is decide who wins when observers disagree across a partition; agreeing on that safely is consensus, the next chapter.
The leader died at 4 a.m. — now what?
You did the hard work already: every shard on the ring is now a leader with a follower shadowing it, so a node death no longer loses data. Then, at 4:02 a.m., a shard’s leader simply stops — a kernel panic, a dead power supply, it doesn’t matter. The data is safe; the follower has a live copy of every write. But safe data isn’t the same as a working service. Right now that shard is down: the follower is sitting there with a perfect copy and no authority to take a single write, and 20% of Hatch’s traffic is getting errors. Someone — or something — has to turn that follower into the new leader and send the traffic to it. That act is failover, and how fast it happens is the difference between a blip nobody notices and an outage that trends on social media.
Here is the whole stake in one comparison. If a human handles it, a pager fires, someone wakes, blinks, confirms the leader is really dead, promotes the follower by hand, and updates the config — and the shard is down for thirty-plus minutes. If an automatic controller handles it, the death is caught in seconds, a follower is promoted in seconds, traffic is repointed in seconds, and almost nobody notices. Same failure, same followers, wildly different outcome — decided entirely by the failover machinery you built before the outage. This page is that machinery, and the sharp edges (a leader that only looked dead, a promoted follower missing recent writes, two leaders at once) that make it harder than “just promote the follower.”
What failover actually does — three steps and a goal
Strip away the tooling and failover is always the same three steps. Detect that the leader is gone. Promote a follower to be the new leader. Repoint every client from the dead leader’s address to the new one. Do those three fast and correctly and the shard heals itself; get any of them wrong and you get a longer outage — or, as we’ll see, something worse than an outage. The single number that scores how well you do all three is your RTO — recovery time objective — the target for how long the shard may be unavailable after a failure. Failover is, in one sentence, the art of driving RTO toward zero without breaking anything.
One distinction to name up front, because it changes the difficulty. A switchover is planned — you’re patching the leader’s OS, so you promote a follower deliberately, at a calm moment, with the old leader cooperating. A failover is unplanned — the leader died on its own terms, maybe messily, maybe only half-dead, and nothing is cooperating. Planned switchovers are easy and worth doing often (they prove your failover path works). Unplanned failover is where all the hard problems live, and it’s what the rest of this page is about.
Detecting a dead leader — and the false-alarm trap
You can’t promote a follower until you’re sure the leader is gone — and “sure” turns out to be the hard word. The mechanism is simple: the leader emits a heartbeat, a tiny “I’m alive” signal, every second or so, and a watcher declares it dead when the heartbeats stop for longer than a timeout. The trouble is that a network can only tell you it hasn’t heard from the leader — it cannot tell you why. A leader that crashed and a leader that’s merely slow (a garbage-collection pause, a saturated disk, a brief network blip between it and the watcher) look identical from the outside: silence.
So the timeout is a genuine dilemma with a cost on both sides. Set it short and you fail over fast — but you’ll sometimes promote a follower while the “dead” leader is actually alive and still taking writes, which is the setup for the worst failure on this page. Set it long and you’ll never cry wolf — but the shard sits dead for that whole timeout before anyone reacts, and that wait lands straight in your RTO. Drag the timeout and feel the trade:
The interactive version of this example is available when JavaScript loads.
Promoting a follower — and which one you pick matters
Once you’ve decided the leader is gone, you promote a follower — you tell it “you’re the leader now,” it stops being read-only and starts accepting writes, and the replica set has a head again. If there’s only one follower, there’s no choice to make. But real replica sets keep two or three, and which one you promote is not a coin flip: promote the follower that is most caught up — the one with the smallest replication lag. That follower has the most of the dead leader’s recent writes, so promoting it loses the fewest of them.
Repointing the clients at the new leader
You’ve promoted a follower, but every client and every proxy in your system is still holding the dead leader’s address, cheerfully trying to connect to a corpse. Promotion is meaningless until traffic actually reaches the new leader, and the mechanism for that redirect — chosen long before the outage — sets how fast the cutover is. There are three common answers, and they differ by more than an order of magnitude. Flip between them:
The interactive version of this example is available when JavaScript loads.
The pattern to notice: the clients should never have to know a leader’s real address. Point them at a stable indirection — a virtual IP, a proxy, or a name they re-resolve — and failover becomes “swing the indirection,” a fast and central operation. Bake the leader’s literal address into a thousand clients and every failover becomes a thousand reconfigurations. Modern HA stacks (Patroni, Vitess, managed clouds) all put a proxy or service-discovery layer in front for exactly this reason.
The recovery budget — why automation wins
Now put the three steps end to end, because your RTO is simply their sum: detect + promote + repoint. This is where the manual-versus-automatic argument stops being a preference and becomes arithmetic. A human does each step in minutes — a pager fires, someone wakes and triages, promotes by hand, edits config — and the total is tens of minutes of downtime. A controller does each in seconds, and detection (the timeout) dominates what’s left. Toggle between them and watch the budget:
The interactive version of this example is available when JavaScript loads.
Split-brain — when the “dead” leader wasn’t
Here is the failure that makes failover genuinely hard, and it follows directly from “you can never be sure the leader is dead.” Suppose the leader wasn’t dead at all — it was just cut off by a network partition. Your controller sees silence, declares it dead, and promotes a follower. Now there are two leaders: the new one, and the old one that never got the memo and is still happily taking writes from whichever clients can still reach it. Both accept a write to the same key. When the partition heals, that record has two different values and no honest way to say which is correct. This is split-brain, and it is worse than an outage — an outage is downtime you recover from; split-brain is silent data corruption you may not even detect. Step through it:
The interactive version of this example is available when JavaScript loads.
Fencing — guaranteeing the old leader can’t write
The cure for split-brain is fencing: before (or as) you promote a follower, you guarantee the old leader can no longer accept writes — so even if it’s alive and partitioned, it can’t corrupt anything. There are two classic ways to do it. The blunt one is STONITH — “shoot the other node in the head” — literally power off or network-isolate the old leader through an out-of-band channel (an IPMI card, a cloud API, a smart PDU) so it is provably, physically unable to write. Brutal, but certain: successful fencing is the only way to be sure a leader is truly stopped rather than just quiet.
The subtler, more modern way is a fencing token: every time a new leader is anointed, it’s handed a number that only ever goes up — an epoch or generation. The new leader stamps that number on every write it sends to the shared storage (or the clients), and the storage is taught one rule: reject any write carrying a token older than the highest it has seen. Now if the deposed old leader wakes from its pause and tries to write, its token is stale, the storage refuses it, and the late write simply bounces. No brute force needed — the system makes a returning zombie leader harmless by construction. Toggle fencing on in the simulator above and watch the old leader’s write get rejected instead of diverging the data.
Who gets to declare the leader dead?
Fencing stops a zombie leader from writing, but it quietly assumes something we haven’t justified: that someone correctly decided to fail over in the first place. So step back and ask who that someone is. A single failover controller watching the leader is itself a single point of failure — if it dies or is partitioned, nobody fails over. Worse, if the network splits the cluster in half, each half might look at its side, see the other side as “dead,” and promote its own leader — and now you have split-brain caused not by a returning old leader, but by two halves that each acted in good faith.
The way out is to stop trusting any single observer and instead require a majority of them to agree before anyone is declared dead or promoted. If there are five voters and a promotion needs three of them to concur, then a partition can only ever give a majority to one side — the minority side, unable to reach three votes, refuses to promote and steps down. Two leaders become impossible. That is why production HA stacks lean on a small cluster of coordinators — etcd, ZooKeeper, or Consul (Patroni uses exactly this) — that hold the authoritative “who is leader” decision behind a majority vote.
Measuring and operating failover
Failover has a cruel property: the code path that matters most runs least often, so it rots silently and you only discover it’s broken during a real incident. Operating it well is mostly about refusing to let that happen.
What to track and practise
- Set explicit RTO and RPO targets, per datastore. “How long can this be down” and “how much can it lose” are product decisions, not accidents. Write them down; they drive your timeout, your sync mode, and your topology.
- Game-day your failovers on a schedule. Kill a leader in staging (and, when you’re brave, production) on purpose and time the recovery. A failover path that’s never been exercised is a failover path that won’t work when you need it — this is the single highest-value habit here.
- Alert on failover events and their duration. Every promotion should page after the fact with how long each phase took. A failover that succeeded but took 90 seconds is a problem forming; a flapping cluster (repeated promotions) means your detection is too twitchy.
- Watch the promoted follower’s lag at promotion time. That number is the data you lost (your realised RPO). If it’s ever non-trivial, your replication mode or follower placement needs attention.
- Verify fencing actually fences. Test that a partitioned old leader is truly blocked from writing — an untested fencing mechanism is worse than none, because it lulls you into running eager automation.
Where failover runs — the HA stacks
You rarely build failover by hand — you run a tool that does detection, promotion, repointing, and fencing for a specific database. They differ mostly in whether the orchestration is bolted on or baked in, but every one is the three steps plus a majority vote plus fencing:
| System | How failover works | The “who decides” layer |
|---|---|---|
| PostgreSQL + Patroni | Patroni agents watch each node and promote a standby on leader loss | A leader key in etcd / ZooKeeper / Consul held by majority vote |
| MongoDB | Replica-set members hold an election and promote a secondary automatically | Built-in — members vote; a majority is required to elect a primary |
| Redis | Redis Sentinel processes monitor the master and promote a replica | A quorum of Sentinels must agree the master is down |
| MySQL | Orchestrator (or Group Replication / InnoDB Cluster) detects and promotes | Orchestrator’s topology view; Group Replication uses a built-in Paxos-style vote |
| Managed (RDS / Aurora / Cloud SQL) | Multi-AZ standby is promoted automatically; DNS/endpoint repointed for you | The cloud control plane owns the decision — a checkbox to you |
The tell across every row is that same “who decides” column: none of them trusts a single watcher. Patroni leans on etcd, Sentinel needs a quorum of Sentinels, MongoDB and MySQL Group Replication bake the vote into the database itself. Recognise that shape and every HA product reads as a variation on one design — three steps, a majority to authorise them, and fencing to make it safe.
Pitfalls that bite in production
- Automatic failover with no fencing. The cardinal sin — the first partitioned-not-dead leader gives you two writers and corrupts data. Fence, or don’t auto-promote.
- A detection timeout tuned only for speed. Too short and routine GC pauses and network blips trigger needless failovers (and split-brain risk). Tune for the leader’s real pause behaviour, not the best case.
- An even number of voters (or just two nodes). Two nodes can’t form a majority when they disagree — each thinks the other died. You need an odd number, or a lightweight witness/arbiter, so one side always wins.
- Promoting the most-lagged follower. Promote the most caught-up one; promoting a laggard needlessly enlarges your data loss (RPO).
- Hard-coding the leader’s address in clients. Then every failover is a fleet-wide reconfiguration. Put a stable indirection (proxy / VIP / re-resolved name) in front.
- Never testing failover. The path runs rarely and rots silently. Game-day it, or assume it’s broken.
- Forgetting the old leader must rejoin as a follower. After recovery it has to resync from the new leader and come back read-only — not wake up thinking it’s still in charge.
Best practices
- Automate failover, but always with fencing (STONITH or fencing tokens) so a returning leader can never become a second writer.
- Require a majority to decide — run detection/promotion behind a quorum of observers (etcd/ZooKeeper/Sentinel), with an odd number of voters, so a partition can only ever promote one side.
- Tune the timeout to the leader’s real pauses — long enough to ride out ordinary GC/network hiccups, short enough to keep RTO low.
- Promote the most caught-up follower to minimise the writes lost on failover, and pair it with sync/semisync replication when RPO must be zero.
- Front the leader with a stable indirection (proxy / service discovery / VIP) so repointing is one fast, central swing.
- Set and monitor RTO/RPO targets, alert on every promotion and its duration, and watch for flapping.
- Game-day it regularly — planned switchovers and staged leader-kills prove the path works before a real 4 a.m. death tests it for you.
The interview deck — intern to principal
- What is failover, and how is it different from replication? (intern)
- Replication keeps a live copy of the data on a follower (durability). Failover is what happens when the leader dies: you promote a follower to leader and repoint traffic so the shard keeps serving (availability). Replication keeps your data; failover keeps your uptime.
- What are the steps of a failover? (intern → SDE2)
- Detect the leader is dead (missed heartbeats past a timeout), promote a caught-up follower to leader, and repoint clients at the new leader (via a proxy, virtual IP, or DNS). The total time is your RTO.
- Why is detecting a dead leader hard? (SDE2 → senior)
- Over a network you can’t distinguish “dead” from “slow” — a crash and a GC pause both look like silence. A short timeout fails over fast but false-alarms on transient pauses (risking split-brain); a long one is safe but lengthens the outage. You can never be certain, only careful.
- What is split-brain and how do you prevent it? (senior → staff)
- If the old leader was only partitioned (not dead) and keeps taking writes after a follower is promoted, you get two leaders and divergent data. Prevent it with fencing: STONITH (forcibly kill/isolate the old node) or a fencing token — a monotonic epoch stamped on writes that the storage rejects if stale — so the old leader can’t write.
- Which follower do you promote, and what does that cost you? (staff)
- The most caught-up one — smallest replication lag — because promoting it loses the fewest of the dead leader’s recent writes. That lost tail is your realised RPO. Synchronous/semisync replication drives it to zero; under async, follower choice is your lever.
- Who should be allowed to declare the leader dead? (principal)
- Not a single controller — it’s a SPOF and a partition could let two halves each promote their own leader. Require a majority of observers to agree (a quorum in etcd/ZooKeeper, or a built-in election), with an odd number of voters, so only one side can ever win. Making that agreement safe under a hostile network is consensus — the next topic.
The one thing to carry
The whole page collapses to a sentence: failover turns a follower into a still-serving leader — detect the death, promote the most caught-up follower, repoint the traffic, and fence the old leader — and the hard part is being safe when detection is wrong. Replication kept your data alive; failover keeps your service alive, and its score is RTO. Automate it, because a human at 4 a.m. is thirty minutes you don’t have — but never automate it without fencing, because an unfenced auto-failover meeting a merely-partitioned leader corrupts your data instead of saving it.
By GOAT Engineer. Back to Concept Library. Privacy Policy · Contact.