System designCore8 min

Failure detection & split brain

How machines decide that another one is dead when they can never be sure, what happens when two of them both think they are in charge, and the simple numbers that stop the wrong one.

1 · The problem

Sometimes the network splits machines into groups that cannot hear each other

A city library runs its lending system on a primary database in the main building, with a standby copy in a branch across town, ready to take over if the primary dies. One afternoon a construction crew cuts the cable between the two buildings.

Main deskPrimarymain buildingStandbybranch libraryBranch desklink cutMain deskPrimarymain buildingStandbybranch libraryBranch desklink cut

Both databases are running perfectly. Each simply can no longer hear the other. From the standby's side, a cut cable and a dead primary look the same.

This is a network partition: machines are split into groups that can talk within the group but not across it. It is not a crash; everyone is still running. Partitions come in awkward shapes too:

  • Partial: A can reach B and B can reach C, but A cannot reach C.
  • One-way: A hears B, but B cannot hear A. A faulty network card that drops incoming packets but still sends outgoing ones does exactly this.

Partitions are not rare exotic events. Links fail every day in ordinary data centers (see Why distributed systems are hard), and any system with more than one machine has to decide what to do when one happens.

2 · Heartbeats

A heartbeat says "I'm alive"; silence is only a guess

The standard way to watch another machine is a heartbeat: a small message it sends every second or so. If the heartbeats stop for longer than a timeout, you declare it dead. The library's standby does exactly this:

The standby cannot know whether the primary crashed, the link failed, or the primary is merely paused. The timeout turns uncertainty into a decision.

Timeout

Real failures

False alarms

Short (a second or two)

Noticed quickly: short outage

Frequent: a busy moment or a brief network hiccup looks like death

Long (a minute)

Noticed slowly: a long outage

Rare: only real silence trips it

There is no timeout that is both fast and never wrong. A smarter detector learns how regular the heartbeats usually are, and instead of a yes-or-no answer reports a suspicion level: how unlikely this much silence would be if the machine were alive. Each system then picks its own threshold, for example "act when there is less than a 1-in-1,000 chance that it is still fine".

3 · At scale

In a big cluster, news spreads faster by gossip than by roll call

Two machines can simply watch each other. A cluster of 1,000 cannot: if every node heartbeats every other node, that is 1,000 × 999, about a million messages each round, and the load grows with the square of the cluster.

Everyone checks everyone
999,000 messages per round
Each checks one at random
1,000 messages per round

1,000 nodes. With random checks, each node sends a steady trickle however large the cluster grows.

So large clusters use gossip instead:

  • Check a random neighbor. Each round, every node pings one other node chosen at random.
  • Ask for a second opinion. If it gets no answer, it asks a few other nodes to try. Maybe only its own link is bad.
  • Suspect before declaring dead. A node that still fails is marked suspected, and it gets a short window to answer "I'm alive" before the verdict.
  • Spread the news like a rumor. Every node passes what it has heard to a few others, who pass it on. The news reaches all 1,000 nodes in a handful of rounds, because each round the number of informed nodes multiplies.

4 · Split brain

Two machines both believe they are in charge

Back at the library. The standby's timeout fires, so it promotes itself to primary, which is exactly what it was built to do. But the old primary was never dead:

Each side accepted writes the other never saw. One copy of the book is now lent to two people, and there is no automatic way to decide which record is right.

This is split brain: two nodes each acting as the leader. Every system with automatic failover is exposed to it, and the damage is not a pause but conflicting data. It happens to large companies too: in 2018 a 43-second network cut made one large code-hosting site's tooling promote a new primary database in another region. Both sides then held writes the other lacked, and putting them back together took over a day.

A node that cannot prove it is still in charge must stop acting as if it is.

5 · The fix

Majorities and fencing tokens stop the old leader

Two ideas, usually used together, make split brain impossible rather than unlikely.

1. Only a majority may act. Run an odd number of nodes, say three: the primary, the standby, and a small third witness somewhere else. A node may lead only while it can reach a majority, two of the three. When the cable is cut, at most one side can have two. The other side knows it is in the minority and stops accepting writes.

With only two nodes, neither side of a cut has a majority, which is why the third one matters. How nodes agree on a leader this way is Consensus & leader election.

2. Stamp every leader with a number. Each time a new leader is chosen, it gets a number higher than any before: a fencing token. It sends that number with every write, and the storage remembers the highest number it has seen and refuses anything lower. An old leader that wakes up late cannot do damage:

The old leader never learned it was replaced. It does not need to: the storage checks the number on every write.

The bluntest version of the same idea has a memorable name, STONITH: "shoot the other node in the head". Before taking over, the new leader cuts the old one's power or network access, so it cannot write anything at all.

Check yourself

5 questions

1. The standby stops receiving heartbeats from the primary. What does it know?
2. You shorten the failure timeout from 30 seconds to 1 second. What happens?
3. Why do large clusters spread failure news by gossip rather than having every node check every other?
4. A two-node primary/standby pair is cut in half by a partition. Why is that dangerous, and what helps?
5. An old leader with fencing token 33 wakes from a long pause and sends a write. The storage has already accepted writes with token 34. What happens?

Takeaways

Remember this

  • A partition leaves machines running but unable to hear each other, sometimes only partly or in one direction.
  • Heartbeats and timeouts turn silence into a decision; short timeouts are fast but raise false alarms.
  • Large clusters check random neighbors and spread the news by gossip, keeping each node's load steady.
  • Split brain is two nodes both leading and accepting conflicting writes; a node that cannot prove it leads must stop.
  • Majorities (an odd number of nodes) and fencing tokens (ever-rising numbers the storage checks) stop the old leader.