CMD Guide
HomeSystem DesignCAP Theorem

Tradeoffs in CAP Theorem

When a network partition cuts a replicated system in two, every node can still take requests locally but can no longer see the other side's writes — so the system must pick one of two behaviors on each side: refuse to answer (so the two sides can never disagree — this is consistency, at the cost of availability) or keep answering from stale local state (so the two sides diverge and must be reconciled later — this is availability, at the cost of consistency). That forced pick during the partition is the entire content of CAP; there is no third option that keeps both, because a node cannot confirm a write is globally agreed while it cannot reach the other replicas.

Why a partition forces the choice

Consider a single key balance replicated on two nodes, N1 and N2, currently both holding $100. A link failure isolates them. A client hits N1 with write balance = $50; a moment later another client hits N2 with read balance. N2 cannot ask N1 whether anything changed. It now has exactly two lawful moves:

The impossibility is not a limitation of clever engineering — it is that "confirm this write is globally agreed" and "answer without contacting the other side" are mutually exclusive when the sides can't talk.

Traced partition timeline: CP vs AP on the same event

Same three-replica cluster (N1, N2, N3), same key x = 100, same failure. A partition isolates N1 from the majority {N2, N3}. Watch how a CP system (quorum-based, e.g. a MongoDB replica set) and an AP system (e.g. Cassandra with ONE consistency) respond to the identical request stream.

tEventCP system (needs majority = 2/3)AP system (any node answers)
0sHealthy; x = 100 everywhereReads/writes served, all agreeReads/writes served, all agree
5sPartition: N1 | {N2,N3}N1 loses contact with majorityAll nodes keep running
6sClient A: write x=50 hits N1Rejected / times out — N1 has no quorum, cannot commitAccepted — N1 stores x=50 locally, returns OK
7sClient B: write x=80 hits N2Committed — {N2,N3} form a majority, x=80Accepted — {N2,N3} store x=80
8sClient C: read x hits N1Rejected (or blocks) — N1 can't prove freshnessReturns stale 50 from N1 (diverged from 80)
20sPartition healsN1 rejoins, catches up to x=80 automaticallyConflict 50 vs 80 must be resolved — last-write-wins by timestamp, or app-level merge
21sFinal statex=80 everywhere; N1 was unavailable 5–20s but never wrongx=80 (if t=7s wins by clock) — but the t=6s write to N1 is silently lost

The CP system traded a 15-second availability hole on the minority side for a guarantee that no client ever read a wrong-or-doomed value. The AP system stayed up the whole time but accepted a write it later discarded and served a stale read — the divergence is real and someone must own the reconciliation policy.

diagram
diagram

CP, AP, and CA — with correctly-classified systems

PACELC: the trade-off when there is no partition

StateChoiceLatency costWhen to pick
Partition (P)CP: refuse minority requestsAvailability hit on minorityWrong answer is worse than no answer
Partition (P)AP: keep serving from local stateReconciliation laterUptime is the primary risk
Else (no partition)EC: wait for global agreementCross-replica/region RTTReads must be current
Else (no partition)EL: answer from local replicaReplica-local, usually sub-msStaleness is acceptable

CAP is the special case under a partition; PACELC is the daily question. The same data set can answer EL for a tolerant read and EC for a must-be-current read.

Pitfalls

When to choose CP, when to choose AP

Decision signals that point to CP: a stale or double-applied operation causes a real-world wrong outcome — money moved twice, one lock held by two owners, oversold inventory, split-brain leadership. If your answer to "what if two clients see different values for a few seconds?" is "that's a bug, not an annoyance," you want CP and you accept that the minority partition returns errors.

Decision signals that point to AP: a brief staleness is cosmetic and downtime is the expensive failure — a feed that's a few seconds behind, a cart that occasionally resurrects a removed item, a catalog page. If "the site must answer even if the number is slightly old" wins, you want AP and you must fund a conflict-resolution strategy.

CP vs AP, concretely — what you gain and what it costs: CP gives you a system you can reason about (there is one truth) and removes an entire class of merge bugs; it costs you availability on the minority side, added write latency from quorum round-trips, and a hard dependency on fast, reliable leader election. AP gives you near-100% write availability and low latency answered from the nearest replica; it costs you correctness during partitions, forces you to design and test reconciliation (LWW/CRDT/merge), and pushes "which write wins?" complexity up into the application. Choose CP when a wrong answer is worse than no answer (coordination, ledgers, uniqueness constraints). Prefer AP when no answer is worse than a slightly stale answer (carts, feeds, presence, telemetry). When you cannot cleanly pick, split the system: keep the ledger CP and the cart AP, rather than forcing one guarantee on both.

Takeaways

Saying CAP in one breath

Interview one-liner: In a distributed replicated system, a network partition forces a choice — either stop answering to guarantee that every answer is consistent, or keep answering and accept that some answers may be stale or conflicting; you cannot do both.

Proof sketch by contradiction

  1. Assume a replicated system is both consistent and available despite a partition.
  2. A client writes value v1 to replica N1.
  3. The network partitions, isolating N1 from replica N2.
  4. A client reads from N2. To be available, N2 must respond. To be consistent, it must return v1, but it cannot reach N1 to learn it.
  5. Contradiction. Therefore the assumption is false: under partition, consistency and availability cannot both hold.

This is the core of Gilbert and Lynch's proof: in an asynchronous network a partitioned replica cannot tell a dead peer from a slow one, so waiting for agreement means waiting forever, while answering immediately risks being stale.

Real-system mapping

This revisits the CP/AP/CA classification from earlier, but adds the column that matters in practice: the tunable lever — the per-request knob each system exposes to move along its consistency/latency axis.

Default postureRepresentative systemsWhy it fitsLatency / consistency lever
CPHBase (ZooKeeper-coordinated),
CockroachDB (serializable default)
A stale or double-committed value is a correctness bug: row keys, account balances, inventory, coordination data.HBase blocks minority reads/writes; CockroachDB waits for consensus by default but offers follower reads (EL) for stale-tolerant queries.
APCassandra (default ONE),
Dynamo / DynamoDB (eventual reads)
Uptime and write availability matter more than a briefly stale read: shopping carts, product catalogs, feeds, telemetry.Cassandra lets you raise read/write consistency to QUORUM when you need stronger guarantees; DynamoDB offers strongly-consistent reads per request.
CASingle-node PostgreSQL / MySQLNot a distributed category. One box is consistent and available while it is up; the instant replicas exist, partitions force CP or AP behavior.Synchronous replication looks CA until the link splits, then it must halt (CP) or diverge (AP).

PACELC latency case. Even without a partition, the same system trades latency for consistency on every request. In Cassandra a QUORUM read waits for a majority of replicas (EC: consistent but slower); a ONE read returns the local replica (EL: fast but possibly stale). DynamoDB strongly-consistent reads wait; eventual reads do not. CockroachDB's default serializable transaction commits through consensus (EC), while follower reads serve historical snapshots from the nearest replica (EL). The interview move is to name the lever, not just the label.

Trap box: when CP is the wrong choice

CP is not a virtue in every design. It is the wrong default when the cost of stopping exceeds the cost of being stale:

The trap: saying "this is important, so CP" without asking "is a stale answer actually wrong, or just slightly out of date?" If the honest answer is the latter, CP buys you correctness you do not need and pays with p99 latency and minority-side outages.

When NOT to invoke CAP as a design trump card

Interviewer follow-ups & drills

  1. Why not always CP? During partition, rejecting all writes may be worse than serving stale reads for a feed.
  2. Ops signals: split-brain double-writes, elevated conflict rates, client timeouts during AZ loss.
  3. Drill: Shopping cart vs bank transfer under AZ partition — which may be AP-ish, which must be CP? Cart can stale; transfer should not double-spend.

Sources: Eric Brewer's PODC 2000 keynote and "CAP Twelve Years Later" (IEEE Computer, 2012); Gilbert & Lynch's 2002 proof of the CAP conjecture; DeCandia et al., "Dynamo: Amazon's Highly Available Key-value Store" (SOSP 2007); Daniel Abadi's PACELC formulation (2012); and the Apache ZooKeeper, Apache Cassandra, and MongoDB documentation for system classifications. Re-authored/Deepened for this guide.

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

Stuck on Tradeoffs in 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 **Tradeoffs in CAP Theorem** (System Design) and want to truly understand it. Explain Tradeoffs in 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 **Tradeoffs in 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 **Tradeoffs in 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 **Tradeoffs in 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