Designbench

Lesson 2.3 · 15 min

Consistency models

Route reads across a lagging leader and followers, watch users miss their own edits or go back in time, then fix it one guarantee at a time.

The idea in plain words

When the same data lives on several machines, a copy may briefly lag behind. A consistency model is the rule for how out of date a read is allowed to be: always fresh, fresh for your own changes, or “it will catch up soon”.

The problem it solves

When data lives on several machines, a write reaches them at slightly different times. A user reading from a copy that hasn’t caught up yet sees old data, and sometimes that looks like a bug.

How it works

  1. A write goes to one copy first (often a primary) and then spreads to the others.
  2. For a short time, different copies return different values.
  3. A consistency model is the promise about what a read may return during that time.
  4. Strong models always return the latest write, but every read must check with others, which is slower.
  5. Weaker models return whatever is nearby, which is fast but can be stale. Many apps mix models per feature.

Example

You change your profile name and refresh. If the refresh hits a copy that is 2 seconds behind, your old name appears and you think the save failed. “Read your writes” fixes exactly this by sending your reads to a copy that has your change, while other users may still see the old name for a moment.

Key terms

Strong (linearizable)
Every read returns the most recent write, as if there were one copy.
Eventual consistency
Copies may disagree for a while but end up the same once writes stop.
Read-your-writes
You always see your own changes, even if others see them later.
Replication lag
How far behind the primary a copy is, in time or in writes.
Anomaly
Something odd a user sees because of lag, such as a comment that disappears on refresh.

In this lesson you'll learn

Sections

  1. Theory: the ladder
  2. Theory: replica lag
  3. Theory: cause before effect
  4. Design: pick a model
  5. Check yourself

Cheat sheet

Linearizablelatest write, real time, everyone
Causalcause shown before effect
Read-your-writesyou see your own edits
Monotonic readsnever back in time
Eventualconverges once writes stop

The interactive simulations and the quiz load with JavaScript.