Databases Bug Report

A network partition made both nodes of our active-passive pair act as primary

A switch failure cut the heartbeat link between two database nodes while both could still reach the application network. Each concluded the other was dead and took the primary role. For eleven minutes, writes went to both. How it happened, how we reconciled the data, and the quorum and fencing design that replaced it.

Owen Gallagher Owen Gallagher @owengallagher · November 28, 2026 · 7 min read · 0 Views
Banner: split brain where two nodes both believe they are primary

An internal service runs on a PostgreSQL pair in an older data centre: a primary, a hot standby, and a floating virtual IP (VIP) that applications connect to. Failover was managed by a small tool built on keepalived: the nodes exchange heartbeats over a dedicated link; if the standby stops hearing from the primary, it promotes itself and takes the VIP.

During routine maintenance, a top-of-rack switch carrying the heartbeat link rebooted unexpectedly. Both database nodes stayed connected to the application network through a second switch. Within seconds:

db-1 keepalived: VRRP_Instance(PG) Entering MASTER STATE           (already primary, no change)
db-2 keepalived: VRRP_Instance(PG) Received no advert for 3s, transition to MASTER
db-2 failover:   promoting standby: pg_ctl promote
db-2 postgres:   LOG:  received promote request
db-2 postgres:   LOG:  database system is ready to accept connections
db-2 keepalived: VRRP_Instance(PG) setting VIP 10.20.0.50/24 on eth0

db-2 could not hear db-1, so it concluded db-1 was dead, promoted itself and claimed the VIP. But db-1 was alive. It still had the VIP too. Both nodes answered ARP for 10.20.0.50, and the network switched between them as gratuitous ARP announcements arrived. Application connections went to whichever node the switch currently associated with the VIP.

For eleven minutes, until an engineer noticed two primaries in monitoring and stopped db-2, both nodes accepted writes. Neither replicated to the other.

The damage

-- on each node, after the incident
SELECT max(id), count(*) FROM orders WHERE created_at >= '2026-10-19 14:02';
db-1:  max(id) = 4419211   count = 1,804
db-2:  max(id) = 4418977   count = 1,233

Both nodes had issued order IDs from the same sequence range, because db-2 started from the last value it had replicated. 1,233 order rows on db-2 had IDs that also existed, for different orders, on db-1.

Two nodes cannot tell "dead" from "unreachable"

From db-2's point of view, "no heartbeats from db-1" has two possible explanations: db-1 has failed, or the link between them has failed. It cannot distinguish them. A two-node design must pick one interpretation, and either choice is wrong in some failure:

  • Assume dead and promote (what we did): a link failure produces two primaries, split brain.
  • Assume unreachable and wait: a real primary failure produces no primary at all.

This is a basic result in distributed systems: with only two participants, there is no majority to break the tie.

What was missing

Correct high-availability designs add two things:

  1. Quorum. A node may act as primary only if it can reach a majority of voters. With three voters, a partition leaves at most one side with two, so at most one side can elect a primary. The minority side knows it is in the minority and steps down.
  2. Fencing. Before a new primary takes over, the old one must be made unable to accept writes: powered off through its management interface (STONITH, "shoot the other node in the head"), its storage access revoked, or its network port disabled. Quorum stops a minority node from promoting; fencing deals with a node that is hung or partitioned and does not know it should stop.

We had neither. keepalived's VRRP is designed for moving a VIP between stateless routers, where two holders for a few seconds is a minor glitch. For a database, two holders means divergent data.

Environment

Item Value
Database PostgreSQL 15, streaming replication, 2 nodes
Failover keepalived VRRP + custom promote script
Heartbeat dedicated link through one switch
Split brain 11 minutes, about 3,000 writes across both nodes

1. Reconcile the data

We took db-1 as the source of truth (it had more writes and had never stopped being primary) and treated db-2's divergent writes as a separate batch to merge:

  1. Exported every row written on db-2 after its promotion, using created_at and updated_at and transaction IDs.
  2. For new orders, re-inserted them on db-1 with new IDs, keeping a mapping table from old to new IDs, and updated related rows (payments, line items) through the mapping.
  3. For updates to existing rows, compared with db-1's version and resolved by hand where both nodes had changed the same row (37 rows).
  4. Emailed customers whose order confirmation numbers changed.

It took two engineers most of two days. Then db-2 was rebuilt from db-1 with pg_basebackup.

2. Replace the two-node failover with a quorum-based manager

We moved the cluster to Patroni, which uses a distributed configuration store (etcd in our case) with three members as the source of truth for who is primary. The primary holds a leader key with a time-to-live and must renew it continuously:

# patroni.yml (excerpt)
scope: orders-db
etcd3:
  hosts: etcd-1:2379,etcd-2:2379,etcd-3:2379
bootstrap:
  dcs:
    ttl: 30
    loop_wait: 10
    retry_timeout: 10
    maximum_lag_on_failover: 1048576
    postgresql:
      use_pg_rewind: true

The rules that prevent split brain:

  • A node can become primary only by acquiring the leader key in etcd, which requires etcd to have a majority.
  • If the primary cannot renew its leader key (for example, it is partitioned from the etcd majority), Patroni demotes it to read-only before the key expires, so by the time another node can acquire the key, the old primary has already stopped accepting writes.

The three etcd members run in three separate failure domains (two racks plus a small witness VM in another room), so no single switch or rack failure leaves a majority on both sides.

3. Fencing with a watchdog

Demotion relies on the old primary's Patroni process working. If the node is hung, it might not demote itself in time. Patroni supports a Linux watchdog device: Patroni must keep resetting the watchdog while it holds the leader key, and if it stops (hung process, frozen node), the kernel reboots the machine:

watchdog:
  mode: required
  device: /dev/watchdog
  safety_margin: 5

With mode: required, a node will not become primary unless the watchdog is active. That gives us fencing without depending on an external power controller.

4. Route connections by role, not by a floating IP

Instead of a VIP that two nodes can claim at once, applications connect through HAProxy, which checks Patroni's REST API on each node and only routes write traffic to the node that answers 200 on /primary:

backend pg_primary
    option httpchk GET /primary
    http-check expect status 200
    server db-1 10.20.0.11:5432 check port 8008
    server db-2 10.20.0.12:5432 check port 8008

Even if two nodes briefly believed they were primary, only the one Patroni reports as leader receives traffic.

1. Recreate the original failure. In staging we cut the network between db-1 and db-2 while both could still reach two of the three etcd members:

db-1: still holds leader key, remains primary
db-2: replica, cannot reach db-1 for replication; does not promote (leader key held)

No promotion, because the leader key was still held and renewed.

2. Partition the primary from the majority. We isolated db-1 from etcd and from db-2, leaving it reachable only by its own HAProxy health check:

14:00:00 db-1: failed to update leader lock, demoting self
14:00:01 db-1: postgres restarted read-only
14:00:31 db-2: acquired leader lock, promoted

db-1 demoted itself before db-2 promoted. Writes sent to db-1 in the gap failed with cannot execute INSERT in a read-only transaction, which the application retried and then sent to db-2 through HAProxy.

3. Hung node. We froze db-1's Patroni process with kill -STOP. The watchdog rebooted the node after the safety margin, and db-2 took over after the leader key expired. At no point were there two writable primaries.

4. Monitoring. An alert fires if more than one node reports pg_is_in_recovery() = false, as a last line of defence.

5. Security implications. Split brain is also a data integrity and audit problem: two divergent histories of financial records. The reconciliation was documented for our auditors, and the new design's guarantee (one writable primary, enforced by quorum and fencing) is now part of our control documentation.

Takeaways

  • Two nodes cannot tell a dead peer from a broken link. Two-node automatic failover risks split brain.
  • Require a majority (quorum) to become primary, with voters in separate failure domains.
  • Fence the old primary (watchdog, power or storage fencing) so it cannot keep writing.
  • Route writes by verified role rather than a floating IP that two nodes can claim.

References: Patroni documentation, Patroni watchdog support, etcd failure modes, PostgreSQL high availability.

Add a label

#failover #high-availability #split-brain #quorum #fencing
Written by
Owen Gallagher @owengallagher

I am a site reliability engineer on a platform team that runs the edge for dozens of internal services: API gateways, reverse proxies, load balancers and the resilience settings everyone copies from each other. The incidents I learn most from happen when a safety mechanism does the wrong thing. A rate limit that locked out an entire office behind one IP, a circuit breaker that never opened, a liveness probe that restarted every pod during a database blip, two nodes that both decided they were primary. Each post explains what the mechanism was meant to do, why it misfired in our setup, how we changed the configuration and how we drilled the failure afterwards to prove it. I keep the examples close to real configs so you can compare them with your own.

Advertisement

Comments

0

No comments yet. Be the first to comment!

Sign In to post a comment.