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
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.
When to use and when not to
| operation | choose | why |
|---|---|---|
| balance check before withdrawal | linearizable (CP) | a stale read informs a write |
| uniqueness check on signup | linearizable, or single-writer | two accounts with the same email |
| inventory reservation | linearizable, or commutative reservation | prevents overselling |
| profile display after an edit | read-your-writes (sticky session or leader read) | the user must see their own edit |
| follower count, recommendations | eventual (AP) | a stale value is cosmetic |
| shopping cart | CRDT or merge | no added item is lost on conflict |
Traps interviewers probe
- 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.
- 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.
- Stopping at 'we are AP'. Fix: PACELC governs normal operation, where the trade is latency against consistency on every synchronous write and replica read.
- Treating a stale read as cosmetic when it informs a write. Fix: route decision reads (balance, uniqueness, inventory) to the linearizable path.
- 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:
- A partition is unavoidable, so during one the choice is consistency or availability.
- Name the operations: balance check linearizable, feed eventual.
- Mechanism: quorums with R + W > N, or leader reads, plus sticky sessions for read-your-writes.
- Minority side: reject balance writes, keep serving the feed.
- Normal case: PACELC latency cost only on the strong path.
- 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.