Module 1: Core Concepts
CAP Theorem
Understand Consistency, Availability, and Partition Tolerance — and how real systems choose CP or AP under network partitions.
CAP is not a slogan. Under a network partition, a distributed data system must choose how it behaves when nodes cannot talk. Interviews use CAP to test whether you can trade correctness against liveness for a concrete feature.
What you will learn
- Define Consistency, Availability, and Partition tolerance in plain interview language
- Explain why partitions force a CP vs AP choice on the write/read path
- Map product features (payments, feeds, inventory) to CP or AP preferences
- Describe what the user sees: error vs stale data
- Avoid common CAP myths and connect briefly to PACELC when asked
What CAP is really about
- CAP talks about a distributed data store when the network between nodes breaks (a partition). It is not a rating of “how good” a database is day to day.
- The useful interview move: name the feature, name the partition scenario, then say what you keep and what you drop.
- Applies to systems with multiple nodes that share or replicate data
- The hard case is when some nodes cannot reach others but clients can still reach both sides
- Your choice shows up as user-visible behavior: fail a request, or return possibly stale data
- CAP is about partition behavior — not the same as “always available” marketing claims
The three letters
- Define each letter before you pick CP or AP. Interviewers listen for precise wording.
- Consistency (C) — a read returns the latest successful write (or an error); no silent stale success
- Availability (A) — every non-failing node that receives a request returns a response (even if data may be stale)
- Partition tolerance (P) — the system keeps operating despite message loss or delay between nodes
- “Consistency” here is linearizability-style single-copy behavior — not the C in ACID alone
Why partitions force a choice
- Real networks drop packets, isolate AZs, and split clusters. Multi-node systems must assume P will happen.
- When two sides of a split both accept writes without coordination, they can disagree. That is the CAP fork.
- If you require C during a partition, some requests must be refused or delayed until a quorum is reachable (CP)
- If you require A during a partition, nodes may serve local state and reconcile later (AP)
- You cannot promise “always latest data” and “always a success response” on every side of a hard split
- When the network is healthy, many systems feel both consistent and available — CAP pain shows up mainly under failure
CP systems — prefer correctness
- CP designs protect a single source of truth. If the primary/quorum is unreachable, they fail or block rather than guess.
- Typical tools: consensus (Raft/Paxos), single-leader with fencing, quorum reads/writes
- User sees: timeouts, 503s, or “try again” — not silently wrong balances
- Good fit: payments, seat booking, inventory reservation, unique username claims
- Cost: lower availability during splits; need careful leader election and client retries
AP systems — prefer liveness
- AP designs keep answering from the reachable replica. They accept temporary disagreement and heal after the partition ends.
- Typical tools: multi-leader or leaderless replication, hinted handoff, conflict resolution (LWW, CRDTs, merge)
- User sees: success with possibly stale likes, presence, or feed counters
- Good fit: social counts, “last seen”, recommendations, analytics sketches
- Cost: you must design conflict rules and explain stale windows in the interview
CP vs AP quick comparison
- Use this side-by-side when the interviewer asks which side you choose.
- Goal — CP: one correct value; AP: keep serving on both sides
- Failure mode — CP: errors/blocks; AP: stale or divergent values
- Healing — CP: resume when quorum returns; AP: merge/reconcile after reconnect
- Ops pain — CP: leader/quorum health; AP: conflict storms and repair tooling
- Interview signal — name the user-visible failure explicitly
Product mapping examples
- Map CAP to features, not whole companies. One product can be CP on checkout and AP on the activity feed.
- Bank transfer / wallet debit → CP (wrong balance is unacceptable)
- E-commerce inventory for a flash sale → usually CP or carefully controlled stock
- Social likes / view counters → AP with eventual catch-up
- Chat message delivery receipts → often AP with causal/order tricks
- DNS-style configs / CDN edge metadata → often AP with TTLs
Consistency spectrum (beyond the slogan)
- Real systems sit on a spectrum. Strong consistency, read-your-writes, causal, and eventual are useful nuance once CAP is clear.
- Strong / linearizable — readers see the latest committed write as if one copy existed
- Read-your-writes — a user sees their own updates even if others may lag
- Causal — respects “happens-before” edges (comment after post)
- Eventual — replicas converge if writes stop; temporary divergence is allowed
- Pick the weakest model that still meets the product promise — over-strengthening kills availability and latency
Common misconceptions
- These lines sound confident but lose points in interviews.
- “MongoDB is AP, Postgres is CP” as a blanket label — depends on deployment and settings
- “CAP means we can ignore latency” — CAP is about partitions; latency is a separate trade-off
- “We will be CA by using one server” — a single node avoids the distributed partition case; that is not a distributed CAP win
- “Eventual consistency means forever wrong” — it means temporary divergence with a convergence plan
- Using CAP jargon without saying what the user experiences under failure
PACELC in one breath
- If the interviewer asks what happens when there is no partition, mention PACELC briefly.
- PACELC: if Partition → choose A or C; Else → choose Latency or Consistency
- Even healthy clusters trade sync replication (stronger C, higher latency) vs async (lower latency, stale risk)
- Example: sync quorum writes raise cross-AZ latency; async replicas feel fast but lag
- Only bring PACELC up if you can finish the explanation in 20–30 seconds
Interview framing
- Use this order when CAP appears in a design round.
- Clarify the data item and the failure (two AZs cannot talk)
- Define C, A, P in one line each
- Pick CP or AP for that feature and state the user-visible outcome
- Name one mechanism (quorum, leader, CRDT, LWW) that implements the choice
- Call out a second feature that would choose the other side
Quick revision checklist
- Define C, A, and P before saying CP or AP.
- Always tie the choice to one concrete feature, not the whole product.
- State the user-visible failure: error vs stale data.
- Mention healing: quorum resume (CP) or conflict merge (AP).
- Avoid database brand stereotypes unless you know the exact config.
- Use PACELC only as a short add-on when asked about the no-partition case.
FAQs
Does CAP mean we ignore latency?
No. CAP is about behavior under partition. Latency vs consistency when the network is fine is the “ELC” part of PACELC — mention it only if you can explain it briefly.
Is CAP the same as ACID?
No. ACID describes transaction properties on a database. CAP “Consistency” is about whether clients see one up-to-date copy across nodes during a partition. Keep the terms separate.
Can a system be CA?
In the classic CAP framing, once you require partition tolerance for a multi-node system, you choose CP or AP under a split. A single-node store avoids that distributed choice but also loses multi-node fault isolation.
Are NoSQL databases always AP?
No. Many offer tunable quorums or consensus modes. Always talk about the consistency setting and topology, not the marketing category.
What should I choose for a payments API?
Prefer CP (or carefully designed strong consistency with quorum). Serving a stale balance as success is worse than returning an error and retrying.
What should I choose for like counts on a post?
Usually AP with eventual reconciliation. Users tolerate a briefly wrong count more than a hard outage on every like.
Learner’s Task
- Practice out loud for 8–10 minutes. Treat CAP as a feature-level decision, not a database brand label.
- Pick a product with two features (e.g. wallet debit + activity feed) and state a two-AZ network partition scenario.
- Define C, A, and P in one sentence each, then choose CP or AP for each feature.
- For each choice, say the user-visible outcome (error/block vs stale success) in plain words.
- Name one mechanism per choice (quorum/leader vs multi-leader/CRDT/LWW) and how healing works after the split heals.
- Write a 4-sentence closing: feature, CAP side, user impact, and one risk you accept.