Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 65 ยท Databases at Scale

Consistency Models for Designers

Pick the right consistency level in system design: strong, eventual, causal and weak consistency, with quorum math and a runnable stale-read simulation.

Once data lives on more than one node, "what does a read return?" no longer has an obvious answer. A consistency model is the contract between your data store and your application about what reads can see after writes. In a design interview or a real design review, the useful question is not "which model is best" but "which model does this feature need, and what does it cost?"

This lesson is the practical, decision-oriented version. For formal definitions such as linearizability checkers and vector clocks, the Distributed Systems track goes deeper.

The menu

Four levels you will meet

Strong Every read sees the latest write.
Causal Cause always visible before effect.
Eventual Copies converge, if writes stop.
Weak No promise. Best effort.

Strength costs latency and availability.

1 / 2

Strong consistency. After a write completes, every later read, from any client, returns that value or a newer one. The system behaves like one copy. Achieving it requires coordination between replicas (a leader, or consensus), so each operation pays network round trips, and during a network partition some nodes must refuse to answer.

Eventual consistency. Replicas accept writes independently and sync in the background. If writes stop, all replicas converge to the same value. Reads can be stale in the meantime. It gives low latency and high availability, and it is the default in many large key-value stores.

Causal consistency. If operation B was caused by A (a reply to a comment, an edit of something you just read), everybody sees A before B. Unrelated operations may appear in different orders for different people. It is the strongest model that stays available during partitions.

Weak consistency. After a write, there is no guarantee that any reader will ever see it. This is acceptable when old data is useless anyway: a dropped video frame is not retried.

Quorums: tuning the dial

Dynamo-style stores let you choose per request. With N replicas, a write waits for W acknowledgements and a read asks R replicas. If R + W > N, every read set overlaps every write set, so a read always touches at least one replica that has the latest write, and the client takes the newest version.

N W R Result
3 3 1 Fast reads, slow writes; one dead node blocks writes
3 1 3 Fast writes, slow reads
3 2 2 Balanced; overlaps; tolerates one failure
3 1 1 Fastest, but can read stale data
Python Quorum overlap: when are reads fresh?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When R + W > N, every case is fresh. When it is not, some read sets miss the write entirely.

Eventual consistency in action

The following simulation has one writer and two replicas that sync after a random-looking but fixed delay. A reader at each replica shows the lag, and last-write-wins (LWW) merging shows how updates are resolved after sync.

Python Two replicas converge after sync

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Convergence is not correctness. LWW picks a winner by timestamp and silently discards the other update, which is wrong for a shopping cart (both added items should survive). Merge-friendly data types (CRDTs) or application-level merge rules fix that.

Choosing per feature

A system rarely needs one level everywhere. Decide feature by feature by asking what a stale or reordered read costs:

  • Money, stock levels, seat reservations, unique usernames: being wrong double-sells or double-charges. Use strong consistency, even though it is slower.
  • Social feed, like counts, recommendations: a few seconds of lag is invisible. Use eventual consistency.
  • Comments and replies: show a reply only after its parent: causal.
  • The user's own actions: give them read-your-writes, even if everyone else gets eventual.

Many products mix models inside one database. For example, a relational primary serves checkout (strong), while read replicas and caches serve product pages (eventual).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. With N=5 replicas, which setting guarantees that a read overlaps the latest write?

    R + W must be greater than N. 3 + 3 = 6 > 5, so every read set shares at least one replica with every write set.
  2. 2. Which feature most needs strong consistency?

    Two people must not both get the last seat. Being wrong has a real cost, so the extra latency of strong consistency is worth it.
  3. 3. What does last-write-wins do to concurrent updates?

    LWW guarantees convergence but not that every update survives; the older concurrent update is lost.
  4. 4. Causal consistency guarantees that:

    Causal consistency preserves cause-before-effect ordering without forcing one global order for unrelated operations.

Summary

  • A consistency model defines what reads may return after writes; stronger models cost latency and availability.
  • Strong: latest value always. Causal: causes before effects. Eventual: converges later. Weak: no promise.
  • Quorums with R + W > N give fresh reads; lower settings buy speed with staleness.
  • Last-write-wins converges but can drop updates; use merge rules where every update matters.
  • Choose per feature by the cost of a stale read, and give users read-your-writes for their own data.

Next: CAP Theorem in Practice