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 levelFree 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.
- No broken or illegal connections
- The database machines can always agree on one primary
- Use Coordination
- No component is overloaded
- After any one machine crashes and failover ends, under 1% of requests fail
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?
- Speed or cost
- Refuse some requests, or risk copies that disagree
- Reads or writes
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?
- 3 is cheaper than 2
- With 3, two still form a majority if one fails
- Databases need an odd number of disks
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?
- When the leader saves it
- When a majority of machines have it
- When every machine has it
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.