A consistency model is the promise a distributed data store makes about reads: after a write succeeds, which values is a later read allowed to return? Strong models promise the latest value every time. Weaker models allow old values for a short while in exchange for speed and availability. Choosing a consistency model means deciding how stale each piece of data is allowed to be.

In this article we follow one example the whole way: a food delivery app in Hyderabad. The wallet, the order status, the chat with the rider, the delivery address and the restaurant ratings all live in the same system, yet each one needs a different consistency model. Every model comes with runnable Java code, plus Python and C++.

Key takeaways

  • Consistency models exist because data is copied to many machines, and copies are updated at different times.

  • The models form a ladder: linearizable (strongest), sequential, causal, session guarantees, eventual (weakest).

  • Stronger models need more coordination between nodes, so they cost latency and availability.

  • Session guarantees such as read-your-writes and monotonic reads fix most user-visible bugs cheaply.

  • Eventual consistency is only safe with a proper merge rule; last-writer-wins can silently lose updates.

  • Real systems mix models: strong for money and stock, weaker for counts, feeds and search.

1. What Is a Consistency Model?

On one machine there is one copy of the data, so a read always returns the last write. Once data is replicated to several machines for scale and availability, a write reaches the copies at different times. A consistency model is the contract between the data store and the application that says which of those in-between states a read may expose.

Note that a consistency model is about the history of reads and writes, not about whether the data is "correct" for the business. If the app writes the wrong price, a strongly consistent database will faithfully return the wrong price everywhere.

This is also a different idea from the C in ACID. ACID consistency means a transaction keeps the database's rules (for example, a balance never goes below zero). Distributed consistency means the copies agree with each other. The CAP theorem uses the second meaning.

2. The Food Delivery App We Will Use

All writes go to a primary database. Reads are spread across read replicas so the primary is not overloaded at lunch time. Changes are copied to the replicas, the cache and the search index asynchronously, which means the primary does not wait for them.

FeatureWhat goes wrong with stale dataModel we will pick
Wallet balance and paymentsMoney is spent twiceStrong (linearizable)
Last plate of a dish in stockTwo people get the same plateStrong (linearizable)
Delivery address, cart, order statusUser sees their change disappear or go backwardsRead-your-writes + monotonic reads
Chat between customer and riderA reply appears before the questionCausal
Restaurant ratings, view counts, searchA number is slightly old for a few secondsEventual

3. Why Copies Disagree: Replication Lag

Every consistency problem starts with replication lag: the time between a write on the primary and that write reaching a replica. Most of the time the lag is a few milliseconds. During a traffic spike, a long-running query or a slow network link, it can grow to seconds.

Priya changes her delivery address from Ameerpet to Madhapur. The fast replica gets the change in 20 ms, but the slow replica needs 300 ms. If the rider's app reads from the slow replica in between, the rider goes to Ameerpet.

Output

t=0ms  write on primary: address = Madhapur (v2)
t=50ms  read from primary: Madhapur (v2)
t=50ms  read from replica-A: Madhapur (v2)
t=50ms  read from replica-B: Ameerpet (v1)  <- STALE
--- 300 ms later ---
t=350ms  read from replica-B: Madhapur (v2)

Dry run. At t=0 the primary moves to v2 (Madhapur). At t=50 ms replica A has received its copy (it arrived at 20 ms), but replica B's copy only arrives at 300 ms, so B still returns Ameerpet (v1) and the read is marked STALE. At t=350 ms the copy has arrived and B returns Madhapur. Nothing was lost; the data was simply late. Each consistency model below is a rule about what the app may see during that late window.

4. The Consistency Spectrum

Consistency models are not a yes/no switch. They form a ladder. Each step down allows more stale or reordered reads, and in return needs less coordination between nodes, so it is faster and stays available during more failures.

ModelPromise to the readerCostDelivery app example
LinearizableBehaves like one copy; once a write is confirmed every read sees itHighest latency; unavailable on the minority side of a partitionWallet balance
SequentialEveryone sees writes in the same order, but possibly lateHighRarely chosen directly
CausalIf B depends on A, nobody sees B without AMedium (track dependencies)Order chat
Session guaranteesOne user sees their own writes and never goes backwardsLow (a version token per user)Address, cart, order status
EventualCopies agree once writes stopLowestRatings, view counts

5. Strong Consistency (Linearizability)

Linearizability is the strongest common model. The system behaves as if there were only one copy of the data and every operation happened at a single instant between its start and its end. Once a write is confirmed, every read anywhere returns that value or a newer one. It is what people usually mean by "strong consistency".

The usual way to build it: one leader orders all writes, a write is confirmed only after a majority of nodes store it (this is what Raft and Paxos do), and reads go through the leader (or check with a majority) so they cannot see an old value.

Output

== Strong: every check reads from the leader ==
Pay Rs 300: balance check on leader sees Rs 500
  CONFIRMED after 2/3 acks (majority)
Pay Rs 400: balance check on leader sees Rs 200
  REJECTED: not enough balance
  balances: leader=200 follower-1=200 follower-2=500
== Weak: the second check reads a lagging follower ==
Pay Rs 300: balance check on leader sees Rs 500
  CONFIRMED after 2/3 acks (majority)
Pay Rs 400: balance check on follower-2 sees Rs 500
  CONFIRMED after 2/3 acks (majority)
  balances: leader=-200 follower-1=-200 follower-2=500

Dry run. In the strong run, the Rs 300 payment is stored on the leader and follower 1 (2 of 3), so it is confirmed. The Rs 400 check reads from the leader, sees Rs 200 and is rejected. In the weak run, the second check reads follower 2, which never received the first payment, sees Rs 500 and approves it. The wallet ends at Rs -200: a double spend. For money and stock, a stale read is not a display glitch; it is a real loss.

The cost is latency (every write waits for a majority, often across data centres) and availability (if the leader cannot reach a majority, it must refuse writes). Use linearizability only where a stale value can lose money, stock, locks or permissions.

6. Sequential Consistency

Sequential consistency promises that all users see all writes in the same order, and each user's own writes appear in the order they made them. It does not promise that a read sees a write that just finished on someone else's screen. In other words, everybody watches the same film, but some people are a few seconds behind.

In interviews, the key difference is real time: linearizability respects the wall clock (a confirmed write is visible immediately), sequential consistency only respects one agreed order. Few databases advertise sequential consistency directly, but the idea shows up in CPU memory models and in replicated logs that followers apply in order.

7. Causal Consistency

Causal consistency keeps cause before effect. If operation B was made after seeing operation A (a reply after a question, a comment after a post), every user must see A before B. Operations that are not related (concurrent) may appear in any order.

In the order chat, the rider writes "I am at your gate" and Priya replies "Coming down in 2 minutes". The restaurant's "Your order is packed" message is unrelated. If the Mumbai replica receives the reply first, an eventually consistent store would show Priya answering a question nobody asked.

Output

== Eventual only: show messages as they arrive ==
  show m2 | Priya: Coming down in 2 minutes
  show m3 | Restaurant: Your order is packed
  show m1 | Rider: I am at your gate
== Causal: hold a message until what it depends on is shown ==
  hold m2 (waiting for m1)
  show m3 | Restaurant: Your order is packed
  show m1 | Rider: I am at your gate
  show m2 | Priya: Coming down in 2 minutes

Dry run. Without ordering, the screen shows m2 (the reply) first. With causal delivery, m2 arrives first but depends on m1, so it is held. m3 has no dependency and is shown. When m1 arrives it is shown, and that releases m2. Final screen: m3, m1, m2. Real systems track dependencies with version vectors or vector clocks instead of one "depends on" field, but the rule is the same.

Causal consistency is attractive because it does not need a global leader: replicas can accept writes during a partition and still keep the order users care about. MongoDB offers it as "causally consistent sessions".

8. Session Guarantees: Read Your Writes and Monotonic Reads

Many complaints about eventual consistency come from one user seeing strange behaviour in their own session. Session guarantees fix exactly that, without making the whole system strong. There are four of them:

GuaranteeThe bug it preventsDelivery app example
Read your writesYou save a change, refresh, and it is gonePriya updates her address and sees the old one
Monotonic readsData goes back in time between two readsOrder status jumps from "Out for delivery" back to "Preparing"
Monotonic writesYour own writes apply out of orderCart: "remove item" applied before "add item"
Writes follow readsYour reply is stored before what you replied toA review reply saved before the review

A simple way to implement them is a session token: the client remembers the highest version it has seen or written, sends it with each read, and the router only uses a replica that has caught up to that version. Otherwise it reads from the primary (or waits briefly).

Output

== Without a session token ==
  read replica-1: v3 Out for delivery
  read replica-2: v2 Preparing
== With a session token ==
  read replica-1: v3 Out for delivery (token v3)
  replica-2 is at v2 but token needs v3 -> use primary
  read primary: v3 Out for delivery (token v3)
  write on primary: add Rs 30 tip -> v4 (token v4)
  replica-1 is at v3 but token needs v4 -> use primary
  read primary: v4 Out for delivery + Rs 30 tip (token v4)

Dry run. Without a token, the first read hits replica 1 (v3, "Out for delivery") and the second hits replica 2 (v2, "Preparing"): the order moved backwards. With a token, the first read sets the token to v3, so the second read skips replica 2 and uses the primary. After Priya adds a tip on the primary (v4), the token becomes v4; replica 1 is only at v3, so the read goes to the primary and she sees her tip. That is monotonic reads and read-your-writes, paid for only by the user who needs them.

Other common ways: read from the primary for a few seconds after a user writes, or pin a user to one replica ("sticky sessions").

9. Consistent Prefix Reads

Consistent prefix means that if writes happened in some order, a reader sees them in that order: possibly only the first part, but never with a gap in the middle. It matters for anything that is a sequence of events, such as order tracking, bank statements or a match commentary.

The usual cause of a broken prefix is storing related events on different shards that replicate at different speeds. The usual fix is to keep events of one order (or one conversation) on the same partition, or to read them through one ordered log.

10. Eventual Consistency

Eventual consistency only promises that if no new writes arrive, all copies will eventually hold the same value. Any replica can accept a write and answer reads immediately, which gives the lowest latency and the best availability. Copies exchange updates in the background, often by gossip.

The hard part is what happens when two replicas changed the same item. If the merge rule is last-writer-wins (keep the value with the newest timestamp), updates that happened elsewhere are silently thrown away. A data type designed to merge, such as a G-Counter (one slot per replica, merge by taking the larger number in each slot), converges without losing anything. These merge-friendly types are called CRDTs.

Output

== Last-writer-wins on one number ==
  Hyderabad count = 103 (written at t=5)
  Mumbai count = 102 (written at t=7)
  Bengaluru count = 101 (written at t=6)
  after sync, every replica keeps 102 from Mumbai -> 4 ratings lost
== G-Counter: one slot per replica, merge = max per slot ==
  gossip Hyderabad <-> Mumbai: Hyderabad=105 Mumbai=105 Bengaluru=101
  gossip Mumbai <-> Bengaluru: Hyderabad=105 Mumbai=106 Bengaluru=106
  gossip Bengaluru <-> Hyderabad: Hyderabad=106 Mumbai=106 Bengaluru=106
  all replicas agree on 106 ratings, none lost

Dry run. 100 ratings already existed, and today Hyderabad, Mumbai and Bengaluru accepted 3, 2 and 1 new ratings. With last-writer-wins, Mumbai wrote last (t=7), so every replica keeps 102 and 4 ratings vanish. With a G-Counter, each gossip round takes the larger value per slot; after three rounds every replica holds [3, 2, 1], which is 106. The data was eventually consistent in both cases, but only one of them was correct.

Eventual consistency is a good fit for view counts, likes, ratings, search indexes, recommendation data and caches, as long as each one has a merge rule that matches its meaning.

11. Tunable Consistency with Quorums

Leaderless databases such as Cassandra and DynamoDB let you choose the consistency per request. With N copies, a write waits for W of them and a read asks R of them and keeps the newest version. If R + W > N, every read group overlaps every write group in at least one copy, so the read always finds the latest confirmed write.

Output

W=1 R=1 (R+W<=N): wrote to 1, read C -> Rs 250 (v1) STALE
W=2 R=1 (R+W<=N): wrote to 2, read C -> Rs 250 (v1) STALE
W=2 R=2 (R+W>N): wrote to 2, read BC -> Rs 280 (v2) fresh
W=3 R=1 (R+W>N): wrote to 3, read C -> Rs 280 (v2) fresh
W=1 R=3 (R+W>N): wrote to 1, read ABC -> Rs 280 (v2) fresh

Dry run. The price of biryani changes from Rs 250 (v1) to Rs 280 (v2). With W=1, R=1 the write lands on A and the read asks C, so it returns Rs 250: stale. W=2, R=1 still misses (A, B written; C read). W=2, R=2 reads B and C; B has v2, so the newest version wins and the read is fresh. W=3, R=1 and W=1, R=3 also satisfy R + W > N and are fresh. In Cassandra, QUORUM writes plus QUORUM reads give this overlap; ONE plus ONE gives speed and eventual consistency.

A quorum overlap on its own is not full linearizability (concurrent writes, failed writes and clock skew can still produce surprises), but it is the everyday tool for "strong enough" reads in leaderless stores. The CAP theorem article shows what quorums do during a partition.

12. Linearizability vs Serializability

These two words sound alike and are often mixed up in interviews. They answer different questions.

LinearizabilitySerializability
AboutSingle reads and writes on one itemTransactions that touch many items
PromiseActs like one copy, in real-time orderResult is as if transactions ran one at a time, in some order
Real timeYes, a confirmed write is visible at onceNo, the chosen order may differ from wall-clock order
Comes fromDistributed systems (replication)Databases (isolation levels)
Both togetherStrict serializability, e.g. Google SpannerStrict serializability, e.g. Google Spanner

13. Choosing a Model for Each Feature

The rule of thumb: choose the weakest model that still protects the feature. Ask what a stale or reordered value would cost, then walk down the ladder until the cost is acceptable.

  1. List the data each feature reads and writes.

  2. Ask the cost of stale data: lost money or stock, a confused user, or nothing.

  3. Pick the model from the ladder and say it out loud with an example ("order status needs monotonic reads so it never goes backwards").

  4. Name the mechanism: leader plus majority, session token, causal metadata, or merge rule.

  5. Measure lag in production (replication lag metrics and alerts), because weaker models are only as good as the lag you actually have.

14. Consistency in Real Databases

Database or serviceWhat it offersHow you choose
PostgreSQL / MySQL with read replicasStrong on the primary, eventual on async replicasSend critical reads to the primary
Google SpannerStrict serializability across regionsDefault; uses TrueTime clocks
etcd, ZooKeeper, ConsulLinearizable writes and readsDefault; used for config and locks
MongoDBTunable; causally consistent sessionswriteConcern "majority", readConcern "majority" or "linearizable"
CassandraTunable per queryConsistency level ONE, QUORUM, ALL
Amazon DynamoDBEventual by default, strong on requestConsistentRead = true on reads
Redis replicationAsynchronous, eventualWAIT command for stronger writes

Common Mistakes

  • Saying "eventual consistency" without a merge rule. Last-writer-wins quietly loses updates; decide how each data type merges.

  • Reading your own write from a replica. A user saves a change and sees the old value; use read-your-writes for user-owned data.

  • Making everything strong "to be safe". It adds latency to every request and reduces availability for data that never needed it.

  • Confusing linearizability with serializability, or distributed consistency with the C in ACID.

  • Forgetting caches and search indexes. They are copies too, and usually the most stale ones.

  • Not measuring replication lag. A model that is fine at 10 ms lag can break users at 10 seconds.

Interview Questions

  • What is a consistency model? The contract that says which values a read may return after writes in a replicated system, from "always the latest" (linearizable) to "eventually the same" (eventual).

  • Strong vs eventual consistency? Strong: every read after a confirmed write sees it, at the cost of latency and availability. Eventual: reads may be stale for a while, but copies converge once writes stop.

  • How do you give read-your-writes on top of async replicas? Track the user's last written version (session token) and only read from replicas that have reached it, or read from the primary for a short time after a write.

  • What does causal consistency guarantee? Operations that depend on each other are seen in the same order by everyone; unrelated operations may be seen in any order.

  • Why does R + W > N give fresh reads? The read group and the write group must share at least one copy, and that copy holds the newest version.

  • Which model would you choose for a wallet, a chat, and a like counter? Wallet: linearizable. Chat: causal. Like counter: eventual with a CRDT counter.

Summary

ModelUse it forHow it is built
LinearizablePayments, stock, locks, permissionsLeader + majority writes and reads (Raft, Paxos)
CausalChats, comments, repliesVersion vectors, dependency tracking
Session guaranteesA user's own address, cart, statusSession tokens, read-after-write routing
EventualCounts, ratings, feeds, searchAsync replication, gossip, CRDT merges
  • Replication makes copies disagree for a while; a consistency model defines what reads may see in that window.

  • Stronger models need more coordination, so they are slower and less available.

  • Session guarantees fix most user-visible problems without making everything strong.

  • Eventual consistency needs the right merge rule, or updates are lost.

  • Pick a model per feature, the weakest one that still protects it.

Previous topic: CAP Theorem. Next: Consistent Hashing.