Three copies, no leader
A shopping cart is kept on 3 machines, and none of them is in charge: any of them takes a write. Right now a write counts once 1 machine has it, and a read asks 1 machine. People add an item, reload, and the item is gone.
Play this levelPart of Bugpit Pro. It runs in your browser: build the design, press Run, and watch what breaks.
The lesson
So far one primary took every write. Stores such as Cassandra, and Amazon's original Dynamo, have no primary. Each piece of data lives on N machines, here 3, and any of them accepts a write. There's no failover to wait for, because there's no leader to lose.
You choose two numbers. W is how many copies must confirm a write. R is how many copies a read asks. With W = 1 and R = 1, a write can land on one machine while your read asks another, and you get old data.
The rule is R + W > N. Then the machines you read from always overlap the machines that took the write. At least one of them has the latest value, and the read returns the newest it sees.
Don't go to the top, though. With W = 3, one dead machine blocks every write. With N = 3, setting W = 2 and R = 2 gives you both: fresh reads, and everything still works with one machine down.
Two people can still write the same key at the same moment on different machines. Simple stores keep the write with the later timestamp, called last write wins, which silently drops the other. Others keep both and let the app merge them.
DynamoDB hides these dials and offers a choice on each read instead. This level shows them because the rule comes up in interviews. Speed isn't simulated: in real stores, higher W and R are slower.
What you have to achieve
6,000 requests a second at the busiest hour, 50% of them writes.
- No broken or illegal connections
- Every read sees the latest write
- Reads and writes still work with one copy down
- No component is overloaded
- Fewer than 0.1% of requests fail
- p99 response time under 200 ms
What you learn
W = 2 and R = 2 out of 3: every read overlaps the latest write, and one machine can die without anyone noticing. It's the majority idea again, used here for data instead of for choosing a leader.
Check yourself
N = 3, W = 1 and R = 1. Can a read miss the latest write?
- No, never
- Yes: it may ask a copy the write hasn't reached yet
- Only when a machine is down
Show the answer
Yes: it may ask a copy the write hasn't reached yet. 1 + 1 isn't more than 3, so the copy you read needn't be the copy that was written.
N = 3. Which setting gives fresh reads and still works with one machine down?
- W = 3, R = 1
- W = 2, R = 2
- W = 1, R = 1
Show the answer
W = 2, R = 2. 2 + 2 is more than 3, and both reads and writes need only 2 of the 3 machines.
N = 5 and W = 3. What's the smallest R that guarantees fresh reads?
- 1
- 2
- 3
Show the answer
3. R + 3 must be more than 5, so R is at least 3.
Two people update the same key at once on different machines, and the store uses last write wins. What happens?
- Both updates are kept
- One update is silently dropped
- The store refuses both
Show the answer
One update is silently dropped. Only the later timestamp survives, and nobody is told. Fine for a profile photo, risky for a cart or a balance.