Consistency: one word, three guarantees
One word, three meanings
Section titled “One word, three meanings”Ask three engineers what “consistent” means and you can get three correct, different answers. All three are in common use, and treating them as one is a source of real design errors.
- ACID consistency. After every transaction, the data obeys the rules of the application. A balance is never negative, a foreign key never points at a missing row, the total of all accounts does not change in a transfer. It is about the validity of the data.
- Consistency in CAP. The system behaves as if there were a single, always-up-to-date copy of the data. Once a write has completed, every read that follows sees it, whichever node answers. This property has a more exact name: linearizability. It is about how copies agree.
- Eventual consistency. Copies may disagree for a while, and if there are no more updates, they converge on the same value. It is the weakest of the three, and it is a real guarantee.
Mental model. ACID consistency is about the rules the data must obey. CAP consistency and eventual consistency are about the copies of the data and when they agree. A system can have either kind and not the other.
Martin Kleppmann puts the first confusion plainly: the C in ACID and the C in CAP merely share a letter. A database sold as “ACID” is not thereby linearizable, because many databases deliberately give up linearizability to be faster.
Try telling them apart
Section titled “Try telling them apart”Eight statements. Which kind of consistency does each describe?
Challenge
Which kind of consistency?
The word consistency is used for at least three different guarantees. Put each statement under the one it describes.
Sorting needs JavaScript.
Categories
- ACID consistency
- The data obeys the rules of the application after every transaction.
- Linearizability (the C of CAP)
- The system behaves as if there were one copy, always up to date.
- Eventual consistency
- Copies may disagree for a while, and converge when writes stop.
Check your answers at least once before looking at the answers.
What linearizability promises
Section titled “What linearizability promises”Imagine a value x stored on two replicas, A and B. A client writes x = 2 to A, and the write completes. Another client then reads x from B.
time ──────────────────────────────────────────────▶
client 1 write x = 2 ──▶ A ✔ (completed)replica A x = 1 ────────────── x = 2replica B x = 1 ─────────────────────── x = 1 ──── x = 2client 2 read x from B ──▶ returns 1 ✘A linearizable system cannot return 1 to client 2: the write had completed before the read began, so the read must see it, or fail. A system that returns 1 is not linearizable. It can still be a perfectly good system: it is eventually consistent, because B catches up a moment later.
Linearizability is expensive for a reason. To give that guarantee, B has to either check with A before answering, which costs a round trip and fails if A cannot be reached, or know that it is up to date. That is the cost the CAP theorem describes.
CAP, stated carefully
Section titled “CAP, stated carefully”The CAP theorem (Gilbert and Lynch, 2002, after Eric Brewer’s conjecture) is often summarised as “pick two of three”. That summary is wrong in a way that matters, and the module on distributed systems goes into it. The core is this:
- Here, consistency means linearizability: every read receives the most recent write, or an error.
- Availability here means that every request received by a node that is working gets a non-error response.
- Partition: the network drops or delays messages between nodes.
Networks do drop messages, so a partition is not a choice but something that happens. When a partition occurs, a system must choose between answering from a node that may be out of date (available, not linearizable) and refusing to answer (linearizable, not available). When there is no partition, the theorem says nothing against having both.
Two cautions from Kleppmann. The CAP definitions of consistency and availability are narrow, and are not what most people mean by a “consistent” or a “highly available” system, so labels such as “CP database” and “AP database” tell you little about how a real product behaves. And many databases are neither CAP-consistent nor CAP-available in the strict sense.
What eventual consistency promises
Section titled “What eventual consistency promises”“Eventual consistency” is often taken to mean “no guarantees”. It does promise something. Werner Vogels, who popularised the term, states it like this: if no new updates are made to an object, eventually all accesses will return the last updated value.
That is a real guarantee, and it rules out a great deal: copies that diverge for ever, or updates that are silently dropped. What it does not promise is when, or what a read returns in the meantime. A read may return an old value, and two clients may see different values at the same moment. If you cannot live with that for a given piece of data, such as the balance that a withdrawal depends on, you need a stronger guarantee for it, and you pay for it in latency or availability.
Try it: a cache is a copy that goes stale
Section titled “Try it: a cache is a copy that goes stale”You met a deliberate, visible example of weak consistency in the caching module. A cache holds a copy of the database, and the copy can be out of date. The lab below shows how stale the cache’s answers are, and how that changes with the TTL.
A TTL ends the life of every entry, whatever happens in the database. With deletion on write switched off, the TTL is the only thing that makes cached data fresh again. Change the lifetime and the write rate, and watch how much of what the cache serves is out of date.
- Cache hit
- Cache miss, goes on to the database
- Waits for another request’s read
- Request fails
Each dot stands for many requests. The numbers show the real rate.
81% of cache hits are stale: the database has changed since the entry was stored.
History
Stale share of hits
84%Use the left and right arrow keys to read values at each moment.
Hit ratio
100%Use the left and right arrow keys to read values at each moment.
Database reads per second
0/sUse the left and right arrow keys to read values at each moment.
Data table
| Simulated time | Stale share of hits | Hit ratio | Database reads per second |
|---|---|---|---|
| 5 s | 38% | 100% | 0/s |
| 10 s | 66% | 100% | 0/s |
| 15 s | 76% | 100% | 0/s |
| 20 s | 72% | 100% | 0/s |
| 25 s | 80% | 100% | 0/s |
| 30 s | 84% | 100% | 0/s |
| Component | State | Utilisation | Queue |
|---|---|---|---|
| Cache, Shard 1 | Healthy | <1% | 0 |
| Cache, Shard 2 | Healthy | <1% | 0 |
| Cache, Shard 3 | Healthy | <1% | 0 |
| Cache, Shard 4 | Healthy | <1% | 0 |
| Database | Healthy | 1% | 0 |
- Traffic
- 299/s
- Successful
- 299/s
- Error rate
- 0%
- Hit ratio
- 100%
- Database reads
- 5.4/s
- p50
- 0.14 ms
- p95
- 0.69 ms
- p99
- 5.3 ms
- Entries cached
- 0 / 100
- Requests that write
- 2%
- Stale hits
- 81%
- Coalesced requests
- 0/s
- Skipped the cache
- 0/s
Figures describe the last 5 simulated seconds.
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- Each request asks for a key drawn independently from a fixed popularity distribution. Real traffic has bursts and trends that this does not capture.
- Every operation on a cache shard (lookups, stores, deletions) joins one queue served by a few slots, so a busy shard slows everything on it.
- Every write changes the single value of its key. A cached copy is stale when the database has changed since it was read.
Simplifications
- The model counts stale reads. It does not measure how old the data is, only that it is behind.
- An entry’s lifetime starts when it is stored. Reading it does not extend it.
- Only a version number is tracked for each key, not the data. A cached copy is stale when its version is behind the database’s.
- The cache never runs out of memory, so no entry is evicted early. Real caches evict the least recently used entries when they are full.
- A write updates the database and then deletes the cached copy (cache-aside). Write-through and write-behind are compared in the trade-off explorer, not simulated here.
- Refresh before expiry reloads an entry in the background when a request reads it in the last part of its life. Production systems often use a probabilistic variant.
- A cache that goes down loses everything it held and comes back empty. A cache with persistence would not.
- The application learns that the cache is down at once, on the request that fails. Health checks and circuit breakers are not modelled.
- When a caller times out and gives up, the callee still finishes the work, so abandoned requests keep consuming capacity.
Parameter values are illustrative. They are not benchmarks of any real product.
Move Entry lifetime (TTL) from 30 seconds down to 1 second. With the lifetime at 30 seconds, about 65% of the cache’s hits return a copy that was already out of date. At 1 second, 9% do. The price is that the hit ratio falls from 99% to 78%, and the database does about twenty times as much work.
This is the trade in its plainest form. A copy is faster and cheaper to read, and it is less consistent than the original. A TTL is a bound on how stale it may be. Switching on Delete the cached copy on write removes most staleness, at the cost of an extra miss for each write. Nothing here is a bug. Each setting is a choice about how much inconsistency to accept for how much speed.
What happens if you read from a lagging replica?
Section titled “What happens if you read from a lagging replica?”A shop writes new prices to a primary database and reads them from replicas. Replication takes about 200 ms to reach a replica. A seller changes a price and the page reloads 50 ms later, with the read going to a replica.
The page shows the old price. The seller concludes that the change failed, and changes it again. Nothing was wrong: the replica was 150 ms away from catching up.
This is an eventually consistent read, and it is a problem not because the system is broken but because the seller expected to see their own write. The usual answers are narrow guarantees rather than full linearizability: read from the primary for a short time after a write, or remember the time of the user’s last write and refuse a replica that has not caught up to it. Which guarantee a feature needs is a design decision, and the replication module covers the options.
- “Consistent” can mean three things: the data obeys its rules (ACID), the system behaves like one up-to-date copy (linearizability, the C in CAP), or copies converge once writes stop (eventual consistency).
- ACID consistency is about the validity of the data. The other two are about how copies agree.
- Linearizability means that after a write completes, every read sees it. It costs a round trip, and it fails when a node cannot be sure.
- When a network partition happens, a system chooses between answering from a node that may be stale and refusing to answer. “Pick two of three” is not what the theorem says.
- Eventual consistency promises convergence once updates stop. It does not promise when, or what a read returns meanwhile.
- A copy is faster and cheaper than the original and less consistent, and a TTL is a bound on how stale it can be.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.