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.
- Detect: health checks or heartbeats miss for long enough (interval × threshold).
- 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.
- Promote: the chosen standby stops replaying, applies what it has, and opens for writes. The controller picks the most up-to-date standby.
- 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.
- 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.