AlgoViz

The CAP theorem

medium 28 min

When 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 hearSayBecause
Two regions, both take writesWhat should happen when they cannot talk?That is the CAP question
Never sell the same item twiceOne home per item, majority, refuse otherwiseWrong is worse than no
The feed must always loadLocal reads that may be oldOld is better than no
Strong consistency and 10 ms worldwideThe speed of light forbids bothPACELC, not CAP
One database, one machineCAP does not apply yetNothing 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.