DevNotes
Support
System Design Basics · Chapter 08

CAP Theorem & Consistency Models

The CAP theorem stated correctly, CP vs AP systems with examples, PACELC and the latency trade-off, strong, eventual and causal consistency, ACID vs BASE, and how to choose consistency per feature.

◷ 5 min read§ 8 sections? 6 interview questions{ } 0 code examples

#Introduction

In a distributed system, data lives on several machines connected by a network that can fail. The CAP theorem and consistency models describe what guarantees are possible when that happens — and why "it depends" is the honest answer to many design questions.

#The CAP Theorem

A distributed data store can't simultaneously guarantee all three of:

PropertyMeaning
C — ConsistencyEvery read sees the most recent write (linearizability — as if there were one copy)
A — AvailabilityEvery request to a non-failing node gets a non-error response
P — Partition toleranceThe system keeps working when the network drops or delays messages between nodes

What CAP Really Says

Network partitions will happen, so P isn't optional. CAP really says: during a partition, you must choose between consistency and availability.

Network partition splits node A from node B
A write arrives at node A
Prefer consistency or availability?
B refuses reads and writes it can't confirm — errors or timeouts
B keeps answering, possibly with stale data

CP vs AP Systems

CP — consistency over availabilityAP — availability over consistency
Rejects or delays requests during a partition rather than return stale dataAlways answers, possibly with stale or conflicting data, and reconciles later
Examples: ZooKeeper, etcd, Kafka's KRaft metadata quorum, HBase, a relational primary with synchronous replicasExamples: Cassandra and DynamoDB (with default settings), DNS, CDN caches, shopping carts
Use for: bank balances, inventory at checkout, leader election, configurationUse for: social feeds, product catalogues, view counts, recommendations

Many databases are tunable: Cassandra with QUORUM reads and writes behaves much more like CP; with ONE it's AP.

#PACELC — the Rest of the Story

CAP only covers partitions. PACELC adds what happens the rest of the time:

If there's a Partition, choose Availability or Consistency; Else, choose Latency or Consistency.

Even without failures, strong consistency costs latency — synchronous replication and quorum reads mean waiting for other nodes, possibly in other regions.

SystemDuring a partitionNormally
Cassandra, DynamoDB (default)PAEL — fast, eventually consistent
Spanner, CockroachDBPCEC — consistent, higher latency

#Consistency Models

From strongest to weakest:

ModelGuaranteeExample
Strong (linearizable)Every read returns the latest write, as if there were one copyA single primary database; consensus systems
SequentialAll nodes see operations in the same order (not necessarily real-time)—
CausalOperations that are causally related are seen in order; unrelated ones may differA reply is never seen before the comment it answers
Read-your-writesA user always sees their own updatesAfter editing a profile, you see the edit
Monotonic readsOnce you've seen a value, you never see an older oneRefreshing doesn't "go back in time"
EventualIf writes stop, all replicas eventually convergeDNS, CDN caches, replicated feeds

#ACID vs BASE

ACID (traditional databases)BASE (many distributed / NoSQL systems)
Atomicity — all or nothingBasically Available — the system keeps responding
Consistency — constraints always holdSoft state — state may change without new input as replicas converge
Isolation — concurrent transactions don't interfereEventually consistent — replicas converge over time
Durability — committed data survives failures

Note that the C in ACID (integrity constraints) is a different idea from the C in CAP (all nodes agreeing on the latest value).

#Choosing Consistency per Feature

FeatureConsistency neededApproach
Money transfer, payment captureStrongACID transaction on one primary; idempotency keys
Seat or stock reservation at checkoutStrong for the decisionConditional update / row lock on the owning service
Order status shown to the customerRead-your-writesRead from the primary after a write, or return the new state
Product catalogue, prices on listing pagesEventual (seconds–minutes)Replicas, caches, CDN
Likes, views, follower countsEventual — approximate is fineCounters in Redis, batched writes
Search indexEventualUpdated from events / CDC

#Interview Questions

Q1What is the CAP theorem?

In a distributed data store, during a network partition you must choose between consistency (every read sees the latest write) and availability (every request gets a response). Partition tolerance isn't optional in a real network.

Q2Give examples of CP and AP systems.

CP: ZooKeeper, etcd, HBase, a primary database with synchronous replication — they refuse requests rather than return stale data. AP: Cassandra or DynamoDB with default settings, DNS, CDNs — they keep answering and reconcile later.

Q3What is PACELC?

An extension of CAP: during a partition choose availability or consistency; otherwise choose lower latency or stronger consistency.

Q4What is eventual consistency?

A guarantee that if no new writes occur, all replicas will eventually converge to the same value; reads in between may be stale.

Q5ACID vs BASE?

ACID provides atomic, consistent, isolated, durable transactions; BASE systems favour availability, allow soft state and converge eventually — trading immediate consistency for scale and availability.

Q6Would you design a banking system as AP or CP?

CP for balances and transfers — refusing or delaying an operation is better than double-spending — while non-critical parts (statements, notifications, analytics) can be eventually consistent.

#Key Takeaways

  • CAP: during a partition, choose consistency or availability — partitions can't be avoided.
  • CP systems refuse stale answers; AP systems answer and reconcile later.
  • PACELC adds the everyday latency-vs-consistency trade-off.
  • Consistency models range from strong to eventual; session guarantees fix most user-visible issues.
  • Choose consistency per feature: strong for money and inventory decisions, eventual for feeds and counters.