CAP Theorem

When the network splits, what do you pick: correct data or an available system? The most famous tradeoff in distributed systems.

Intermediate7 min read
Language
Theme
ConsistencyAvailabilityPartition toleranceNetwork partitions WILL happen,so the real choice during one is:CP: correctAP: stay up
Key idea at a glance
INTERACTIVE

Playground: feel the CAP theorem

Two datacenters, one link. Cut the link, then write to one side and read from the other. AP answers with stale data; CP refuses to answer. You cannot have both.

DC-1
X = 0
DC-2
X = 0

The statement, precisely

CAP says: when a network Partition happens (two parts of your system cannot talk), a distributed system must choose between Consistency (every read sees the latest write, or an error) and Availability (every request gets a non-error response, possibly stale). You can have at most two of three, and since partitions are a fact of networks, the real choice during a partition is only between C and A.

The two ATMs (the example that makes it stick)

A bank runs 2 ATMs in different cities showing the same account balance. The link between them breaks. Two options:

  • Refuse withdrawals (CP): ATM says "try later". Balance is never wrong, but the ATM is unavailable. Correct over available.
  • Keep dispensing (AP): ATM allows withdrawal using the last known balance. Available, but if the user also withdraws in the other city, the account goes negative. Available over correct.

A bank chooses CP for balances. A social network chooses AP for likes: nobody dies if a like count is stale for 30 seconds.

Why P is not optional

Cables get cut, switches fail, cloud regions partition. If you build a distributed system (more than one node), you will face partitions. So "CA" systems only exist as single-node systems, which are not really distributed and are themselves SPOFs.

Real systems on the spectrum

  • CP: ZooKeeper, etcd, HBase, MongoDB (default majority reads/writes). Used for coordination, config, leader election: being wrong is worse than being down.
  • AP: Cassandra, DynamoDB, CouchDB. Used for carts, sessions, feeds: being down is worse than being briefly stale.
  • Postgres/MySQL single primary: consistent when up, unavailable during failover: effectively CP with a manual partition story.

PACELC: the fuller picture

CAP only describes behavior during partitions. PACELC adds the normal case: if Partition, choose Availability or Consistency; Else (normal operation), choose Latency or Consistency. Dynamo-style systems: PA/EL (available under partition, low-latency normally, eventual consistency always). This explains "eventual consistency" as an everyday latency optimization, not only a partition behavior.

Quorums: the tunable middle

Dynamo-style systems answer CAP with knobs: N replicas, W write acknowledgments, R read acknowledgments. If W + R > N, reads see the latest write (strong-ish). W=1, R=1: fastest, weakest. W=N, R=1: every read fast, writes wait for all replicas. In interviews, naming N=3, W=2, R=2 and why (W + R = 4 > 3) is the mark of someone who has actually used these systems.

Common interview traps

  • "The system is CA" for anything distributed.
  • Treating CAP as a one-time system-wide label instead of a per-feature choice made during partitions.
  • Forgetting the latency angle: PACELC.

Interview Q&A

Q: Design a shopping cart: CP or AP?
A: AP. A cart must accept adds even during network issues (availability drives revenue), and a rare stale merge (two devices' carts merged) is acceptable. Checkout/payment, however, is CP: money must be exactly right.

Q: What happens to your design when the network partition heals?
A: AP systems reconcile: last-write-wins, vector clocks or CRDTs merge divergent writes. CP systems had nothing to reconcile: they refused writes on the minority side.

Key takeaways

Interview pointer

CAP is rarely asked directly; it is tested mid-design: 'what if the link between these two datacenters goes down?' Answer confidently: 'for this component I would choose AP because stale data is acceptable, but the payments part stays CP.'