Split Brain and Fencing
Overview
The scariest failure in leader-based systems is split brain: two nodes both believe they are the leader and both accept writes. The data then diverges and can be corrupted.
This lesson shows how it happens and the two defences that stop it.
Detailed Notes
What is split brain
Split brain means two parts of a system each think they are in charge. In a leader-follower setup, that means two leaders accepting conflicting writes at the same time.
The problem it solves
Failure detection is only a suspicion (Heartbeats chapter). Imagine the leader is not dead, only cut off by a network partition (a network failure that splits machines into groups that cannot talk). Followers stop hearing heartbeats and elect a new leader. Meanwhile the old leader, unaware, keeps accepting writes from clients that can still reach it. Now there are two leaders, each writing different data. Another version: the old leader freezes for 30 seconds (a long pause), wakes up, and still believes it leads.
How it works
Two defences work together:
1. Majority rule. A leader needs a majority of nodes behind it. In a 5-node cluster split 3 and 2, only the group of 3 can elect a leader. The group of 2 cannot reach a majority, so it must stop accepting writes. Because two majorities cannot exist at once, at most one side can lead.
2. Fencing tokens. Majority rule does not stop a paused old leader that wakes up and doesn't know it was replaced. So every leadership grant comes with an ever-increasing number, the fencing token (or term). The leader sends its token with every write, and the storage keeps the highest token it has seen and rejects anything lower.
Example: old leader A has token 33. A freezes. The cluster elects B with token 34, and B writes. When A wakes and writes with token 33, storage sees 34 was already used and rejects A's write.
Supporting tools:
- Leases: a leader's authority expires after a short time unless renewed, so a cut-off leader steps down by itself. Do not rely on clocks alone, use safety margins.
- Tie-breaker node: with two data centres, put a third small node elsewhere so a majority can exist.
- Decide in advance whether a minority side should stop completely or serve possibly stale reads.
The principle: enforce correctness where the data lives, not only where the decision is made.
A simple example
A night-shift manager (old leader) leaves for a nap, and a new manager takes over with badge number 34. When the first manager returns with badge 33 and tries to approve orders, the warehouse sees 34 is the latest and refuses.
System design example
A billing job runs on one leader node. During a partition the old leader pauses; a new leader gets token 13 and starts charging customers. When the old leader wakes with token 12 and tries to write results, the billing database has already seen 13 and rejects the writes. Customers are billed once even though the old node still believed it was in charge.
Summary
- Split brain = two leaders at once, usually caused by a network partition or a long pause.
- Majority rule means only one side of a split can elect a leader.
- Fencing tokens let storage reject writes from stale leaders.
- Leases, tie-breakers and up-to-date-log checks add more safety.
Architecture Diagram
Only the majority side leads, and storage rejects stale tokens.
- 1A network partition splits the cluster into a majority and a minority.
- 2Only the majority side elects a leader and receives a higher token.
- 3The leader sends writes with token 34 to storage.
- 4Any stale write from the old leader with token 33 is rejected.
Common Interview Questions
Chapter quiz
Leader and Follower · 5 questions
1.What is the main reason for having a single leader?
2.Which replication mode risks losing recent writes if the leader crashes?
3.How many failures can a 5-node majority cluster tolerate?
4.Why must a candidate have an up-to-date log to win an election?
5.What does a fencing token let storage do?
0 of 5 answered
Finished this lesson?
0 of 91 lessons done · 0% of the course

