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.
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?
There are two separate fixes. Raise the timeout above normal blips, perhaps to 30 seconds, and accept a longer outage during real failures. Then make a nearby follower semi-synchronous and promote only a follower that holds every acknowledged write, accepting slightly higher commit latency. Also make sure the old leader is fenced as part of promotion, and save its unreplicated writes during recovery so someone can review them rather than silently losing them.
Use this as a guide for self review; written answers are not graded.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.