Alice posts “I’m getting married!” Bob replies “Congratulations!” Charlie opens the app and sees Bob’s reply, but not Alice’s post. “Congratulations for what?”
Nothing crashed. No data was lost. The reply simply reached Charlie’s replica before the post did. In the previous part we kept copies of data on several machines, and copies fall behind. A consistency model is the promise a system makes about what a read can return while that’s happening.
There isn’t one right model. There’s a spectrum, from “every read sees the very latest write” to “everyone will agree, eventually”. Stronger promises cost speed and availability; weaker ones cost surprises. Most real systems use several at once, one per kind of data.
The consistency spectrum
| Model | The promise | Cost | Good for |
|---|---|---|---|
| Strong (linearizable) | Every read sees the latest completed write | Slowest; may refuse requests during failures | Balances, inventory, bookings, locks |
| Sequential | Everyone sees all writes in the same order | Slow | Coordination, replicated logs |
| Causal | If A caused B, everyone sees A before B | Medium | Comments, chat, social feeds |
| Read-your-writes | You always see your own latest writes | Low | Profile edits, settings, posts |
| Monotonic reads | Your reads never go back in time | Low | Feeds, dashboards, any paged list |
| Eventual | If writes stop, all copies converge | Lowest | Likes, view counts, DNS, product pages |
What is strong consistency (linearizability)?
Strong consistency means the system behaves as if there were one copy of the data. Once a write completes, every read, from anyone, anywhere, returns that value or a newer one. The formal name is linearizability.
You get it by making reads and writes agree on one copy: read and write through the leader, use synchronous replication, or use a consensus system (like etcd, ZooKeeper, or Spanner) where a majority must agree on each write.
It’s what you need when two people competing for the same thing must never both win:
// Java · the last ticket: one atomic, conditional update on the leader
@Transactional
public boolean book(long eventId) {
int updated = jdbc.update(
"UPDATE events SET seats_left = seats_left - 1 WHERE id = ? AND seats_left > 0",
eventId); // runs on the leader, never a replica
return updated == 1; // 0 means sold out
}
The cost: every operation waits for coordination, so it’s slower, and when replicas can’t talk to each other the system may refuse requests rather than risk a wrong answer. That trade-off is exactly what the CAP theorem is about.
Sequential consistency is a slightly weaker cousin: everyone sees all writes in the same order, but that order doesn’t have to match real time. It’s mostly found inside coordination systems and CPU memory models; in application design you’ll usually choose between the others.
What is causal consistency?
Most data doesn’t need a single global order. It needs cause before effect. A reply depends on the post. An answer depends on the question. A “payment received” email depends on the payment.
Causal consistency guarantees that if one write could have been influenced by another, everyone sees them in that order. Writes that have nothing to do with each other can still arrive in any order, which keeps the system fast and available.
The usual way to build it: each write carries what it depends on (a reply_to ID, or a version vector). A replica doesn’t show a write until its dependencies have arrived.
// Java · show a reply only after the post it depends on
public void deliver(Message m, Feed feed) {
if (m.replyTo() == null || feed.contains(m.replyTo())) {
feed.add(m);
feed.releaseWaitingOn(m.id()); // replies that were waiting for this one
} else {
feed.holdUntil(m.replyTo(), m); // buffer until the parent arrives
}
}
What is read-your-writes consistency?
You change your profile picture, refresh, and see the old one. To you, the save failed, so you upload it again. Everyone else seeing the old picture for a second is fine; you seeing it is not.
Read-your-writes (also called read-after-write) promises that you always see your own writes. Other users may lag a little.
Three ways to build it, from simplest:
- Read your own data from the leader for a short window (say 10 seconds) after you write.
- Remember the write’s position in the replication log and only read from a replica that has caught up to it. In PostgreSQL, store the commit’s log position (LSN) in the session and compare it with each replica’s
pg_last_wal_replay_lsn(). - Update the screen from the write itself. The client already knows the new value; it doesn’t need to read it back.
// Java · read-your-writes with a log position, not a timer
public Profile profile(long userId, Session s) {
for (Replica r : replicas.healthy()) {
if (r.replayedLsn().compareTo(s.lastWriteLsn()) >= 0) { // caught up to my write
return r.query(PROFILE_SQL, userId);
}
}
return leader.query(PROFILE_SQL, userId); // none caught up yet
}
What are monotonic reads?
You see 5 comments on a post. You refresh, land on a different replica that’s further behind, and now there are 3. Refresh again: 5. The data isn’t wrong, but it looks broken, because time went backwards.
Monotonic reads promise that once you’ve seen a value, you’ll never see an older one. The simple fix is to pin each user to one replica (for example, hash(userId) % replicas), falling back to another only if it fails. A stricter fix is the log-position check above: never read from a replica that’s behind your last read.
Read-your-writes and monotonic reads are called session guarantees. They’re cheap, and they fix most “the app is glitching” complaints without making the whole system strongly consistent.
What is eventual consistency?
Eventual consistency is the weakest common promise: if no new writes arrive, all replicas will eventually agree. It says nothing about how long that takes, or what you see in the meantime.
That sounds alarming, but much of the internet runs on it:
- DNS: change a record, and resolvers around the world pick it up over minutes to hours as caches expire.
- Like counts and view counts: 1,042 or 1,047 for a few seconds hurts nobody.
- Product pages, search indexes, recommendations: a slightly old description is fine.
In return you get the fastest reads and writes, and a system that keeps working even when replicas can’t reach each other. The questions to ask are: how stale can this be, for how long, and who would notice? If the honest answer is “a few seconds, and nobody”, eventual consistency is the right choice.
Many databases let you choose per request. DynamoDB reads are eventually consistent by default, and you can ask for a strongly consistent read when it matters:
// Java · AWS SDK v2: one strongly consistent read, at about twice the read cost
GetItemResponse r = dynamo.getItem(GetItemRequest.builder()
.tableName("accounts")
.key(Map.of("id", AttributeValue.fromS(accountId)))
.consistentRead(true) // default is false: eventually consistent
.build());
Pick per feature, not per system
The most useful habit: don’t choose one model for the whole app. Choose for each kind of data, based on what goes wrong if a read is stale.
What breaks, and how to fix it
| What breaks | What users see | The fix |
|---|---|---|
| Stale read after own write | “My change didn’t save” | Read-your-writes: leader for a short window, or log-position checks |
| Reads jump between replicas | Counts and lists going backwards | Monotonic reads: pin users to a replica |
| Effect before cause | Replies to posts that aren’t there | Causal: carry dependencies, hold until they arrive |
| Eventual window too long | Old data for minutes, not seconds | Monitor replication lag, alert on it, fix the slow replica or consumer |
| Strong consistency everywhere | Slow pages, outages during network trouble | Downgrade non-critical reads to eventual |
Puzzle 1: the phantom balance
This user needs read-your-writes; other readers don’t. After a deposit, record the write’s log position (or just the time) in the user’s session. For that user’s next balance reads, use only a replica that has caught up to that position, or the leader if none has. Everyone else keeps reading replicas freely.
For money, also make the app honest: show the balance with “as of” time, and update the screen from the deposit response itself, so the user sees ₹1,500 the instant it’s confirmed.
Puzzle 2: the disappearing comment
Causal consistency. The comment depends on the post, but the two travelled to Charlie’s replica separately and arrived in the wrong order.
Every comment already knows its parent (reply_to). Use it: when a comment arrives before its parent, hold it and show it once the parent is there (or fetch the parent directly from its source). Unrelated posts don’t wait on each other, so the rest of the feed stays fast.
Puzzle 3: the concurrent edit
Both edits should survive, and every copy must end up identical: say “!!!Hello World” on every screen. Last-write-wins would throw one edit away, which users never accept in a document.
Collaborative editors solve this with operational transformation (adjusting each edit’s position to account for concurrent ones, as Google Docs does) or CRDTs (data structures designed so concurrent edits always merge the same way). Both give strong eventual consistency: replicas may differ for a moment, but they always converge to the same text, and no edit is lost.
Key takeaways
- A consistency model is a promise about what reads can return while replicas catch up.
- Strong consistency (linearizability) acts like one copy: correct, but slower and less available. Use it for money, stock and bookings.
- Eventual consistency is fast and always on, but stale for a while. Use it where a few seconds of staleness hurts nobody.
- Causal consistency keeps cause before effect. Use it for comments, replies and chat.
- Read-your-writes and monotonic reads are cheap session guarantees that fix most “glitchy app” bugs.
- Choose per feature. Ask: what goes wrong if this read is stale?
Next in the Consistency & CAP theorem thread: the CAP theorem, why a distributed system can’t stay both perfectly consistent and fully available when the network fails, and what that really means for your design.
Comments