SPLIT BRAIN
Two leaders, one disaster
A 5-node cluster partitions into {N1,N2} (N1 leads) and {N3,N4,N5}. Clients keep writing to N1. With majority quorum enforced, what happens to those writes?
A cluster has one leader so that writes have one order. A partition cuts the leader off from most of its followers. The followers time out and elect a new leader; the old one, hearing nothing, keeps going. Now two nodes accept writes for the same data under different epochs. Both acknowledge. When the network heals, one history has to lose. The guard is that a leader must keep proving it is heard by a majority, and that storage must refuse any leader whose epoch is older than the newest it has seen.
// Leader, on client write:
append(write); // no quorum check
reply(ACK); // client believes it is durable
// Follower, on election timeout:
becomeLeader(); // nobody told the old leader// Leader, on client write:
if (replicateTo(majority) fails) reply(RETRY); // quorum
else reply(ACK);
// Leader, on heartbeat answered by < majority: stepDown();
// Storage, on write(epoch):
if (epoch < storage.epoch) reject(); // fencing
else { storage.epoch = epoch; apply(); } Watch the invariant strip. In TWO LEADERS it turns red the moment N3 wins its election while N1 is still accepting, and stays red through heal when w2 is discarded. In both fixes the same partition, the same election and the same client writes happen, and the strip stays green: the count of nodes accepting writes never passes 1. FIX: QUORUM models the check-quorum rule: N1 refuses writes it cannot replicate to a majority and steps down on a short heartbeat.
FIX: FENCING is green partly because of the schedule. Fencing protects storage from a stale leader once the new epoch is set. It does nothing for a write N1 acknowledges in the window between the partition and N3’s fence step: N1 has no quorum commit here, so that write is acked on 2 of 5 nodes and discarded at heal. That is why real systems combine the two layers: quorum commit so nothing is acknowledged without a majority, and fencing so a deposed leader cannot write.
This cluster never checks quorum and has no fencing, but with a healthy network nothing goes wrong. Inject the partition at the step where two nodes end up acknowledging writes and a client loses a write it was told succeeded.
GOAL: two nodes accept writes at once, or an acknowledged write is lost at heal
Storage receives every write tagged with the epoch of the leader that sent it. Implement acceptWrite so a leader from an older epoch than the newest one storage has seen is rejected, while a legitimate leader keeps writing. The grader replays a partition, a stale leader, and a re-election.
Database failover (MySQL with Orchestrator, Postgres with Patroni), ZooKeeper or etcd lock services, Kafka controller elections, any primary/replica pair with an automatic promotion script. Symptom: both sides served writes during an incident and the post-mortem has a section titled “data reconciliation”.
“What is split brain and how do you prevent it?” Name the two layers: majority quorum so at most one side can elect and commit, and fencing tokens (epochs) so a leader that failed to notice it lost cannot write. Mention the GC-pause case to show why quorum alone is not enough, and say honestly which acknowledged writes are lost at heal.
USED IN
Chapters that build on this lesson.
- Failover, split brain and fencingDatabases