SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
  • Challenges of Distribution
  • Failure Types: Crash vs Byzantine
  • Network Partitions
  • Split Brain Problem
  • Heartbeats
  • Failure Detectors
  • Handling Failures
  • Consistency Models
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Distributed Systems/Distributed System Fundamentals

Consistency Models

The rules for what a reader may see after a write: strong, eventual and the ones in between.

⏱ 16 min readAdvancedMust KnowInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

A consistency model is a promise about what a reader may see after someone has written data, when that data is stored on several copies.

  • Strong consistency: every read sees the latest write, as if there were only one copy. Simple to think about, but slower and less available.
  • Eventual consistency: copies may differ for a short time, but they all agree if writes stop. Fast and available, but readers can see old data.
  • Between them are useful middle levels, such as read-your-writes, monotonic reads and causal consistency.

02Concept: What is it?

To survive failures and serve users near them, we keep copies (replicas) of data on many machines. But a write cannot reach every copy at the same instant, because the network takes time and some machines are slow or down. So for a short time the copies disagree. A consistency model says which disagreements the user is allowed to notice.

It is a contract between the database and the programmer. A strong model makes the programmer's life easy but costs speed and availability. A weak model gives speed and availability but pushes more work to the programmer, who must handle old or out-of-order data.

The consistency spectrum
slowestStrong (linearizable)Acts like one copy. Reads always see the latest write.
SequentialEveryone sees writes in the same order, maybe not the latest.
CausalIf one event caused another, everyone sees them in that order.
Read-your-writes / monotonic readsYou never see your own change vanish, or time go backwards.
fastestEventualAll copies agree later, if writes stop.
Moving down gives more speed and availability, and fewer guarantees.
🧠Do not mix it up with ACID
The C in ACID means a transaction keeps the data rules valid (for example, no negative balance). Consistency models are about how replicas agree and what readers see. They are different ideas with the same word.

03Real-Life Example

🍡The Bank Branches and the Social Feed
You deposit money at one branch of a bank. If you then check your balance at another branch, you expect to see the deposit at once. That is strong consistency, and the bank is willing to make you wait a moment for it. Now think of a photo you post. Your friend in another country sees it 3 seconds later and nobody is hurt. That is eventual consistency. But if you post a photo and your own phone does not show it for a minute, you feel something is broken. Avoiding that surprise is read-your-writes.
Bank and social app
Consistency
Every branch shows the same balance at once
Strong consistency
A photo that appears for friends a few seconds later
Eventual consistency
You always see your own new post
Read-your-writes
A reply never appears before the comment it answers
Causal consistency
Refreshing never shows older data than before
Monotonic reads
Waiting while all branches update
The latency cost of strong consistency
Two branches updating at once and fighting
A write conflict
Why replicas disagree for a moment
✍️Write x = 5
→↓
πŸ—„οΈReplica 1x = 5 now
⇒⇣copying...
πŸ—„οΈReplica 2x = 3 still
Has the new value Not updated yet

04Why Do We Need It?

🧠Know what to expect
Programmers need to know whether a read can be old, or their code has hidden bugs.
βš–οΈChoose speed or safety
The CAP theorem and latency limits force a trade. Models name the choices.
🌍Serve users everywhere
Copies near users give low latency, but make agreement harder.
πŸ›‘οΈKeep working in failures
Weaker models can answer even when part of the network is cut off.
πŸ’°Protect what matters
Money and stock need strong guarantees. Likes and views do not.
πŸ§ͺTest the right thing
Tests and bug reports make sense only when you know the promised behaviour.
The question is not "strong or weak?" for the whole system, but "how fresh does this particular read need to be, and what will I pay for it?"

05Where Do We Use It?

🏦Strong
Account balances, inventory counts, leader election, locks, unique usernames (Spanner, etcd, ZooKeeper, a single-primary SQL database).
πŸ“°Eventual
Social feeds, like counts, product catalogs, DNS, caches (DynamoDB default reads, Cassandra with low consistency, CDNs).
πŸ‘€Read-your-writes
Profile edits and posts: the author must see their own change at once.
πŸ’¬Causal
Comments and replies, chat messages, collaborative editing where order of cause and effect matters.
πŸ”’Quorum reads and writes
Dynamo-style stores let you tune the level per request.
🧩Mixed in one app
Most real systems use strong for a few key paths and eventual for the rest.

06Types / Parts / How It Works

The main models, from strongest to weakest

ModelThe promiseCost or catch
Strong (linearizable)Each operation seems to happen at one instant, and every read returns the latest completed writeNeeds coordination across copies: higher latency, and unavailable on some partitions
SequentialAll clients see operations in the same single order, which respects each client's own orderOrder is shared, but a read may not show the very latest write in real time
CausalOperations that depend on each other are seen in that order by everyone. Unrelated ones may differNeeds tracking of dependencies (for example vector clocks)
Read-your-writesAfter you write, your own reads show itOther users may still see old data
Monotonic readsOnce you have seen a value, you never see an older oneReads must stick to a fresh enough replica
EventualIf writes stop, all copies finally agreeNo promise about when, and reads can be old or out of order meanwhile

How a write and a read can overlap

Quorum idea: W + R > N
N = 3 copies, W = 2, R = 2
πŸ—„οΈAin write set
πŸ—„οΈBwrite + read set
πŸ—„οΈCin read set

2 + 2 > 3, so the two sets always share a copy (here B) and the read sees the latest value.

W = 1, R = 1
πŸ—„οΈAin write set
πŸ—„οΈB
πŸ—„οΈCin read set

1 + 1 is not more than 3. The read can miss the write. Faster, but only eventually consistent.

With 3 copies, writing to 2 and reading from 2 means every read set meets every write set in at least one copy.

Ways to settle disagreements

MethodHow it worksUsed by
Last write winsThe write with the newest timestamp is keptCassandra (default), many caches. Can lose a write if clocks differ.
Version vectorsKeep both versions when they are concurrent and let the app mergeDynamo-style stores, Riak
CRDTsData types built so that merges always work, in any orderRedis CRDB, Riak, collaborative tools
Read repair and anti-entropyFix stale copies when they are read, or compare copies in the backgroundCassandra, DynamoDB-style systems
VisualizerOne write, many readers: when is a read stale?Pick eventual consistency and move the read time right after the write, then ask replica 3. Compare with strong consistency (no stale reads, but a slower write) and with quorum or read-your-writes.
Consistency model

A write is confirmed by the leader alone and copied to the others later. A read goes to one replica, so it may be old, but all replicas agree in the end.

Value on each replica at tick 3 (the white ring marks the replica or replicas this read asks)
Replica 1 (leader)
x = 5
Replica 2
x = 3
Replica 3
x = 3
The same read at every moment (green = new value, amber = stale after the write was confirmed, grey = before the confirmation)
0
1
2
3
4
5
6
7
8
9
10
11
This read returns
x = 3
stale
Stale reads
4
out of 10 after confirmation
Write confirmed after
instantly
write latency
All replicas agree at
tick 6
convergence
Tick 3: the user was already told the write succeeded, but replica 3 still has x = 3. This is a stale read. It lasts until tick 6, when all copies agree. Pick another replica, or lower the lag.

A simplified model with fixed delays (no random numbers). The user changes x from 3 to 5 at tick 2. Real systems differ in the details, but the trade is the same: fresher reads cost slower or less available writes.

07How to Use It / Steps / Implementation

Choosing a consistency level

  1. 1
    List your reads and writes
    For each, ask: what is the worst thing that happens if this read is a few seconds old? Money lost or a harmless extra like?
  2. 2
    Use strong for rules that must hold
    Unique names, stock counts, balances and locks. Pay the latency for these only.
  3. 3
    Use eventual for the rest
    Feeds, counts, search, recommendations. Faster and available during partitions.
  4. 4
    Fix the surprising cases cheaply
    Read-your-writes: send the user to the primary, or to a replica that has their write, for a short time after they write.
  5. 5
    Keep order for conversations
    Use causal ordering, or put all related messages in one partition, so replies never come before the question.
  6. 6
    Tune the quorum
    In Dynamo-style systems pick R and W: W + R > N for fresher reads, or lower values for speed.
  7. 7
    Plan for conflicts
    If two writes can happen at once on different copies, decide how they merge: last write wins, a merge function, or a CRDT.
  8. 8
    Say it in the API docs
    Tell your users and teammates which reads may be old, and for how long.
πŸ’‘Per-request choice
Many databases (Cosmos DB, DynamoDB, Cassandra) let you choose the level per call. Use the strong or quorum option only on the few calls that need it.
⚠️Eventual is not "eventually fixed by itself"
Copies only converge if there is a clear merge rule. With last-write-wins and unsynchronised clocks, a write can silently be lost. Know what your system does on conflict.

08Key Terms to Know

TermMeaning in easy words
ReplicaOne copy of the data on one machine
LinearizabilityThe strongest model: every operation seems to happen at a single instant, in real-time order
Sequential consistencyEveryone agrees on one order of operations, but it need not match real time
Causal consistencyOperations that depend on each other are seen in the same order by everyone
Eventual consistencyIf writes stop, all replicas become the same after some time
Read-your-writesA client always sees its own earlier writes
Monotonic readsA client never sees data go back to an older state
Stale readA read that returns an older value than the latest write
QuorumThe minimum number of replicas that must answer (usually a majority)
Replication lagHow far a replica is behind the leader
Conflict resolutionThe rule used to choose or merge when two copies changed at once

09Pros, Cons and Trade-offs

βœ… Pros
  • Strong: simple to reason about, no stale reads
  • Eventual: low latency and high availability
  • Middle models fix the most annoying surprises cheaply
  • Tunable levels let one system fit many needs
  • Clear contract makes testing and debugging possible
⚠️ Cons
  • Strong: slower writes and may be unavailable in a partition
  • Eventual: readers can see old or out-of-order data
  • Weaker models need conflict handling in the app
  • Mixed levels make the system harder to explain
  • Easy to assume a stronger guarantee than you have

Trade-offs

ChoiceWe gainWe lose
Strong consistencyAlways the latest value, simple codeHigher latency, lower availability under partitions
Eventual consistencySpeed, scale and availabilityStale reads, conflicts, extra app logic
Read from the leader onlyFresh readsNo read scaling, leader gets busy
Read from any replicaRead scaling and low latencyPossible stale reads
Quorum (W + R > N)Fresh reads without one leaderEvery call waits for several replicas
Last write winsSimple conflict ruleCan silently drop a concurrent write

10Common Problems and Mistakes

Common mistakes

  1. Reading right after writing from a replica. The user saves a form and the page shows the old data. Read from the primary for a moment, or use read-your-writes.
  2. Assuming strong consistency by default. Many stores are eventual unless you ask for more. Check the default.
  3. Check-then-act on stale data. "Stock is 1, so sell it" on an old copy sells the same item twice.
  4. Using last-write-wins with bad clocks. A write with a slightly fast clock overrides a newer one.
  5. Mixing up ACID consistency and replica consistency. Two different ideas, and both matter.
  6. Strong consistency everywhere. It makes the whole system slow and fragile for no gain.
  7. No merge plan. Two concurrent edits happen and nobody decided who wins.

Things to watch

⏳Replication lag
Measure it. A lag that grows means readers see older and older data.
πŸ”€Time going backwards
Two reads from different replicas can show new, then old data. Use sticky sessions or monotonic reads.
🌐Partitions
A strong system may refuse requests when it cannot reach a majority.
🧾Cross-service views
A page built from many services can mix fresh and old data from different places.

11Real-World Examples

🌐Google Spanner
Offers strong (external) consistency across the world, using synchronised atomic clocks and waiting briefly on commits.
πŸ“¦Amazon DynamoDB
Reads are eventually consistent by default, and you can ask for a strongly consistent read for a higher cost.
πŸͺCassandra
Per-query levels (ONE, QUORUM, ALL). Quorum reads and writes give fresher results.
πŸƒMongoDB
Read and write concerns let you decide how many replicas must confirm and which replica you read from.
🧭etcd and ZooKeeper
Strongly consistent stores for configuration and coordination, built on consensus.
πŸ“ΈSocial networks
Feeds and counts are eventual, but your own post shows up at once (read-your-writes).
πŸ“Note
Default levels and names change between products and versions. Check the documentation for exactly what each setting promises.
Mini case: saving a profile
πŸ‘€Author saves
→↓write
πŸ—„οΈPrimarynew profile
⇒⇣async copy
πŸ—„οΈReplicascatch up in a moment
Author reads from primary Others read replicas
Right after saving, the author is sent to the primary, while other users read from replicas.

12Related Topics to Read Next

CAP Theorem→

Why strong consistency costs availability during a partition.

Replication→

The copies whose agreement we are discussing.

Network Partitions→

The situation where the consistency choice becomes real.

Vector Clocks→

A way to track causal order and spot concurrent writes.

CRDTs→

Data types that merge without conflicts.

Consensus Algorithms→

How strongly consistent systems agree on one order.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • βœ”What: the promise about what a reader sees when data has many copies.
  • βœ”Strong: always the latest, slower, may be unavailable in a partition.
  • βœ”Eventual: fast and available, reads may be old, copies agree later.
  • βœ”Middle: read-your-writes, monotonic reads, causal order.
  • βœ”Quorum: W + R > N means reads meet the latest write.
  • βœ”Conflicts: last write wins (can lose data), merge in the app, or CRDTs.
  • βœ”Choose per use: strong for money and locks, eventual for feeds and counts.
  • βœ”One line: β€œDecide how fresh each read must be, and pay only for that.”
← Previous
Handling Failures
Next β†’
Clock Synchronization Problem