The CAP Theorem
C vs A, only during a partitionCAP names three properties a distributed store might want: Consistency (every read sees the most recent write, as if there were one copy), Availability (every request gets a non-error response), and Partition tolerance (the system keeps working when the network drops messages between nodes).
The popular framing, "pick two of three," is wrong. Partitions are not a choice you make; networks fail whether you like it or not. So P is not optional for any real distributed system. The theorem is really about one moment: when a partition happens, do you sacrifice consistency or availability?
Client writes x=2 Client reads x
โ โ
โผ โผ
โโโโโโโโโโโโโโโโโ โณ partition โณ โโโโโโโโโโโโโโโโโ
โ Node A โ โโโโโโโโโโโ X โโโโโโโโโโโโโโโ โ Node B โ
โ accepts x=2 โ (cannot sync) โ still x = 1 โ
โโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโ
CP choice โ Node B refuses the read (or A refuses the write): no stale data, but unavailable.
AP choice โ Node B returns x=1: available, but the read is stale until the partition heals.
During a partition, reject requests the system can't serve correctly. The minority side stops taking writes; some clients get errors.
Fits: anything where a wrong answer is worse than no answer โ balances, inventory, locks, config, leader election.
Examples: ZooKeeper, etcd, HBase, Spanner.
During a partition, keep serving from whatever node you can reach. Reads may be stale, writes may conflict, and the system reconciles once the partition heals.
Fits: anything where a slightly stale answer is fine โ feeds, likes, product catalogs, view counts.
Examples: Cassandra, DynamoDB, Riak.
PACELC: The Part CAP Leaves Out
the trade exists even without partitionsCAP only speaks about the partition case, which is rare. PACELC finishes the sentence: if there is a Partition, choose Availability or Consistency; Else, in normal operation, choose Latency or Consistency.
That "else" is the honest part. Any system that replicates data faces a choice on every write: wait for replicas to acknowledge before returning (consistent, slower) or return as soon as one node has it (fast, possibly stale). You pay this tax all day, not just during the once-a-quarter partition.
| System | PACELC | Meaning |
|---|---|---|
| Cassandra, Dynamo | PA / EL | Stay available in a partition; favor latency otherwise. Tunable, but this is the default posture. |
| Spanner | PC / EC | Consistency always, even at the cost of latency. Pays with synchronized-clock waits. |
| Single-node RDBMS | โ / EC | No partition story; strongly consistent because there's one copy. |
| MongoDB (default) | PA / EC | Available under partition; consistent reads from the primary in normal operation. |
The Consistency Spectrum
linearizable โ eventual"Consistent" is not one thing. It's a ladder, and each rung down buys lower latency and higher availability by allowing one more kind of anomaly. Know the rungs and one concrete anomaly each, because that anomaly is how you explain the level to an interviewer.
STRONGER โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโถ WEAKER more coordination ยท higher latency less coordination ยท lower latency Linearizable โโโถ Sequential โโโถ Causal โโโถ Eventual single-copy global order cause-before replicas illusion, but no real-time -effect only, converge if no stale reads ordering concurrent free writes stop
| Level | Guarantee | Anomaly at this level |
|---|---|---|
| Linearizable | Every read returns the latest write; the system acts like one copy on a single timeline. | None. This is the gold standard, and the most expensive. |
| Sequential | All nodes agree on one order of operations, respecting each client's own order. | Two users may disagree on real-time timing: your write "happened first" on the clock but is ordered after mine. |
| Causal | Operations that are causally related are seen in order everywhere; unrelated ones may differ. | Concurrent writes can appear in different orders on different replicas โ but you never see a reply before the comment it answers. |
| Eventual | If writes stop, all replicas converge to the same value. That's the only promise. | Read-your-writes violation: you post a comment, refresh, and it's gone because you hit a lagging replica. |
Session Guarantees
cheap consistency where users noticeFull linearizability is often overkill. What users actually notice is inconsistency within their own session: they post something and it vanishes, or a page refresh shows older data than the last one. Session guarantees fix exactly those, and they're cheap because they only constrain one client's view, not the whole cluster.
| Guarantee | Promise | Bug it prevents |
|---|---|---|
| Read-your-writes | You always see your own updates. | Comment disappears right after you post it. |
| Monotonic reads | Once you've seen a value, you won't later see an older one. | Timeline jumps backward on refresh. |
| Monotonic writes | Your writes apply in the order you issued them. | Second edit lost behind the first. |
| Writes-follow-reads | A write you make is ordered after the read it was based on. | Reply appears before the post it answers. |
ACID vs BASE
two philosophies, one spectrum- Atomicity โ all of a transaction happens, or none of it.
- Consistency โ a transaction moves the DB from one valid state to another, preserving invariants.
- Isolation โ concurrent transactions don't see each other's partial work.
- Durability โ once committed, it survives a crash.
Cheap and natural on a single node. The classic home of relational databases.
- Basically Available โ the system answers, even if degraded.
- Soft state โ data may change without input as replicas sync.
- Eventual consistency โ replicas converge over time.
The design philosophy of AP systems: give up strict guarantees to stay up and stay fast at scale.
Choosing in an Interview
map the requirement to the modelThe move is not to pick one consistency level for the whole system. It's to look at each piece of data and ask what a wrong or stale answer costs. Most real designs mix levels: strong where money and identity live, eventual for the derived views that dominate traffic.
| Requirement | Model | Why |
|---|---|---|
| Payments, balances, inventory, seats | Strong / linearizable | Double-spend or overselling is unacceptable; correctness beats latency. |
| Usernames, locks, leader election | Strong / linearizable | "Exactly one" must be true across the cluster. |
| Comment threads, collaborative editing | Causal | Cause-and-effect order must hold; concurrent edits can be reconciled. |
| Your own posts and profile edits | Session (read-your-writes) | Users must see their own actions; others can lag briefly. |
| Feeds, likes, view counts, catalogs | Eventual | Read-heavy, tolerant of staleness; availability and latency win. |
Quick Reference
models on one card| Model | Guarantee | Example system | Anomaly avoided |
|---|---|---|---|
| Linearizable | Reads see the latest write, single-copy illusion | etcd, ZooKeeper, Spanner | Stale reads, lost order |
| Sequential | One global order respecting per-client order | Distributed logs | Reordered per-client ops |
| Causal | Cause precedes effect everywhere | MongoDB causal sessions | Reply before its comment |
| Read-your-writes | You see your own updates | Session layer on eventual store | Your write vanishing |
| Eventual | Replicas converge if writes stop | Cassandra, DynamoDB, DNS | Only guarantees convergence |