Trade-offs in databases
medium 30 minEvery database trades freshness, speed, safety and uptime against each other.
See it run
Turn R and W on five copies, take a copy away, and see what each setting costs.
Operations
Balanced: 3 and 3
Majorities for both. The sets always meet.
Step 1 of 4
Five copies
| copy 1 | copy 2 | copy 3 | copy 4 | copy 5 | |
|---|---|---|---|---|---|
| value | v1 | v1 | v1 | v1 | v1 |
| write set | |||||
| read set |
All hold v1.
Step 1: Five copies
def write(value): acks = send_to_all(value) wait(acks >= W) # W = 3def read(): got = ask(R) # R = 3 return newest(got)- Reads
- Medium
- Writes
- Medium
- Survives
- 2 of 5 failures
Look at the worst case: the sets as far apart as possible.
In the real world
The same trade-offs in databases doing real jobs. Pick one and step through it.
Quorums overlap, and an update is lost all the same.
Step 1 of 4
Stock is 1
All three copies say so.
Step 1: Stock is 1
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Waiting for all copies
majorOne copy in maintenance stops every write.
WrongW = NRightW = N // 2 + 1Trusting overlap with concurrent writers
criticalTwo writers may both meet the quorum and one change vanishes.
Wrongwrite("stock", read("stock") - 1)Rightupdate_if("stock", expected=1, new=0)Confirming from one disk
criticalThe disk dies and a paid order is gone.
Wrongplace_order(w=1)Rightplace_order(w="majority")Writing the level into the query text
minorIn Cassandra the level is sent by the driver, not written in the SELECT.
WrongSELECT * FROM usersUSING CONSISTENCY QUORUM;RightCONSISTENCY QUORUM;SELECT * FROM users WHERE id = 7;
Interview tips
How to explain it
- N copies, W confirm, R answer.
- R + W > N makes the sets meet.
- Choose for each operation.
Memory trick: RWN
- Readers and Writers
- must meet in the Network.
- Two circles that overlap.
Go safer for
- Orders and payments.
- Stock at checkout.
- Anything confirmed to a customer.
Go faster for
- Views and likes.
- Reviews.
- Metrics.