Architecture

Split-Brain in Distributed Systems: Causes, Damage and Prevention

By NileVPS Team · Published · 5 min read

اقرأ المقال بالعربي

Two database servers are set up as a high-availability pair. The network link between them drops for forty seconds. Each server sees its partner go silent, concludes the partner has died, and promotes itself to primary. Clients on both sides of the break keep writing. When the link comes back, you have two databases that each believe they are the source of truth, with different orders, different balances and different user records. This is split-brain, and it is one of the most expensive failures a distributed system can suffer, because the damage is silent until someone notices the numbers do not add up.

What split-brain actually is

Split-brain happens when a cluster that is supposed to have exactly one leader ends up with two or more, each accepting writes independently. The root cause is almost always a network partition: nodes are alive and healthy, but they cannot talk to each other. From inside one node, "my peer crashed" and "the cable between us is broken" look identical. Both produce missed heartbeats.

Systems that are affected include:

  • Primary/replica databases with automatic failover (PostgreSQL with Patroni, MySQL with Orchestrator, Redis Sentinel).
  • Shared-storage clusters (Pacemaker/Corosync, DRBD) where two nodes might mount the same filesystem.
  • Leader-elected services such as schedulers, cron runners and queue consumers that must run on one node only.
  • Floating IP setups using keepalived and VRRP, where both nodes can claim the virtual IP.

Why it is so damaging

Divergent writes cannot be merged automatically in most cases. If node A accepted order #1001 for customer X and node B accepted a different order #1001 for customer Y, there is no correct answer a script can compute. Someone has to reconcile by hand, often by replaying logs and contacting customers. With shared storage, two nodes writing to the same block device without coordination can corrupt the filesystem outright.

The core defence: quorum

The standard rule is that a node may act as leader only if it can reach a majority of the cluster. With n voting members, a majority is floor(n/2) + 1.

Cluster sizeMajority neededFailures tolerated
220
321
431
532

Two lessons come straight out of this table. First, a two-node cluster cannot safely survive any partition on its own, because neither half has a majority. Second, even numbers add cost without adding tolerance; four nodes tolerate the same single failure as three. That is why etcd, Consul, ZooKeeper and Patroni deployments almost always use three or five members.

During a partition, the side with the majority keeps working and the minority side stops accepting writes. Losing availability on the small side is the price of never having two truths.

Witness nodes and tie-breakers

If you only want to pay for two full database servers, add a small third voter that stores no data. Examples include a qdevice for Corosync, a lightweight etcd member for Patroni, or a third Sentinel process for Redis. Place it in a different location from both main nodes, so a single link failure cannot isolate it together with one side. A small VPS in a second region works well as a witness; see /pricing for sizes.

Fencing: making sure the old leader is really gone

Quorum decides who should be leader, but the old leader may not know it has been demoted. A paused VM or a long garbage-collection pause can wake up and keep writing. Fencing stops that:

  • STONITH ("shoot the other node in the head"): power off or reset the losing node through an IPMI or cloud API before promoting a new one.
  • Fencing tokens: every new leader gets a strictly increasing number, and storage rejects writes carrying an older token.
  • Self-fencing: a node that loses contact with the quorum demotes itself or stops its service. Patroni does this when it cannot renew its leader key in the distributed store.

Lease-based leadership depends on reasonable clocks, so keep time in sync; our article on clock skew covers chrony setup.

Practical checklist

  1. Use an odd number of voters: three or five.
  2. Never run two-node automatic failover without a witness or fencing.
  3. Spread voters across failure domains (different hosts, ideally different locations).
  4. Configure your tool to refuse writes when quorum is lost. For example, in Redis set min-replicas-to-write 1 so an isolated primary stops accepting writes.
  5. For keepalived, add a health-check script and avoid relying on VRRP alone for anything stateful.
  6. Monitor for two primaries. A simple check queries every node with SELECT pg_is_in_recovery(); and alerts if more than one returns false.
  7. Test it. Block traffic between nodes with iptables -A INPUT -s <peer-ip> -j DROP in a staging cluster and watch what happens.

Recovering from a split-brain

If it does happen: stop writes on all but one node immediately, take snapshots of every side before touching anything, pick the side with the most important data as the survivor, then export and manually reconcile the rows written on the other side during the split window. Afterwards, rebuild the losing node as a fresh replica rather than trying to rejoin it as is.

FAQ

Can split-brain happen with only two servers?

Yes, and two-node clusters are the most exposed setup. Neither server can form a majority alone, so without a witness or fencing, a broken link leaves each one to decide independently, and both may promote themselves.

Does quorum mean I lose availability during a partition?

On the minority side, yes. Nodes that cannot reach a majority stop accepting writes. The majority side continues serving traffic, which is usually the right trade-off for data you cannot afford to duplicate or lose.

Is split-brain only a database problem?

No. Any component that must have a single active instance can suffer from it, including job schedulers, payment workers, cron runners and services holding a floating IP. If two copies running at once would cause duplicate actions, you need leader election with quorum.

Related guides