Bugpit

Lessons › Core concepts

Who's in charge?

Your database has a primary and one standby. When the network between them broke, both thought they were in charge and accepted writes, and the data no longer agreed. 5,000 req/s arrive.

Play this level

Free with an account. It runs in your browser: build the design, press Run, and watch what breaks.

The lesson

The CAP theorem says that while the network is split, a system must choose: refuse some requests to stay consistent, or answer everything and risk copies that disagree. You can't have both during the split.

Consensus is how machines agree anyway. Protocols like Raft and Paxos elect a leader by majority vote, and a write only counts once a majority has it.

A majority needs an odd number of machines. With 3, one can fail and 2 still agree. With 2, a split leaves 1 and 1, and neither side can safely take charge, which is how you end up with two primaries.

What you have to achieve

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

What you learn

Three database machines and a coordination service mean only the side with a majority can lead. A broken network now costs a little availability, never a second primary.

Check yourself

The network splits your database machines in two. What does CAP say you must choose?

Show the answer

Refuse some requests, or risk copies that disagree. During a split, a side that can't reach the others must either stop answering, staying consistent, or carry on and risk disagreeing.

Why run 3 database machines rather than 2?

Show the answer

With 3, two still form a majority if one fails. A leader needs a majority. With 2 machines a split leaves 1 and 1, and neither has a majority.

In Raft, when does a write count as saved?

Show the answer

When a majority of machines have it. A majority is enough to survive a minority failing, and waiting for every machine would stop all writes when one is down.

Try it yourself

More in Core concepts

← Built for writes All or nothing →