Bugpit

Lessons › Keep it running

Two primaries

Your database has two machines: a primary and a standby. The network between them drops for a minute. The standby can't see the primary, decides it's dead, and makes itself primary. Now two machines both accept writes, and their data disagrees.

Play this level

Part of Bugpit Pro. It runs in your browser: build the design, press Run, and watch what breaks.

The lesson

This is split brain. From the inside, a crashed machine and an unreachable machine look exactly the same: no answer. With two machines, neither can tell which it is.

The fix is to vote. A machine may only act as primary while it can reach a majority of the group. This majority is a quorum. With 3 machines, 2 are a majority, so the side with 2 carries on and the lone machine stops taking writes.

That's why these groups have an odd number. 4 machines still need 3 to agree, so they survive one failure, the same as 3 do, and they can split 2 against 2 with no majority at all.

Click **Primary DB** and set the machines to 3. Real systems use an algorithm such as Raft or Paxos for the vote, often through a tool like etcd or ZooKeeper. Some run two database machines and a small third machine, a witness, that holds no data and only votes. That's still three voters.

What you have to achieve

6,000 requests a second at the busiest hour, 5% of them writes.

What you learn

With 3 machines there is always exactly one side with a majority, so there is always at most one primary. This is leader election, and the same idea sits under distributed locks, Kafka, Kubernetes and every "highly available" database.

Check yourself

A group of 5 machines is split by a network fault into 3 and 2. Which side keeps taking writes?

Show the answer

The side with 3. 3 of 5 is a majority. The side with 2 knows it can't reach one, so it stops, and the data stays in one piece.

How many machines can fail in a group of 5 before it stops working?

Show the answer

2. It needs 3 to agree. Lose 2 and 3 are left. Lose a third and no majority is possible.

An old primary wakes up after a long pause and still thinks it's in charge. What stops it from writing?

Show the answer

Each new leader gets a higher number, and storage refuses writes carrying an older one. This is a fencing token. Clocks and good intentions aren't enough; the storage itself rejects the stale leader.

Try it yourself

More in Keep it running

← The release that broke everything Someone else's limit →