codequest / system design
Core concept 9 of 21

CAP theorem & consistency models

During a network partition you must choose between answering (availability) and being correct (consistency).

What CAP actually says

A distributed data store can't guarantee all three of Consistency (every read sees the latest write),Availability (every request gets a non-error answer) and Partition tolerance (it keeps working when the network between nodes breaks). Networks do break, so partition tolerance isn't optional. The real choice is what to doduring a partition: refuse requests to stay correct (CP), or answer and risk stale data (AP).

Try it

Cut the network, write a new balance through node 1, then read through node 2. Do it as a CP system, then as an AP system.

Node 1100balanceNode 2100balanceConnected: writes replicate
  1. Try: cut the network, write, then read. Then switch to AP and repeat.

Which to choose

  • CP for money, inventory, bookings and unique usernames: a wrong answer is worse than no answer. (Typical: relational databases with synchronous replication, ZooKeeper, etcd, Spanner.)
  • AP for likes, view counts, feeds, shopping carts and presence: a slightly stale answer is fine and staying up matters more. (Typical: Cassandra, DynamoDB in default settings.)
  • One system can mix both: payments CP, the product catalogue AP.

Consistency models

  • Strong (linearizable): every read sees the latest write, as if there were one copy.
  • Read-your-writes: you always see your own updates, even if others see them later.
  • Eventual: if writes stop, all replicas converge. Reads in between can be stale.

PACELC adds the everyday trade-off: even without a partition, you choose between lower latency and stronger consistency, because waiting for replicas to agree takes time.

In the interview

  • Don't say "we pick CA". Partitions happen; the choice is C or A while they do.
  • Decide per piece of data: "balances are CP, the feed is eventually consistent".
  • Explain what the user sees in the AP case (a stale count) and why that's acceptable.