Skip to content
Tech Interview Prep
Technical interview guide

CAP Theorem & Consistency Models

Why a distributed system can't have perfect consistency, availability, and partition tolerance all at once, and what real systems trade off.

Minutes to read
3
Practice MCQs
25
Interview Q&A
25
Edition
v4
Editorial status
Reviewed

Scope: Vendor-neutral distributed-systems principles; current AWS architecture guidance.

Overview

Curated: · Written: · Reviewed:

CAP Theorem & Consistency Models

In one line

During a network partition, a replicated store must give up either linearizable consistency or availability, because partitions cannot be avoided. Reach for CAP when asked what a store does when replicas cannot reach each other, then decide per operation which guarantee each read and write needs.

Key definitions

  • Partition: messages between nodes are lost or delayed. It is not an option you can decline.
  • Linearizable (CAP consistency): every read returns the latest acknowledged write, as if there were one copy. Not the same as ACID consistency.
  • Availability (CAP): every non-failed node returns a non-error response. A node blocked waiting for a quorum counts as unavailable.
  • Quorum: W replicas must acknowledge a write, and R replicas are consulted on a read, out of N replicas.
  • Session guarantees: read-your-writes and monotonic reads. Usually what a product needs, and far cheaper than global strong consistency.
  • PACELC: if Partition, choose Availability or Consistency; Else, choose Latency or Consistency.

Formulas and complexity

R+W>NR + W > N guaranteed overlap=R+W−N\text{guaranteed overlap} = R + W - N

N = 3, W = 2, R = 2 gives an overlap of at least 1, so a read sees the latest acknowledged write. N = 3, W = 1, R = 1 gives 2, which is not greater than 3, so the read may miss the write. Overlap is necessary but not sufficient: versions must compare correctly, and sloppy quorums can place replicas outside the intended set.

How it works

  • A write is acknowledged once W replicas store it; the remaining replicas catch up asynchronously.
  • A read consults R replicas and returns the highest version it sees.
  • With R + W > N, every read set shares a replica with every acknowledged write set.
  • During a partition, a CP side refuses requests it cannot complete with a quorum. An AP side accepts writes on both sides and reconciles on heal.
yes no, partitioned Client Coordinator Replica A Replica B Replica C W acks or R reads reached? Respond CP: error / AP: serve, reconcile later

When to use and when not to

operationchoosewhy
balance check before withdrawallinearizable (CP)a stale read informs a write
uniqueness check on signuplinearizable, or single-writertwo accounts with the same email
inventory reservationlinearizable, or commutative reservationprevents overselling
profile display after an editread-your-writes (sticky session or leader read)the user must see their own edit
follower count, recommendationseventual (AP)a stale value is cosmetic
shopping cartCRDT or mergeno added item is lost on conflict

Traps interviewers probe

  1. Saying 'pick two'. Fix: P is not optional. The real choice is C or A during a partition, so name the concrete behaviour on each side.
  2. Calling a live but quorum-blocked node available. Fix: CAP availability means a non-error response from a non-failed node. A node waiting on a quorum is unavailable.
  3. Stopping at 'we are AP'. Fix: PACELC governs normal operation, where the trade is latency against consistency on every synchronous write and replica read.
  4. Treating a stale read as cosmetic when it informs a write. Fix: route decision reads (balance, uniqueness, inventory) to the linearizable path.
  5. Assuming R + W > N guarantees freshness. Fix: state the overlap, then confirm version comparison is correct and that sloppy quorums are not in use.

Interview tips

Open with: 'Consistency is chosen per operation. During a partition I keep the feed available and make the balance check linearizable. In normal operation I only pay latency on the strong path.'

Expect these follow-ups:

  • What does the minority side do with writes it cannot commit?
  • How do you tell a partition from a slow node?
  • What does the strong path cost at p99?
  • How do you test the partition path before production does?
  • Last-write-wins or CRDT for conflicting writes, and what does each discard?

30-second answer outline:

  1. A partition is unavoidable, so during one the choice is consistency or availability.
  2. Name the operations: balance check linearizable, feed eventual.
  3. Mechanism: quorums with R + W > N, or leader reads, plus sticky sessions for read-your-writes.
  4. Minority side: reject balance writes, keep serving the feed.
  5. Normal case: PACELC latency cost only on the strong path.
  6. On heal, reconcile with a named rule.

Feedback on this overview

Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.