Lesson 237 of 296
CAP, consistency models and eventual consistency
What you will learn
- The CAP theorem, explained correctly
- PACELC: latency vs consistency
- Strong vs eventual consistency
- Other consistency guarantees users notice
- Quorums
- How to talk about consistency in a design
On this page
The CAP theorem
In a distributed data system, when a network partition happens (some nodes cannot talk to others), you must choose between:
- Consistency (C): every read gets the latest write (or an error).
- Availability (A): every request gets a non-error response, even if it might not be the latest data.
You cannot have both during a partition. Partition tolerance (P) is not really optional for systems spread over a network, because partitions happen. So the real choice is CP or AP when a partition occurs:
| Choice | During a partition | Examples |
|---|---|---|
| CP | Refuse some requests rather than return stale or conflicting data | A bank ledger, a booking system's seat allocation, ZooKeeper, etcd, a single-primary relational database (unreachable from the cut-off side) |
| AP | Keep answering, possibly with stale data, and reconcile later | Shopping basket, social feed, likes counter, DNS, Cassandra and DynamoDB in their default modes |
Common mistake: saying "pick two of three". You do not pick P away; you pick what happens when a partition occurs.
PACELC
CAP only talks about partitions. PACELC adds the normal case: if Partition, choose A or C; Else, choose Latency or Consistency. Even without failures, making every replica agree (strong consistency) adds latency. Many systems choose lower latency and weaker consistency most of the time.
Consistency models
| Model | Meaning | Example |
|---|---|---|
| Strong (linearisable) | Once a write is confirmed, every later read sees it | Reading from the primary database |
| Eventual | If writes stop, all replicas eventually agree; meanwhile reads may be stale | DNS changes, read replicas, DynamoDB default reads, CDN content |
| Read-your-writes | A user always sees their own writes | After editing a profile, you see the change, even if others see it a little later |
| Monotonic reads | A user never sees data go backwards in time | You do not see a comment, refresh, and see it vanish |
| Causal | If event B depends on A, everyone sees A before B | A reply never appears before the message it replies to |
Users mainly notice the last three. Designs often provide eventual consistency in general plus read-your-writes for the user's own actions (Lesson 235).
Quorums
Replicated stores like Cassandra and DynamoDB-style systems use quorums: with N replicas, a write must be acknowledged by W replicas and a read must query R replicas.
- If R + W > N, every read set overlaps every write set, so a read sees the latest write (with N = 3, W = 2, R = 2).
- Lower W or R gives faster, more available operations with weaker consistency.
Choosing per feature
Consistency is a per-feature decision, not one setting for the whole system:
| Feature | Needs |
|---|---|
| Account balance, payment status, stock for the last item, seat booking | Strong consistency (or careful locking and constraints) |
| Like counts, view counts, follower counts | Eventual is fine |
| News feed | Eventual, with read-your-writes for your own posts |
| Search index | Eventual (seconds behind the database) |
| Analytics dashboards | Eventual (minutes or hours are fine) |
How to say it in an interview
"CAP says that during a network partition a distributed store must choose between consistency, refusing or failing some requests, and availability, answering with possibly stale data; partition tolerance is not optional across a network. PACELC adds that even without partitions there is a latency versus consistency trade-off. I choose per feature: strong consistency for money, bookings and stock, eventual consistency for counters, feeds, search and analytics, and I add read-your-writes and monotonic reads so users do not see confusing behaviour. Quorum systems are consistent when R plus W is greater than N."
Key words
| Word | Meaning |
|---|---|
| Network partition | Nodes unable to communicate with each other |
| CAP theorem | During a partition, choose consistency or availability |
| PACELC | Otherwise, choose latency or consistency |
| Strong consistency | Reads always see the latest confirmed write |
| Eventual consistency | Replicas agree after a delay |
| Read-your-writes | Users always see their own changes |
| Quorum | The number of replicas that must respond |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.