SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
  • Challenges of Distribution
  • Failure Types: Crash vs Byzantine
  • Network Partitions
  • Split Brain Problem
  • Heartbeats
  • Failure Detectors
  • Handling Failures
  • Consistency Models
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Distributed Systems/Distributed System Fundamentals

Split Brain Problem

Two parts of a system both think they are in charge, and both accept writes.

⏱ 12 min readIntermediateInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

Split brain happens when a system that should have one leader ends up with two, and both accept writes. The two "brains" then make different decisions and the data grows apart.

  • It usually starts with a network partition or a long pause of the leader, so the others think it is dead and pick a new one.
  • The old leader does not know it was replaced, so it keeps working.
  • It is stopped with majority votes, terms or epochs and fencing tokens.

02Concept: What is it?

Many systems use a leader (also called primary or master) to decide the order of writes. Having one leader keeps things simple: everyone follows one source of truth.

The followers watch the leader with heartbeats. If the heartbeats stop, they assume the leader died and elect a new one. The trouble is that "the heartbeats stopped" does not mean "the leader died". Maybe the network is cut. Maybe the leader was frozen by a long garbage-collection pause. In both cases the old leader is still alive, and when it wakes up it still believes it is the leader.

How split brain happens
Before
👑Leader Aone writer
🗄️Bfollower
🗄️Cfollower
Split brain
👑Leader AX = 1
↔✂ cut
👑Leader BX = 2
💥Two truthsdata diverges
Two leaders write different data at the same time. Later there is no single correct history.
🧠The old leader never knows
A leader can not tell "I am the leader" from "I was the leader a minute ago". Its belief is only a memory. A safe design never depends on the leader noticing by itself.

03Real-Life Example

🍵Two Captains on One Ship
A ship has a captain. During a storm the radio between the captain and the officers fails. The officers wait, hear nothing, and choose a new captain. But the first captain is still on the bridge and does not know about the vote. Now one ship has two captains giving orders: one says "turn left", the other says "turn right". The crew follows both and the ship goes in circles. To prevent this, the rule could be: "A captain only gives orders if a majority of officers sign them", and "each captain carries a numbered badge, and crew ignores any badge older than the newest they have seen."
The ship
Split brain
Captain
Leader (primary) node
Radio fails
Network partition or leader pause
Officers pick a new captain
Automatic failover and election
The old captain does not know
The stale leader still accepts writes
Both captains give orders
Two leaders, conflicting writes
Orders need a majority of signatures
Quorum-based writes
Numbered badges
Terms, epochs and fencing tokens
Fencing keeps the old leader out
👑Old leadertoken 1
→↓write, token 1
🛡️Storagehas seen token 2
→↓rejected
👑New leadertoken 2

04Why Do We Need It?

💥It corrupts data
Two leaders write different values to the same record. Some of those writes can never be merged cleanly.
💸It costs money
Two leaders may both charge, ship or book the same thing.
🕵️It is quiet
Nothing crashes. The damage can sit unseen for hours or days.
🔁Failover causes it
The very tool that gives you high availability, automatic failover, is also what can create it.
🧰It has known fixes
Quorums, epochs and fencing are well understood. You only have to use them.
🎯It is a classic interview topic
It tests if you understand leaders, failure detection and consensus together.
Having a leader is simple, but replacing a leader is dangerous. Split brain is what happens when we replace one that was not really gone.

05Where Do We Use It?

🗄️Primary-replica databases
MySQL or PostgreSQL setups with automatic failover can promote a replica while the old primary still runs.
🔒Distributed locks
Two clients both think they hold the lock, usually after a pause.
🧮Clusters with a master
Search, file-system and queue clusters have one coordinating node.
🏢Active-passive data centres
Failing over to a standby site while the main site is only cut off.
🔌Two-node HA pairs
With only two nodes, neither can prove it holds a majority.
☁️Cloud auto-scaling groups
A controller may start a replacement for an instance that was only unreachable.

06Types / Parts / How It Works

Common causes

CauseWhat happensWho is surprised
Network partitionLeader is cut off from the followers, who elect a new oneThe old leader, still serving its own clients
Long pause (GC, swap, VM freeze)Leader stops for seconds, then continues as if nothing happenedThe old leader, which wakes up still sure it leads
Wrong failure detectorA timeout is too short, so a slow leader is declared deadEveryone, as the cluster flaps between leaders
Manual failover mistakeAn operator promotes a replica without stopping the old primaryThe team, a day later
Two nodes onlyEach node can not tell if the other is dead or cut offBoth nodes, which both take over

How to prevent it

Three lines of defence
Quorum (majority)
🗳️Need > halfto be leader or to commit
🧮Odd cluster size3 or 5 nodes
Terms / epochs
🔢Each leader has a numberterm 1, term 2...
🚫Old term rejectedby followers
Fencing / STONITH
🎟️Token on each writestorage rejects old ones
🔌STONITHpower off the old node
Needs a majority Numbers the leaders Blocks the old leader
Real systems often use more than one.
DefenceHow it worksLimit
Quorum writesA write counts only if a majority acknowledge itThe minority side can not write, so it is unavailable
Terms / epochsA new leader gets a bigger number. Nodes ignore messages from smaller numbers (Raft, Paxos)Needs a consensus system to hand out the numbers
Fencing tokenA lock or lease returns an increasing number. The storage refuses lower numbersThe storage must check tokens
Lease with expiryA leader may act only until its lease ends, and must renew itClocks must not drift much, and a pause can still outlive the lease
STONITH (shoot the other node in the head)Power off or isolate the old node before promoting a new oneNeeds extra hardware or cloud API access
A witness / tie-breakerA third small node that breaks 1-vs-1 tiesOne more thing to place and run
VisualizerTwo leaders: watch it happen, then stop itStep through the story with no protection. Then switch to majority votes and fencing tokens and compare the result. Also try the frozen-leader version.
What goes wrong?
What protects the system?
🗄️Aleader, term 1
🗄️Bfollower
🗄️Cfollower
🗄️Dfollower
🗄️Efollower
Step 1 of 5
Everything is healthy
A is the leader (term 1). The other nodes follow it and watch its heartbeats. All writes go through A.
Leaders accepting writes
2
at the end
Conflicting writes
Yes
Data safe
No
Price paid
Corruption
found much later
With no protection both leaders accept writes, so two copies of the truth grow apart. This is split brain.

07How to Use It / Steps / Implementation

Avoiding split brain

  1. 1
    Use a consensus-based system
    Let a system such as etcd, ZooKeeper or Raft elect leaders. They are built so there is never more than one leader per term.
  2. 2
    Require a majority
    A node may lead or commit only if more than half of the cluster agrees. Use 3 or 5 nodes, spread over three zones.
  3. 3
    Number every leadership
    Give each new leader a larger term or epoch. Followers refuse anything from an older term.
  4. 4
    Fence the old leader
    When a lock or lease is granted, include an increasing token. The resource (storage, database) must reject writes with an old token.
  5. 5
    Use leases with care
    A leader acts only while its lease is valid, and stops early when unsure. Remember that a long pause can still run past the lease, so combine it with fencing.
  6. 6
    Do not fail over with two nodes alone
    Add a third node or a witness. Otherwise one side can not tell a dead partner from a cut link.
  7. 7
    Practise it
    Pause the leader, cut the network and check that only one side can write. Write a runbook for manual fixes.
💡Safety first, then speed
Slow failover is annoying. Fast but wrong failover loses data. Prefer a safe detection time (a few seconds) over reacting to the first missed heartbeat.
⚠️Leases are not enough alone
A lease says the leader may act for 10 seconds. If the leader freezes for 15 seconds and then writes, it still thinks the lease is valid. Only a check at the storage (a fencing token) stops that write.

08Key Terms to Know

TermMeaning in easy words
Split brainTwo nodes (or groups) both act as the leader at the same time
Leader / primaryThe node that decides the order of writes
FailoverMoving the leader role to another node when the leader seems to fail
Term / epochA number that increases with each new leader
Fencing tokenAn increasing number given with a lock, so storage can reject writes from an old holder
LeaseA permission that expires after a set time unless it is renewed
QuorumA majority of nodes, needed to take a decision
STONITH'Shoot the other node in the head': forcing the old node to power off
Witness / tie-breakerAn extra node used only to settle votes
Stale leader (zombie)An old leader that does not know it has been replaced
Failure detectorThe part that decides, from heartbeats, if a node is down

09Pros, Cons and Trade-offs

✅ Pros
  • Majority rules guarantee at most one leader per term
  • Fencing protects data even when a node pauses
  • Automatic failover keeps downtime short
  • Terms make leader changes easy to reason about
  • Well-known tools already implement the hard parts
⚠️ Cons
  • The minority side can not write, so some users see errors
  • Needs at least three nodes or a witness
  • Fencing needs support from the storage layer
  • Leases depend on clocks being roughly right
  • Misconfigured failover can still cause split brain

Trade-offs

ChoiceWe gainWe lose
Quorum required to writeNo two leaders can both commitNo writes on the minority side
No quorum (any node may lead)Always availableSplit brain and conflicting data
Fast failure detectionShort downtimeMore false alarms and needless failovers
Slow failure detectionFewer false failoversLonger outages when a leader really fails
Fencing tokensOld leaders can not corrupt dataStorage must be able to check them
STONITHCertain that the old node is offNeeds special hardware or access, and a bug can kill the wrong node

10Common Problems and Mistakes

Common mistakes

  1. Two-node clusters with automatic failover. Each side can promote itself, because it can not prove the other is gone.
  2. Trusting the leader's own belief. A paused leader wakes up and keeps writing. The check must be on the receiving side.
  3. Relying on timeouts alone. A timeout only says "I did not hear from you", not "you are dead".
  4. Distributed locks without fencing. A client with an expired lock still writes, after a pause.
  5. Manual promotion without stopping the old primary. Both stay writable.
  6. Even-sized clusters in two zones. A zone cut leaves no majority, or leaves each side thinking it has one.
  7. Ignoring clock jumps. A lease that depends on time fails when a clock moves.

Things to watch

🧟Zombie leader
An old leader keeps serving reads (stale) or writes after it was replaced.
🔁Flapping
Leadership changes again and again because the failure detector is too sensitive.
🧩Diverged data
After a split brain, copies differ. Someone must pick or merge, often by hand.
📉Silent damage
Nothing alerts. Add checks for 'more than one leader' in your monitoring.

11Real-World Examples

🗄️etcd and ZooKeeper
Use majority votes so a minority group can never elect a leader.
🍃MongoDB
A primary that loses contact with the majority steps down, so the majority side can safely elect a new one.
🔎Elasticsearch
Older versions could elect two masters. Newer versions use a voting configuration with a majority to prevent it.
🐘PostgreSQL HA tools
Tools such as Patroni keep the leader key in a consensus store and demote a primary that loses it.
🔒Lock services
Systems like Chubby and ZooKeeper-based locks hand out increasing numbers (fencing tokens) so storage can reject stale holders.
📚Well-known incidents
Public post-mortems from large companies describe a failover that promoted a second primary and caused hours of data repair.
📝Note
Details such as defaults and election rules change between versions. Read the product's docs for the exact guarantees.
Mini case: why three nodes beat two
🗄️2 nodes1 | 1 split
→↓no majority
🤷Both promote?or both stop
→↓add a 3rd node
🗄️3 nodes2 | 1 split
Unsafe Safe
With three nodes, a 1-vs-2 split always has a winner. With two nodes it has none.

12Related Topics to Read Next

Network Partitions→

The usual way a split brain begins.

Heartbeats→

The signals that, when missed, trigger failover.

Leader Election→

How a new leader is chosen safely.

Raft→

A consensus algorithm that uses terms and majorities to prevent two leaders.

Distributed Locks→

Where fencing tokens matter most.

Consensus Algorithms→

The general tool for agreeing on one leader.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • ✔What: two leaders at once, both accepting writes, so the data diverges.
  • ✔Causes: network partition, long pause of the leader, jumpy failure detector, bad manual failover.
  • ✔Key fact: the old leader does not know it was replaced.
  • ✔Quorum: need a majority to lead or commit. Two majorities can not both exist.
  • ✔Terms / epochs: bigger number wins. Ignore messages from older terms.
  • ✔Fencing token: storage rejects any write with a lower token. Leases alone are not safe.
  • ✔Cluster: 3 or 5 nodes over three zones. Never auto-fail-over with just two.
  • ✔One line: “A leader is only a leader if a majority says so, and the storage double-checks.”
← Previous
Network Partitions
Next →
Heartbeats