Library / Blogs
Please stop calling databases CP or AP
Why the CAP labels hide more than they reveal, and what to ask instead.
By Martin Kleppmann · martin.kleppmann.com · Added 2026-10-12
All content belongs to Martin Kleppmann / martin.kleppmann.com. We only link to it and give credit. Report a problem
Learn this article
About 37 min left. Your work is saved as you go.
What you are about to study
martin.kleppmann.com wrote this to explain how they thought about a problem. It assumes you are comfortable with distributed systems.
Ideas you will meet
- CAP Theorem
- Consistency, availability and partition tolerance trade-offs.
- Consistency Models
- Linearizable, causal and eventual consistency explained.
What we already know about this
Our own lessons on these ideas. Read them here before you start, so it makes sense.
System Design Foundations · 12 minConsistency, availability and the CAP theoremWhat goes wrong when data lives on several machines, and the vocabulary for choosing what to guarantee.Read here
The problem
On one machine, data has one value. Spread it over several machines and new questions appear:
- If I write to machine A, when can machine B see it?
- If A and B cannot talk to each other, should they keep serving requests?
- If two people change the same item at the same moment, who wins?
Consistency models are the answers to the first question. The CAP theorem frames the second.
Consistency: what a read may return
From strongest to weakest, in plain words:
- Strong (linearizable): once a write completes, every later read anywhere sees it. It behaves like one copy. Easiest to reason about, slowest and least available.
- Read-your-writes: you always see your own updates, though others may lag.
- Eventual: if writes stop, all copies will eventually agree. Reads may be stale in between. Fast and highly available.
100%Figure SDA-FND-07-1 · Replica lag and eventual consistency · © forcsguy.com
Stronger guarantees cost latency (replicas must coordinate) and availability (some must be reachable). Weaker ones are cheaper but push complexity to the application.
The CAP theorem
For a distributed data store, three properties are in tension:
- Consistency (C): every read sees the latest write (in the strong sense above).
- Availability (A): every request to a working node gets a response.
- Partition tolerance (P): the system keeps working even when messages between nodes are lost or delayed.
Networks do fail, so P is not optional. The real choice is what to do when a partition happens:
100%Figure SDA-FND-07-2 · Choosing C or A during a partition · © forcsguy.com
- CP systems (for example many coordination stores and strongly consistent databases) may reject requests rather than risk a wrong answer.
- AP systems (for example many Dynamo-style stores) stay up and let replicas diverge briefly, then merge.
A more useful lens: latency too
Even without partitions, strong consistency costs time because replicas must agree. The PACELC idea adds this: if there is a Partition choose A or C, Else (normal operation) choose between Latency and Consistency. It explains why a store may feel slower when configured to be strongly consistent.
100%Figure SDA-FND-07-3 · How many copies must confirm a write · © forcsguy.com
Resolving conflicts
If two replicas accept different writes, the system needs a rule:
- Last write wins: simple, but silently drops one update.
- Version vectors or merge logic: detect that the writes were concurrent, then merge or ask the user.
- CRDTs: data types designed so concurrent updates always merge cleanly (used for counters and collaborative editing).
Choosing per feature, not per system
You rarely need one consistency level for the whole product. Use strong consistency where wrong data is costly (payments, inventory, seat booking) and eventual consistency where speed and uptime matter more (counts, feeds, recommendations). Many databases let you pick per query.
A worked example: quorums with numbers
A key is stored on N = 3 replicas. A write must be acknowledged by W of them, and a read asks R of them and takes the newest version.
Setting W R R + W > N? What you get Fast reads 3 1 yes (4 > 3) Strong reads, but one dead replica blocks writes Balanced 2 2 yes (4 > 3) Strong reads; survives one replica down for both reads and writes Fast writes 1 3 yes (4 > 3) Quick writes, slow reads, reads fail if any replica is down Very fast 1 1 no (2 < 3) Fastest, but a read can miss the latest write: eventual consistency The rule R + W > N guarantees the set of replicas read overlaps the set that acknowledged the last write, so at least one has the newest value. Concretely, with W = 2, R = 2: a write of version 5 reaches replicas A and B; C is still at version 4. A read that asks B and C sees versions 5 and 4 and returns 5. Whichever two replicas you ask, at least one has version 5.
100%Figure SDA-FND-07-4 · Why read and write quorums overlap · © forcsguy.com
A story: the comment that disappeared
A user posts a comment. The write goes to the primary and the page reloads, but the read is served by a replica that is 300 ms behind. The comment is missing, the user posts it again, and now there are duplicates.
This is a read-your-writes violation, the most common user-visible consistency bug. Fixes, from cheapest to strongest:
- For a short time after a user writes (say 5 seconds), route that user's reads to the primary.
- Return the new item in the write response and show it on the page without re-reading.
- Track a version or log position from the write, and have the replica serve the read only when it has caught up to it.
- Make that data strongly consistent end to end (higher latency, fewer failure modes tolerated).
Not every read deserves strong consistency. A like counter that is a few seconds old is fine; a balance after a transfer is not.
What a network partition actually does
Imagine a three-node store split into two nodes on one side and one node on the other.
- A CP system (consistency over availability) lets only the side with a majority accept writes. The lone node refuses requests, which is unavailable but correct.
- An AP system (availability over consistency) lets both sides accept writes. Everyone gets an answer, but the two sides now hold different versions, and after the partition heals the system must merge: last-write-wins (simple, can lose data and trusts clocks), version vectors that keep both and ask the application, or data types designed to merge (CRDTs such as counters and sets).
100%Figure SDA-FND-07-5 · Behaviour during a partition · © forcsguy.com
Mistakes people make
- Reading CAP as "pick any two". Partitions happen whether you like it or not, so the real choice is what to give up during a partition. Without partitions you can have both.
- Assuming "eventual" means "soon enough". It means "if writes stop, replicas converge", with no bound. Measure the real lag.
- Last-write-wins on unsynchronised clocks. A node with a clock a second ahead silently overwrites newer data. Prefer logical versions.
- One setting for the whole system. Choose per feature: strong for payments and inventory, eventual for counters, feeds and recommendations.
- Forgetting the latency side. Even without partitions, stronger consistency costs round trips between replicas, a trade-off often called PACELC.
Try it yourself
Use the CAP theorem playground to partition a cluster and watch CP and AP behave differently, and the replication playground to see stale reads and what a failover loses. Then try the System Builder with replicas.
More on this site
Related reading
- Amazon Dynamo: Highly Available Key-Value StoreWerner Vogels · AmazonCase Studies
- Please stop calling databases CP or APMartin Kleppmann · martin.kleppmann.comBlogs
- CAP Twelve Years Later: How the Rules Have ChangedEric Brewer · InfoQBlogs
- Martin Kleppmann's BlogMartin Kleppmann · martin.kleppmann.comBlogs
Show 6 moreShow fewer
- Martin Kleppmann: Distributed Systems LecturesMartin Kleppmann · University of CambridgeYouTube
- Amazon S3 strong consistencyAmazon Web ServicesDocumentation
- Designing Data-Intensive ApplicationsMartin Kleppmann · O'ReillyBooks
- Read Consistency with Database Replicas at ShopifyShopify engineers · ShopifyCase Studies
- All Things DistributedWerner Vogels · AmazonBlogs
- Aurora BlogAurora engineers · AuroraBlogs
Low-Level Design Practice · 11 minElevator systemRequests, directions and scheduling: a state machine per car and a dispatcher that decides which car answers which call.Read here
The problem
A building has several elevators. People press a button on a floor (a hall call with direction) and a floor button inside the car (a car request). The system moves cars, opens doors and decides which car should answer each call.
Entities
100%Figure SDA-LLD-07-1 · Elevator classes · © forcsguy.com
One elevator as a state machine
100%Figure SDA-LLD-07-2 · Elevator car states · © forcsguy.com
A simple and effective scheduling rule is the elevator (scan) algorithm: keep two sorted sets of stops (above and below); continue in the current direction, serving stops in order; reverse only when none are left in that direction.
class Elevator { floor = 0; dir: "UP" | "DOWN" | "IDLE" = "IDLE"; up = new SortedSet<number>(); down = new SortedSet<number>(); // pending stops addStop(f: number) { (f >= this.floor ? this.up : this.down).add(f); if (this.dir === "IDLE") this.pick(); } private pick() { this.dir = this.up.size ? "UP" : this.down.size ? "DOWN" : "IDLE"; } step() { // called once per tick if (this.dir === "UP") { this.floor++; if (this.up.deleteIfPresent(this.floor)) this.openDoors(); if (!this.up.size) this.pick(); } if (this.dir === "DOWN") { this.floor--; if (this.down.deleteIfPresent(this.floor)) this.openDoors(); if (!this.down.size) this.pick(); } } }Many cars: the dispatcher
100%Figure SDA-LLD-07-3 · Assigning a hall call · © forcsguy.com
Make the dispatcher a strategy so you can swap "nearest car" for "least loaded" or a smarter estimate without touching
Elevator.Design notes
- Concurrency: calls arrive while cars move. Use one event loop per elevator, or a queue per car, so state changes happen one at a time.
- Safety: doors must not close on a blocked sensor; weight limits stop a car from moving.
- Peak modes: morning (send idle cars to the lobby) and evening patterns are policies.
- Extensions: express cars, destination dispatch (people enter their destination at the hall), emergency and maintenance modes.
Real-world reading
Related reading
- Apple Machine Learning BlogApple engineers · AppleBlogs
- AWS Machine Learning BlogAmazon Web Services engineers · Amazon Web ServicesBlogs
- Machine Learning for Ranking Answers End-to-End at QuoraQuora engineers · QuoraCase Studies
- Machine Learning (2 parts) at Condé NastCondé Nast engineers · Condé NastCase Studies
Show 6 moreShow fewer
- Google SRE Book: Distributed Periodic Scheduling with CronGoogle SRE team · GoogleBlogs
- Please stop calling databases CP or APMartin Kleppmann · martin.kleppmann.comBlogs
- Large-Scale Machine Learning Pipeline for Job Recommendations at IndeedIndeed engineers · IndeedCase Studies
- Machine Learning Platform at YelpYelp engineers · YelpCase Studies
- Engineering at QuoraQuora engineers · QuoraCase Studies
- Distributed SchedulingUmbcPapers
Go deeper on this
A path through videos, articles, other case studies and code on the same ideas as Please stop calling databases CP or AP.
Warm upGet the vocabulary first.
Courses
MIT 6.824: Distributed Systems
Free university course on fault tolerance, replication and consensus with Go labs.
MITAdvanced
Books · Free from the author
Concurrent and Distributed Systems (Cambridge lecture notes)
Eight lectures on time, broadcast, replication, consensus and transactions, with notes, slides and video. Free from the author under CC BY-SA.
University of CambridgeIntermediate
WatchHow others solve the same problem, on video.
Videos belong to their creators and play from YouTube. More on YouTube (opens in a new tab)
ReadEngineers explain it in their own words.
Blogs · Read here
CAP Twelve Years Later: How the Rules Have Changed
The author of the CAP conjecture revisits it and explains the trade-offs more carefully.
InfoQAdvanced
Blogs
Martin Kleppmann's Blog
Essays on distributed systems, CRDTs, consistency and data-intensive design.
martin.kleppmann.comAdvanced
See it in another companyThe same ideas, a different system.
Case Studies · Read here
Amazon Dynamo: Highly Available Key-Value Store
Werner Vogels' write-up of the design that inspired DynamoDB, Cassandra and Riak.
AmazonAdvanced
Case Studies · Read here
Read Consistency with Database Replicas at Shopify
At Shopify, we’ve long used database replication for redundancy and failure recovery, but only fairly recently started to explore the potential of replicas as an alternative read-only data source for applications.
ShopifyIntermediate
Read the codeSee how it looks when it is real.
GitHub
System Design (Karan Pratap Singh)
A free, structured system design course in markdown, from IP and DNS to CAP and microservices.
karanpratapsinghBeginner
Keep learning
Loading community…