Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 36 ยท Replication

Consistency Models

Understand consistency models: linearizability, sequential, causal and eventual consistency, read-your-writes and monotonic reads, with stale-read demos.

When data lives on several replicas, a simple question gets complicated: if I write a value and then read it, what am I allowed to see? A consistency model is a promise about exactly that. It describes which results of reads and writes the system may show you, and which it must never show.

Stronger models are easier to program against but cost latency and availability. Weaker models are fast and always on, but push surprises into your application. In this lesson you will meet the models from strongest to weakest, reproduce the classic anomalies on lagging replicas, and run a tiny linearizability checker.

The consistency spectrum

From strict to relaxed

Linearizable Acts like ONE copy, in real time.
Sequential One global order, not tied to the clock.
Causal Causes are seen before effects.
Eventual Copies agree if writes stop.

Each step down gains speed and availability and gives up guarantees.

1 / 2

Linearizability: the strongest model

A system is linearizable (also called strong or atomic consistency) if it behaves as though there is one copy of the data and every operation takes effect at a single instant between its start and its end. Two consequences follow:

  1. Once a write has completed, every later read, from any client anywhere, must see it (or something newer).
  2. Once any read has returned the new value, no later read may return the old one.

Linearizability gives you the intuition of a single variable in a single-threaded program, which is why locks, leader election and unique-username checks need it. It is also expensive: replicas must coordinate on every operation, so it costs latency, and it can't stay available during a network partition (see CAP, PACELC and Real Trade-offs).

Sequential consistency

Sequential consistency keeps one global order that every client agrees on, and keeps each client's own operations in the order it issued them, but drops the link to real time. A read may return an older value even though a write finished long ago, as long as all clients see one consistent story. It is weaker than linearizability and a little cheaper.

Causal consistency

Many events are unrelated; only some depend on others. Causal consistency promises that if event A could have influenced event B (see Lamport Clocks and Happens-Before), then everyone sees A before B. Concurrent events may be seen in any order by different readers. It is the strongest model that can still stay available during partitions, which makes it popular in geo-replicated systems. The mechanism behind it is often vector clocks.

Eventual consistency

Eventual consistency only promises convergence: if writes stop, all replicas eventually hold the same value. It says nothing about what you may read in between. It's the weakest useful model, and it's what asynchronous replication gives by default.

Anomalies on lagging replicas

Let's reproduce the surprises. Our cluster has a leader and three followers; each tick the leader may accept a write, and each follower independently applies at most one pending write. A client writes sometimes and reads from a random replica. We count two kinds of anomaly:

  • Read-your-writes violation: the client reads a version older than its own last write.
  • Monotonic-read violation: the client reads a version older than one it already read.
Python Random reads on lagging replicas

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.

Both anomalies are common here, and they match real complaints: "I saved my profile and the page still shows the old name" (read-your-writes), and "I refreshed and the comment disappeared, then came back" (monotonic reads: the second refresh hit a fresher replica than the first).

Session guarantees: cheap fixes

You don't have to jump to linearizability to fix these. The trick is a session token: the client remembers the highest version it has written or read. A replica that is behind the token is not allowed to answer, so the read goes to another replica (the leader always qualifies).

Python Session tokens remove the anomalies

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.

Both counts drop to zero, and the price is a share of reads that had to go to the leader. Real systems implement the token as a log position, a timestamp or a "read from the replica that served my last write" sticky session.

Session guarantees apply per client. Two different users can still see different versions at the same moment. That is fine for a profile page and not fine for a seat booking, which needs a stronger model.

Checking linearizability

How would you test that a system is linearizable? You record a history: each operation with its start time, end time, and result. Then you search for a single order of the operations that (1) respects real time (if A ended before B started, A comes first) and (2) obeys the sequential rules of a register (a read returns the latest write). If one exists, the history is linearizable.

Here is a brute-force checker for a single register that starts at 0. It tries every ordering, so it only works for small histories, but it shows the idea behind tools like Jepsen's checkers (see Testing Distributed Systems).

Python A tiny linearizability checker

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.

The second case is a stale read after a completed write, exactly what lagging followers produce. The fourth one is a monotonic-read violation, which linearizability forbids too. The third is fine: a write that is still in progress is allowed to "take effect" at any instant in between, so reading the old value early and the new value later is legal.

Choosing a model

Need Reasonable model
Locks, unique names, leader election, bank balance checks Linearizable
Comments and replies, chat Causal (or consistent prefix)
Profile edits seen by the editor Read-your-writes
Like counts, view counters, caches Eventual

The rule of thumb: use the weakest model your feature can tolerate, and pay for strong consistency only where being wrong costs real money or trust. The next lesson, Quorums and Tunable Consistency, shows how systems let you dial this choice per operation.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which guarantee says that once a write completes, all later reads anywhere must see it?

    Linearizability makes the system act like a single copy in real time: after a write completes, no later read may return an older value.
  2. 2. You post a comment, refresh, and your comment is missing. Which guarantee was violated?

    The read hit a replica that had not yet applied your own write. Read-your-writes promises you always see your own updates.
  3. 3. A user sees a reply appear before the question it answers. What would prevent this?

    The reply causally depends on the question. Causal consistency (and consistent prefix reads) guarantee a cause is never seen after its effect.
  4. 4. What does eventual consistency promise?

    It promises only convergence over time. It makes no promise about what reads return in the meantime.
  5. 5. In the linearizability checker, why is 'read 0, then read 1' during one slow write acceptable?

    A write that overlaps with the reads has no fixed order against them. The checker can place it between the two reads, so the history is valid.

Summary

  • A consistency model defines which read results are allowed after writes on replicated data.
  • Linearizable: behaves like one copy in real time. Sequential: one global order without real time. Causal: causes before effects. Eventual: convergence only.
  • Lagging replicas cause read-your-writes and monotonic-read anomalies.
  • Session tokens (a remembered version) remove those anomalies cheaply by refusing answers from replicas that are too stale.
  • Linearizability can be tested by searching for an ordering that respects real time and register semantics.
  • Use the weakest model your feature can tolerate; pay for strong consistency where mistakes are expensive.

Next: Quorums and Tunable Consistency