The CAP theorem
medium 28 minWhen the network between copies of your data breaks, each copy must either refuse some requests or risk answers that disagree.
When it comes up
The moments this idea belongs in a design interview, and the moments it does not.
Signals
When to bring it up
| You hear | Say | Because |
|---|---|---|
| Two regions, both take writes | What should happen when they cannot talk? | That is the CAP question |
| Never sell the same item twice | One home per item, majority, refuse otherwise | Wrong is worse than no |
| The feed must always load | Local reads that may be old | Old is better than no |
| Strong consistency and 10 ms worldwide | The speed of light forbids both | PACELC, not CAP |
| One database, one machine | CAP does not apply yet | Nothing can be split |
Options
Compare your moves
A wallet app runs in two regions. The link between them drops. What about payments?
BreaksBoth regions accept, last write wins
- How
- Each region updates the balance it knows.
- Limit
- The same money can be spent twice, and one write vanishes when the link is back.
- Choose when
- Never for balances.
WorksStop all payments until the link is back
- How
- No region has agreement, so refuse everything.
- Limit
- Correct, but a short cable fault stops every customer.
- Choose when
- Two regions and no way to say who decides.
StrongEach wallet has a home region with a majority inside it
- How
- Payments go to the home. The other region shows the balance as of a time, and may offer small offline payments with a limit.
- Limit
- Travellers far from home wait during the split. Moving a home needs care.
- Choose when
- Most money systems.