AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Skip to content
SD notes

System Design · Note 8 of 22

CAP Theorem

Intermediate · 7 min read · updated 29 October 2026

Quick answer

What is the CAP theorem?

The CAP theorem says that when a network partition splits a distributed system, each side must choose between consistency (every read sees the latest write, or fails) and availability (every request gets an answer, possibly stale). Partitions cannot be ruled out, so the real choice is what to give up while one lasts.

On this page
  1. The three letters
  2. CP: refuse rather than be wrong
  3. AP: answer, even if stale
  4. After the partition: eventual consistency
  5. Quorums: tuning between the two
  6. CP vs AP at a glance
  7. PACELC
  8. Interview questions
  9. Common questions

CAP is about one moment: the network has split, and a replica cannot reach its peers. What should it do with the request in front of it?

The three letters

  • Consistency (C): every read sees the latest write, as if there were one copy. If that cannot be guaranteed, the request fails.
  • Availability (A): every request to a working node gets an answer, not an error.
  • Partition tolerance (P): the system keeps running when messages between nodes are lost or delayed.
  • Partitions are not optional. Cables, switches and cloud zones fail, so "CA" is not a real option for a distributed system. The choice is C or A while a partition lasts.

CP: refuse rather than be wrong

  • Requests need a majority of replicas (2 of 3 here).
  • The majority side keeps working; the cut-off side refuses reads and writes.
  • No client ever sees stale data, but some clients get errors.
  • Typical of systems built on consensus (Raft, Paxos), such as etcd and ZooKeeper.
A CP system during a partition: the cut-off side refuses

Before any failure, a write of x = 1 reaches all three replicas, so every replica agrees. This system is CP: it needs a majority (2 of 3) for every request.

0:01 / 0:19Step 1 of 6
Transcript · 6 steps

AP: answer, even if stale

  • Every replica answers from what it has.
  • Both sides can accept writes, so their copies diverge.
  • Fits data where a stale answer costs little: likes, carts, feeds, DNS.
An AP system during a partition: every side answers, maybe stale

Before any failure, a write of x = 1 reaches all three replicas, so every replica agrees. This system is AP: any reachable replica answers.

0:01 / 0:17Step 1 of 6
Transcript · 6 steps

After the partition: eventual consistency

  • When the link heals, replicas compare versions (anti-entropy, read repair).
  • A conflict rule picks the winner. Last-write-wins is simple but silently drops a write; vector clocks or CRDTs keep or merge both.
  • If writes stop, all replicas end up equal: eventual consistency.
After the partition heals: replicas swap versions and converge

After the AP run the replicas disagree: A and B hold x = 2 (written at t3), C holds x = 3 (t5). Each write carries the time it was made.

0:01 / 0:16Step 1 of 5
Transcript · 5 steps

Quorums: tuning between the two

  • N replicas; a write waits for W acknowledgements; a read asks R replicas.
  • R + W > N means every read overlaps the latest write.
  • Small R and W are fast and available but can read stale data.
  • Common setting: N = 3, R = 2, W = 2.
Quorums: with N = 3, R + W > N makes every read meet the last write

With N = 3 replicas, a write waits for W of them to confirm and a read asks R of them. The table lists R + W for every choice of R and W.

0:01 / 0:24Step 1 of 8
Transcript · 8 steps

CP vs AP at a glance

CPAP
During a partitionMinority side refusesEvery side answers
ReadsAlways the latestMay be stale
WritesOnly with a majorityAnywhere, may conflict
After healingNothing to mergeReconcile conflicts
Good forLocks, config, balancesFeeds, carts, counters

PACELC

  • Partition → A or C; Else → Latency or Consistency.
  • Without failures, waiting for more replicas still costs time on every request.
  • Many databases let you choose per request (for example a read at quorum versus a read from one replica).

Interview questions

  • Why not choose CA? A distributed system cannot rule out partitions, so it must decide what to do when one happens.
  • Would you pick CP or AP for a bank balance? CP: refusing a transfer briefly is better than allowing a double spend.
  • And for a social feed? AP: a slightly old feed is fine; being down is not.
  • How do quorums give strong reads? With R + W > N, any read set shares a replica with the last write set.
  • What does last-write-wins lose? A concurrent write with an older timestamp is discarded without any error.

Next: Consistent Hashing · Before this: Database Sharding

Common questions

Is CAP really "pick two of three"?

Not in practice. Partition tolerance is not optional, because networks do drop and delay messages. The choice only bites during a partition: refuse some requests to stay consistent (CP) or answer them all and accept stale or conflicting data (AP). With no partition, a system can offer both.

What does consistency mean in CAP?

Linearizability: every read returns the most recent completed write, as if there were one copy of the data. It is a stronger and different idea from the C in ACID, which is about a transaction keeping the database's rules intact.

What is the difference between a CP and an AP system?

During a partition a CP system refuses requests on the side that cannot reach a majority, so it never returns stale data. An AP system answers on every side, so it stays up but may return stale values and must reconcile conflicting writes once the partition heals.

What is eventual consistency?

A guarantee that if writes stop, every replica eventually holds the same value. Replicas may disagree for a while, especially after a partition, and converge through anti-entropy, read repair or hinted handoff, with a rule such as last-write-wins or a CRDT merge for conflicts.

How do quorums relate to CAP?

With N replicas, writes wait for W acknowledgements and reads ask R replicas. If R + W > N every read set overlaps the last write set, so reads see the latest write. Requiring quorums makes a minority side unable to answer, which is a CP choice.

What is PACELC?

An extension of CAP: if there is a Partition, choose Availability or Consistency; Else, in normal running, choose Latency or Consistency. Even without failures, waiting for more replicas to confirm makes every request slower, so systems trade latency for consistency all the time.

CAP Theorem in System Design, Animated

The CAP theorem for system design interviews: CP vs AP during a network partition, eventual consistency, quorums (R + W > N) and PACELC, shown as animations.

  • Subject: System Design
  • Level: Intermediate
  • Reading time: 7 min
  • Updated: 2026-10-29

What is the CAP theorem?

The CAP theorem says that when a network partition splits a distributed system, each side must choose between consistency (every read sees the latest write, or fails) and availability (every request gets an answer, possibly stale). Partitions cannot be ruled out, so the real choice is what to give up while one lasts.

CAP is about one moment: the network has split, and a replica cannot reach its peers. What should it do with the request in front of it?

The three letters

  • Consistency (C): every read sees the latest write, as if there were one copy. If that cannot be guaranteed, the request fails.
  • Availability (A): every request to a working node gets an answer, not an error.
  • Partition tolerance (P): the system keeps running when messages between nodes are lost or delayed.
  • Partitions are not optional. Cables, switches and cloud zones fail, so "CA" is not a real option for a distributed system. The choice is C or A while a partition lasts.

CP: refuse rather than be wrong

  • Requests need a majority of replicas (2 of 3 here).
  • The majority side keeps working; the cut-off side refuses reads and writes.
  • No client ever sees stale data, but some clients get errors.
  • Typical of systems built on consensus (Raft, Paxos), such as etcd and ZooKeeper.
network partitionReplica Ax = 2Replica Bx = 2Replica Cx = 16. read x at ARx = 2
A CP system during a partition: the cut-off side refuses.
  1. Before any failure, a write of x = 1 reaches all three replicas, so every replica agrees. This system is CP: it needs a majority (2 of 3) for every request.
  2. The network splits: C can no longer reach A and B. Partitions are not a design choice; on a real network they happen, and the system must decide what to do.
  3. A can reach 2 of 3 replicas, a majority, so the write is accepted: A and B now hold x = 2.
  4. A read at C is refused: C cannot reach a majority, so it cannot prove its x = 1 is the latest. Consistent, but unavailable on this side.
  5. C alone is not a majority, so the write of x = 3 is refused. The client gets an error, but no replica can ever disagree with the majority side.
  6. A read at A, on the majority side, returns x = 2, the latest write. The CP system stayed correct and lost only the cut-off side.

AP: answer, even if stale

  • Every replica answers from what it has.
  • Both sides can accept writes, so their copies diverge.
  • Fits data where a stale answer costs little: likes, carts, feeds, DNS.
network partitionReplica Ax = 2Replica Bx = 2Replica Cx = 36. read x at ARx = 2 (stale)
An AP system during a partition: every side answers, maybe stale.
  1. Before any failure, a write of x = 1 reaches all three replicas, so every replica agrees. This system is AP: any reachable replica answers.
  2. The network splits: C can no longer reach A and B. Partitions are not a design choice; on a real network they happen, and the system must decide what to do.
  3. A can reach 2 of 3 replicas, and an AP system accepts the write anyway: A and B now hold x = 2.
  4. The read at C answers x = 1 straight away, though x = 2 was written on the other side. Available, but the answer is stale.
  5. C can reach 1 of 3 replicas, and an AP system accepts the write anyway: C now holds x = 3.
  6. A read at A returns x = 2, while C holds x = 3. Both sides kept accepting writes, and their copies have diverged.

After the partition: eventual consistency

  • When the link heals, replicas compare versions (anti-entropy, read repair).
  • A conflict rule picks the winner. Last-write-wins is simple but silently drops a write; vector clocks or CRDTs keep or merge both.
  • If writes stop, all replicas end up equal: eventual consistency.
Replica Ax = 3 · t5Replica Bx = 3 · t5Replica Cx = 3 · t5
After the partition heals: replicas swap versions and converge.
  1. After the AP run the replicas disagree: A and B hold x = 2 (written at t3), C holds x = 3 (t5). Each write carries the time it was made.
  2. The network heals, but nothing has synced yet: a read at B still answers x = 2. Right after a partition, replicas can disagree for a while.
  3. Anti-entropy: the replicas exchange what they hold and compare the timestamps. C's write (t5) is newer than A's and B's (t3).
  4. Last write wins: A and B take x = 3. The write of x = 2 is silently discarded, a known cost of this rule; vector clocks or CRDTs keep both instead.
  5. Every replica now holds x = 3: the system has converged. That is eventual consistency: if writes stop, all replicas end up agreeing.

Quorums: tuning between the two

  • N replicas; a write waits for W acknowledgements; a read asks R replicas.
  • R + W > N means every read overlaps the latest write.
  • Small R and W are fast and available but can read stale data.
  • Common setting: N = 3, R = 2, W = 2.
234345456R = 1R = 2R = 3W = 1W = 2W = 3R + WN = 3: strong when R + W > 3Replica 1x = 6 · v3Replica 2x = 6 · v3readReplica 3x = 4 · v1read
Quorums: with N = 3, R + W > N makes every read meet the last write.
  1. With N = 3 replicas, a write waits for W of them to confirm and a read asks R of them. The table lists R + W for every choice of R and W.
  2. When R + W > N, any R replicas and any W replicas must share at least one, so a read always reaches the latest write. 6 of the 9 pairs qualify, such as R = 2, W = 2.
  3. Take W = 1: a write of x = 5 is confirmed as soon as Replica 1 has it (v2). Replicas 2 and 3 have not seen it yet.
  4. With R = 1 a read may ask only Replica 3 and get x = 4 (v1): 1 + 1 is not greater than 3, so the read set can miss the write set.
  5. Now W = 2: a write of x = 6 waits for Replicas 1 and 2 to confirm (v3). Replica 3 is still behind.
  6. A read with R = 2 that asks Replica 1 and Replica 2 sees v3 on both and returns x = 6.
  7. Asking Replica 1 and Replica 3 gets v3 and v1; the reader keeps the higher version, x = 6. Replica 1 is in both the read and the write set.
  8. Even Replica 2 and Replica 3, which includes the stale replica, overlaps the write at Replica 2. Every 2-replica read meets the 2-replica write, so R + W > N holds.

CP vs AP at a glance

CPAP
During a partitionMinority side refusesEvery side answers
ReadsAlways the latestMay be stale
WritesOnly with a majorityAnywhere, may conflict
After healingNothing to mergeReconcile conflicts
Good forLocks, config, balancesFeeds, carts, counters

PACELC

  • Partition → A or C; Else → Latency or Consistency.
  • Without failures, waiting for more replicas still costs time on every request.
  • Many databases let you choose per request (for example a read at quorum versus a read from one replica).

Interview questions

  • Why not choose CA? A distributed system cannot rule out partitions, so it must decide what to do when one happens.
  • Would you pick CP or AP for a bank balance? CP: refusing a transfer briefly is better than allowing a double spend.
  • And for a social feed? AP: a slightly old feed is fine; being down is not.
  • How do quorums give strong reads? With R + W > N, any read set shares a replica with the last write set.
  • What does last-write-wins lose? A concurrent write with an older timestamp is discarded without any error.

Next: Consistent Hashing · Before this: Database Sharding

Common questions

Is CAP really "pick two of three"?

Not in practice. Partition tolerance is not optional, because networks do drop and delay messages. The choice only bites during a partition: refuse some requests to stay consistent (CP) or answer them all and accept stale or conflicting data (AP). With no partition, a system can offer both.

What does consistency mean in CAP?

Linearizability: every read returns the most recent completed write, as if there were one copy of the data. It is a stronger and different idea from the C in ACID, which is about a transaction keeping the database's rules intact.

What is the difference between a CP and an AP system?

During a partition a CP system refuses requests on the side that cannot reach a majority, so it never returns stale data. An AP system answers on every side, so it stays up but may return stale values and must reconcile conflicting writes once the partition heals.

What is eventual consistency?

A guarantee that if writes stop, every replica eventually holds the same value. Replicas may disagree for a while, especially after a partition, and converge through anti-entropy, read repair or hinted handoff, with a rule such as last-write-wins or a CRDT merge for conflicts.

How do quorums relate to CAP?

With N replicas, writes wait for W acknowledgements and reads ask R replicas. If R + W > N every read set overlaps the last write set, so reads see the latest write. Requiring quorums makes a minority side unable to answer, which is a CP choice.

What is PACELC?

An extension of CAP: if there is a Partition, choose Availability or Consistency; Else, in normal running, choose Latency or Consistency. Even without failures, waiting for more replicas to confirm makes every request slower, so systems trade latency for consistency all the time.

← Database Sharding · Consistent Hashing →