Almost every engineer has heard "CAP theorem: consistency, availability, partition tolerance, pick two". That summary is misleading enough to cause bad decisions. This post goes through what CAP actually says, why the PACELC framing is more useful most of the time, and what the different consistency models look like to the people using your product.
What CAP says
The CAP theorem, as proved by Gilbert and Lynch, concerns a system that stores data on several nodes. It says that if the network partitions, so some nodes cannot reach others, the system must choose between:
- Consistency (here meaning linearizability): every read sees the most recent write, as if there were a single copy of the data.
- Availability: every request to a non-failed node gets a response, without waiting for the partition to heal.
You cannot have both during a partition. A node cut off from the others either refuses to answer (or waits), staying consistent, or answers from its own possibly stale data, staying available.
Three common misreadings:
- "Pick two of three." Partitions are not optional in a distributed system; networks fail. The real choice is what to do when a partition happens: C or A.
- "CA systems." A single-node database is "CA" only in the sense that it has no network between nodes to partition. Once you add a replica, you are choosing again.
- "Consistency" means ACID consistency. It does not. The C in CAP is linearizability, a property about ordering of reads and writes across nodes, not about constraints in a transaction.
Why PACELC is more useful day to day
Partitions are rare. Most of the time the network is fine, and CAP says nothing about that time. PACELC, proposed by Daniel Abadi, extends it:
If there is a Partition, choose between Availability and Consistency; Else, choose between Latency and Consistency.
The "else" half is where most design decisions live. Keeping replicas consistent on every write requires waiting for other nodes to acknowledge it, which costs latency on every request, not just during failures. Asynchronous replication is fast and lets replicas lag.
Common classifications (with the caveat that most systems are configurable):
| System (typical config) | During partition | Normal operation |
|---|---|---|
| PostgreSQL primary + async replicas, reads from replicas | replicas keep serving stale reads (A) | low latency, stale replica reads (L) |
| PostgreSQL with synchronous replication, reads from primary | writes block if sync replica unreachable (C) | higher write latency (C) |
| Cassandra / DynamoDB-style, low quorum | A | L |
| Same, quorum reads and writes | C for those operations | C, higher latency |
| etcd, ZooKeeper (consensus) | minority side refuses writes (C) | C, latency of a majority round trip |
The point is not to label a database once. It is to see that each configuration, and often each query, makes a trade-off.
Consistency models, from what the user sees
Between "always the latest" and "eventually the same", there are several models. Here is each one with an example from our own product.
Linearizable (strong) consistency
Guarantee: once a write completes, every subsequent read, from anyone, anywhere, sees it.
User-visible example: a username is unique. When Ana claims ana_k, nobody else can claim it a moment
later from another region. Unique constraints, stock for the last item, and leader election need this.
Cost: every such operation goes through a single leader or a majority of nodes. Cross-region, that is tens to hundreds of milliseconds per write.
Sequential and causal consistency
Causal guarantee: if one event could have influenced another, everyone sees them in that order. Unrelated events can appear in different orders to different users.
User-visible example: in a comment thread, Ben replies to Ana's comment. Causal consistency guarantees nobody sees Ben's reply without Ana's comment above it. Two unrelated comments posted at the same time may appear in different orders for different readers, and nobody cares.
Cost: lower than linearizability; can be served locally once dependencies are present.
Read-your-writes
Guarantee: after you write something, you will see it on your subsequent reads. Others may not yet.
User-visible example: you update your profile photo and reload the page. You see the new photo, even if a follower in another region still sees the old one for a second. Without this guarantee, the user thinks the save failed and tries again. This was one of our most common support complaints after we started reading from replicas, until we routed a user's reads to the primary for a few seconds after their writes.
Monotonic reads
Guarantee: once you have seen a value, you never see an older one later.
User-visible example: a notification count goes from 5 to 6, then back to 5 on the next page load because the request hit a more lagging replica. Monotonic reads prevent that, usually by pinning a session to one replica or by tracking the last version seen.
Eventual consistency
Guarantee: if writes stop, all replicas eventually converge to the same value. Nothing about when, or what you see in the meantime.
User-visible example: the like count on a popular post shows 1,204 for one user and 1,209 for another for a few seconds. Search results do not include a product added a moment ago. View counters, recommendations, analytics dashboards are fine with this.
Cost: lowest latency and highest availability.
How we choose, per feature
We do not pick one model for the whole system. We pick per piece of data, based on what goes wrong if a user sees stale or reordered data:
| Data | Model we need | How we get it |
|---|---|---|
| Usernames, email uniqueness | linearizable | unique constraint on the primary |
| Last-item stock, seat booking | linearizable | conditional update on the primary |
| Account balance shown after a payment | read-your-writes | read from primary for a short window after write |
| Comment threads | causal | replies stored with parent, rendered only when parent present |
| Notification counts | monotonic reads | session pinned to one replica |
| Like counts, view counts | eventual | async counters, cached |
| Search index | eventual (seconds) | change events into the search cluster |
The question we ask in design reviews is concrete: "If a user sees a value that is two seconds old here, what happens?" If the answer is "nothing", eventual is fine and cheaper. If the answer is "they double-book a seat", it is not.
During a partition, decide the product behaviour in advance
The CAP half of PACELC rarely happens, but when it does, the behaviour should be a decision, not an accident. For a multi-region deployment we wrote down, per feature:
- Checkout: prefer consistency. If the region cannot reach the primary for inventory, show "temporarily unavailable" rather than risk overselling.
- Browsing and search: prefer availability. Serve from local replicas and caches even if slightly stale.
- Posting comments: prefer availability. Accept locally, queue for replication, show a "posting" state.
These decisions are product decisions as much as technical ones, which is why product managers sat in on those reviews.
Summary
- CAP is about what happens during a partition: answer with possibly stale data (A) or refuse (C).
- PACELC adds the everyday trade-off: even without partitions, stronger consistency costs latency.
- Between linearizable and eventual there are causal, read-your-writes and monotonic reads, each preventing a specific, visible anomaly.
- Choose per feature by asking what a stale or reordered value would cost the user, and decide partition behaviour in advance.
References: Gilbert and Lynch, Brewer's Conjecture and the Feasibility of Consistent, Available, Partition-Tolerant Web Services, Abadi, Consistency Tradeoffs in Modern Distributed Database System Design, Jepsen consistency models.
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.