bigonotes
System DesignDatabases

CAP Theorem and Consistency Models

What CAP says about partitions, how eventual, strong and tunable consistency differ, and how quorums decide reads.

7 min readLast updated

As soon as you keep copies of data on more than one machine, the copies can disagree. A write lands on one replica before the others, or the network between replicas fails. The CAP theorem describes the hard choice this creates. Consistency models describe what a reader may see while replicas catch up. Both come up in almost every design that uses a distributed database.

The three properties#

CAP is named after three properties a replicated data store can offer. You'd like all three, and the rest of the note is about why you can't always have them.

Property Meaning
Consistency (C) Every read returns the most recent write, or an error. All clients see one up-to-date copy.
Availability (A) Every request to a working node gets a non-error response, which may hold old data.
Partition tolerance (P) The system keeps running when messages between nodes are lost or delayed.

The "C" in CAP is not the "C" in ACID. CAP consistency is about replicas agreeing on the latest value. ACID consistency is about rules and constraints holding.

What CAP says#

CAP is often told as "pick any two of three". That version is misleading. In a distributed system, network partitions will happen: a switch fails, a link drops, a data center loses contact. You cannot opt out of them. So the real statement is:

During a network partition, a replicated system must choose consistency or availability. When there is no partition, it can provide both.

Here's what that choice looks like. Picture two replicas, A and B, that lose contact. A client writes x = 2 to A. Another client then reads x from B, which still holds x = 1. B has two options:

  • Choose consistency (CP behavior): B refuses the read or returns an error, because it cannot confirm the latest value. Some clients see errors until the partition heals.
  • Choose availability (AP behavior): B returns x = 1. Every client gets an answer, but some answers are stale. The replicas reconcile after the partition heals.

A "CA" system, consistent and available with no partition tolerance, only makes sense on a single machine. Once data lives on two nodes, P is not optional.

Beyond partitions#

Partitions are rare, but the trade-off has an everyday form too. To return the latest value, a read or write must wait for other replicas to answer. That costs latency on every request, not only during failures. The PACELC framing captures this: during a Partition, choose A or C; Else, choose Latency or Consistency. Many databases let you choose per request, as the sections below show.

Real databases rarely fit one label. Most offer settings that move them along the spectrum, so "Cassandra is AP" or "MongoDB is CP" describes defaults, not laws.

Consistency models#

CAP tells you what happens during a failure. Consistency models cover the everyday case: a consistency model is a promise about what reads can return while replicas catch up. These are the ones you name most often in designs.

Model Promise Cost
Strong (linearizable) A read always sees the latest completed write Higher latency; may refuse requests during a partition
Eventual If writes stop, all replicas converge to the same value Reads may be stale for a while
Read-your-writes You always see your own writes; others may lag Route or pin your reads
Monotonic reads You never see data go backwards in time Pin you to one replica

Read-your-writes and monotonic reads sit between strong and eventual. Scaling Reads shows how to provide them with read replicas.

Eventual consistency and stale reads#

With eventual consistency, a write reaches one replica first and spreads to the others in the background. This usually takes milliseconds to seconds. Until it finishes, a read from a lagging replica returns the old value: a stale read. Here's one playing out on a user's bio:

Time Replica 1 Replica 2 What a reader sees
t0 bio = "old" bio = "old" "old" everywhere
t1 User writes bio = "new" bio = "old" Replica 2 returns "old" (stale)
t2 bio = "new" Update arrives: bio = "new" "new" everywhere

Eventual consistency fits data where a short delay does no harm:

  • Social feeds, like counts and view counts.
  • Caches and CDN copies of pages.
  • Product reviews and public profiles.

It does not fit data where a stale read causes real damage: account balances, the last seat on a flight, or unique usernames.

Tunable consistency#

You don't have to pick one model for a whole database. Some databases, such as Apache Cassandra, Amazon DynamoDB and MongoDB, let you choose the consistency level per operation.

  • DynamoDB: reads are eventually consistent by default. You can ask for a strongly consistent read, which costs more read capacity.
  • MongoDB: write concern and read concern set how many replicas must confirm a write and which data a read may return. majority on both gives stronger guarantees.
  • Cassandra: each read and write names a consistency level, such as ONE, QUORUM or ALL.

This lets you use strong reads for a balance and fast, eventual reads for a feed, in the same database.

Quorums#

Settings like QUORUM raise a question: how does asking more replicas make a read correct? Quorums are the answer, and they're how leaderless databases (where any replica can accept a write) make tunable consistency work. Each piece of data has N replicas.

  • A write succeeds when W replicas confirm it.
  • A read asks R replicas and takes the newest value among the replies.

If R + W > N, every read set overlaps every write set in at least one replica. That replica holds the latest acknowledged write, so the read can return it.

Here's how Cassandra's common levels map onto that, with N = 3:

Level Replicas that must respond Speed Survives one node down?
ONE 1 Fastest ✓
QUORUM A majority: 2 of 3 Medium ✓
ALL 3 Slowest ✗

Combining read and write levels gives these results for N = 3:

Write (W) Read (R) R + W > 3? Outcome
ONE (1) ONE (1) ✗ (2) Fast, but reads can be stale
QUORUM (2) QUORUM (2) ✓ (4) Latest write visible; tolerates one node down
ALL (3) ONE (1) ✓ (4) Fast reads, but one node down blocks writes
ONE (1) ALL (3) ✓ (4) Fast writes, but one node down blocks reads

QUORUM for both reads and writes is the usual balance. It keeps reads current and still survives the loss of one replica.

The overlap rule is easy to check. This sketch tries every possible write set and read set for N = 3:

from itertools import combinations


def always_overlap(n: int, w: int, r: int) -> bool:
    """True if every read set shares a replica with every write set."""
    replicas = range(n)
    return all(
        set(ws) & set(rs)
        for ws in combinations(replicas, w)
        for rs in combinations(replicas, r)
    )


print(always_overlap(3, 2, 2))  # True: 2 + 2 > 3
print(always_overlap(3, 1, 1))  # False: a read can miss the write

Quorum caveats#

R + W > N gets you most of the way, but it is not the same as full linearizability (the strong model from the table above).

  • Concurrent writes to the same key still conflict. The database needs a rule to pick a winner, such as last write wins. See Replication Models.
  • Failed writes may land on some replicas and not others. A later read can return that partial value.
  • Sloppy quorums (used by some stores when home replicas are down) accept writes on other nodes. The overlap guarantee no longer holds until the data moves home.

Choosing in a design#

All of this comes down to one habit: decide per kind of data, not per system.

Data Prefer Why
Payments, balances, inventory Consistency A stale read can double-spend or oversell
Usernames, seat booking Consistency Two winners for one item is a bug
Feeds, likes, view counts Availability A short delay is invisible to users
Shopping cart Availability Accept the add; merge conflicts later

In an interview, name the choice and its cost. For example: "Bookings need strong consistency, so I'll use quorum writes and reads and accept higher latency. The feed can be eventually consistent."

Key takeaways#

  • CAP: during a partition, a replicated system chooses consistency or availability. Partitions are not optional.
  • Without a partition, the trade-off becomes latency against consistency.
  • Eventual consistency allows stale reads but stays available. Strong consistency returns the latest write but may refuse requests.
  • Tunable consistency lets you pick per operation: ONE, QUORUM or ALL.
  • With N replicas, R + W > N makes read and write sets overlap, so reads see the latest acknowledged write.
  • Choose per kind of data: consistency for money and unique items, availability for social signals.
Revision notes · 2 min

Core idea#

During a network partition, a replicated system must choose consistency or availability. Consistency models describe what a reader may see while replicas catch up.

Key points#

  • C: every read returns the latest write or an error. A: every request to a working node gets a non-error answer. P: the system survives lost messages between nodes.
  • Partitions are not optional in a distributed system. "Pick two of three" misleads.
  • CP behavior refuses some requests during a partition. AP behavior answers, but some answers are stale.
  • Without a partition, the trade-off is latency against consistency (PACELC).
  • CAP "C" is not ACID "C". CAP is replicas agreeing; ACID is constraints holding.
  • Labels like "AP" or "CP" describe defaults. Most databases offer settings.

Consistency models#

Model Promise
Strong A read sees the latest completed write
Eventual Replicas converge once writes stop; reads may be stale
Read-your-writes You see your own writes
Monotonic reads Data never goes backwards for you
  • Tunable consistency: DynamoDB strongly consistent reads, MongoDB read and write concern, Cassandra levels per operation.

Quorums#

  • N replicas. A write needs W confirmations. A read asks R replicas and takes the newest.
  • R + W > N makes every read set overlap every write set.
W R N = 3 outcome
ONE ONE Fast; stale reads possible
QUORUM QUORUM Latest write visible; survives one node down
ALL ONE Fast reads; one node down blocks writes

Choose per kind of data#

Data Prefer
Payments, balances, inventory, seats Consistency
Feeds, likes, view counts, carts Availability

Pitfalls#

  • Quorums do not resolve concurrent writes. You still need a conflict rule.
  • A failed write can land on some replicas and show up in later reads.
  • Sloppy quorums break the overlap guarantee until data moves home.
  • Eventual consistency for unique usernames gives two winners.

Say it in an interview#

  • "During a partition I choose consistency for bookings and availability for the feed."
  • "Quorum reads and writes, R + W > N, so reads see the latest write."

Found a mistake? Email ask@bigonotes.com