Video in progress
How does split brain occur during automatic failover in leader-follower replication, and how do you prevent it?
When the network splits, the old leader stays alive but the others consider it dead and elect a new leader — two nodes accept writes at the same time and the data diverges. To prevent it you need a quorum-based election, shutting down the old leader (fencing), and accepting writes only when a majority confirms them.
📖 Answer
The scenario: primary A and replica B, with a monitor that manages failover. The network between A and the monitor goes down, but A is still in contact with the applications. The monitor considers A dead and makes B the primary. Now some applications write to A and some to B. When the network recovers there are identical IDs and conflicting balances — you'll need a manual rescue.
What people do:
Quorum. The failover decision is made not by a single monitor but by an odd number of observers with a majority vote (Redis Sentinel, Patroni through etcd). In a network split into two sides, only the majority side can appoint a new leader.
The old leader shuts itself down. In Patroni, the primary regularly renews the leader key in etcd. If it can't renew it (no contact with etcd), it makes itself read-only. That is, the truth about "who is the leader" lives in one consensus system.
Fencing. When a new leader is elected, forcibly stop the old primary (STONITH — "shoot the other node in the head"), or reject the old leader's writes at the storage layer with an epoch/term number.
On the client side, applications shouldn't guess the primary's address themselves but get it from a single source (DNS, a proxy, HAProxy health check through pg_is_in_recovery()).
One more thing: with asynchronous replication, the failover itself can lose the last writes — even without split brain. These are two separate problems, mention both in an interview.
Sign up to mark studied questions and save your progress.