CMD Guide
HomeSystem DesignSystem Design Building Blocks

CAP Theorem

CAP falls out of one physical fact: when a network partition splits your replicas into groups that cannot talk, a write accepted on one side is invisible to the other side until the link heals — so the instant a client reads from the far side, the system must either hand back possibly-stale data (stay Available) or refuse to answer until it can confirm it is current (stay Consistent). The theorem is not a menu of three things you casually pick two of; it is a statement about that one forced choice during a partition.

The three properties, stated precisely

The loose "pick 2" slogan hides how narrowly each word is defined in the Gilbert–Lynch formalization (the 2002 proof of Brewer's conjecture). Get these definitions wrong and every later argument goes wrong.

Because P is forced on you, the real theorem reads: during a partition, a distributed system must sacrifice either Consistency or Availability. CA is incoherent as a distributed choice — a system that assumes partitions never happen has simply not decided what it does when one occurs, and it will then drop C or A anyway. A single-node database is "CA" only in the trivial sense that it is not distributed.

A worked partition trace

Three replicas N1, N2, N3 hold one key, stock:sku42, with replication factor N=3. Initially all three agree the value is 5. A partition then isolates N3 from the majority side {N1, N2}. Follow the same events under a CP configuration (quorum: W=2, R=2, so R+W>N guarantees read/write overlap) and an AP configuration (W=1, R=1).

tEventCP system (W=2, R=2)AP system (W=1, R=1)
t0Steady stateN1=N2=N3=5N1=N2=N3=5
t1Partition: {N1,N2} | {N3}link to N3 downlink to N3 down
t2Client A writes stock=4 (an order is placed)Majority acks (N1,N2 = 2 ≥ W). Commit succeeds. N3 still 5.Nearest replica acks locally. Succeeds. N3 still 5.
t3Client B reads via the stranded N3N3 cannot assemble R=2 (it is alone). Returns error/timeout — refuses to serve. C preserved, A sacrificed on this side.N3 answers from its local copy: returns 5 (stale). A preserved, C sacrificed — the store just oversold the item.
t4Partition healsN3 rejoins, replays the log, converges to 4. No data was ever wrong.Anti-entropy / read-repair pushes 4 to N3. If both sides had taken conflicting writes, version vectors or last-write-wins must reconcile them.

The fork is entirely at t3. Notice the majority side {N1,N2} keeps serving consistent reads in both configurations — the sacrifice is localized to the minority partition. That is why "CP means unavailable" is too coarse: it is unavailable only where it cannot prove freshness.

diagram
diagram

Real CP and AP datastores

The choice is baked into products (and, in tunable stores, into each request):

Pitfalls

When to choose CP vs AP

Signals that point to CP (consistency-first): a stale read causes real, hard-to-undo harm — double-spending a balance, overselling inventory, two clients both winning a lock or a leader election, violating a uniqueness constraint. You gain correctness by construction. You pay with unavailability on the isolated side during a partition and higher write latency every day (quorum round-trips). Reach for etcd/ZooKeeper, MongoDB majority, or R+W>N in a tunable store.

Signals that point to AP (availability-first): serving something beats serving nothing and staleness is either tolerable or mergeable — shopping carts, social feeds, session and preference stores, product catalogs, metrics, DNS. You gain uptime and low latency on every replica. You pay with conflict-resolution machinery (LWW, version vectors, CRDTs) and application code that must tolerate reading slightly old or divergent data. Reach for Cassandra, DynamoDB, or Riak.

The decision rule: choose CP when a wrong answer is worse than no answer; prefer AP when no answer is worse than a slightly wrong answer. And because most real systems are mixed, apply it per data domain, not per company — the same shop can run inventory-decrement on CP and the browse feed on AP. The moment you also care about the everyday, partition-free latency-vs-consistency trade, graduate to PACELC.

Takeaways


Sources: Eric Brewer's PODC 2000 keynote (the CAP conjecture) and his 2012 retrospective "CAP Twelve Years Later: How the Rules Have Changed" (IEEE Computer); Seth Gilbert & Nancy Lynch, "Brewer's Conjecture and the Feasibility of Consistent, Available, Partition-Tolerant Web Services" (2002, the formal proof); Martin Kleppmann, Designing Data-Intensive Applications, ch. 5 & 9; the Amazon Dynamo (2007) and Google Spanner (2012) papers; and Kyle Kingsbury's Jepsen analyses for the real-world failure modes. Re-authored/Deepened for this guide.

🤖 Don't fully get this? Learn it with Claude

Stuck on CAP Theorem? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **CAP Theorem** (System Design) and want to truly understand it. Explain CAP Theorem from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **CAP Theorem** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **CAP Theorem** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **CAP Theorem** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes