Consistency models

In one sentence

The contract for what a read may return on replicated data, from linearizable (behaves like one up-to-date copy) to eventual (converges someday). Stronger contracts cost latency and availability.

Analogy: A group chat: linearizable means everyone sees each message the instant it's sent; causal means a reply never shows up before its question; eventual means everyone gets the whole thread, sometime.

With N=3, W=2, R=2, any two replicas you read include at least one that took the write, so the newest version is visible.

How it works

  • Linearizable writes must reach a majority: ~70–150 ms across regions instead of ~1 ms.
  • Causal keeps dependent operations (question → reply) in order for everyone; roughly the strongest model available during a partition.
  • Session guarantees are per client: read-your-writes shows your own updates; monotonic reads never go back in time.
  • With quorums, R + W > N (e.g. N=3, W=2, R=2) makes every read overlap the latest write.
  • Choose per data: balances need linearizable, like counts can be eventual, chat history needs read-your-writes.

Check yourself

Answer to mark it learned; your answers aren't scored.

A reader sees a new comment on a shared document, refreshes, and it's gone; another refresh brings it back. Reads are load-balanced across three async replicas with different lag. Which guarantee is broken, and what's the fix?

Go deeper

Quorums: N, R, W Interactive lab
Full explanation

A consistency model is the promise a replicated store makes about what reads can return. From strongest to weakest:

  • Linearizable (strong): behaves like one copy. Once a write is acknowledged, every later read, from anyone, sees it.
  • Sequential: everyone sees all operations in the same order, respecting each client's own order, but not necessarily real time.
  • Causal: operations that depend on each other (question → reply) appear in that order to everyone; unrelated ones may differ. Roughly the strongest model that stays available during a partition.
  • Session guarantees: per-client promises, mainly read-your-writes (you see your own updates) and monotonic reads (you never go back in time).
  • Eventual: if writes stop, replicas converge. No bound on staleness.

With quorums (N replicas, W write acks, R read replies), R + W > N makes every read set overlap the latest write set, e.g. N=3, W=2, R=2.

Strength costs latency: a linearizable write must reach a majority, which across regions means ~70–150 ms instead of ~1 ms. So choose per data: balances and uniqueness constraints need linearizable; like counts are fine eventual; chat history needs read-your-writes, or the LLM answers without the message the user just sent.

Code Read-your-writes with Postgres replicas (LSN token)
pythonRead-your-writes with Postgres replicas (LSN token)
import psycopg

def send_message(primary: psycopg.Connection, conv_id: str, text: str) -> str:
    primary.execute("INSERT INTO messages (conv_id, body) VALUES (%s, %s)", (conv_id, text))
    primary.commit()
    # WAL position after my commit: a "causality token" kept in the user's session
    return primary.execute("SELECT pg_current_wal_lsn()::text").fetchone()[0]

def caught_up(replica: psycopg.Connection, token: str) -> bool:
    row = replica.execute("SELECT pg_last_wal_replay_lsn() >= %s::pg_lsn", (token,)).fetchone()
    return bool(row[0])

def load_history(primary, replicas, conv_id: str, token: str | None) -> list[str]:
    # Any replica that replayed past my last write is good enough; otherwise use
    # the primary. Never build the LLM prompt from a history missing my last turn.
    conn = next((r for r in replicas if token is None or caught_up(r, token)), primary)
    rows = conn.execute(
        "SELECT body FROM messages WHERE conv_id = %s ORDER BY id", (conv_id,)
    ).fetchall()
    return [body for (body,) in rows]
Real example

A chat assistant writes each user message to the Postgres primary and builds the next prompt from a read replica. During a nightly batch load, replica lag reaches 2 s. A user sends "Actually, answer in Spanish" and the model replies in English: the context came from a replica that hadn't seen the message yet. The model is fine; it's a read-your-writes violation. Fix: keep the WAL position of the user's last write and read only from replicas past it, else from the primary. The like counter on the same page stays eventual.

Trade-offs +3 · −3

+ You gain

  • Choosing per data type means you pay coordination cost only where correctness needs it.
  • Session guarantees fix most user-visible anomalies at a fraction of linearizability's cost.
  • Eventual consistency keeps reads local, fast and available through partitions.

− You pay

  • Linearizability costs a majority round trip per write: ~70–150 ms across regions.
  • Weaker models push anomaly handling into application code that few people test.
  • Vendor terms ("strong", "session", "bounded staleness") don't map cleanly to textbook models; read the fine print.

Use it when

Whenever data is replicated or cached: decide per entity which anomalies users can tolerate, then pick the weakest model that rules out the rest.

Avoid it when

Don't buy global linearizability by default for counters, feeds or analytics: you pay latency and availability for a guarantee nobody notices.

Numbers to know 4
  • Quorum rule: R + W > N; the usual setting is N=3, W=2, R=2.
  • DynamoDB reads are eventually consistent by default; a strongly consistent read costs 2× the read capacity.
  • Amazon S3 has been strongly read-after-write consistent since December 2020.
  • Spanner's TrueTime uncertainty (ε) ran ~1–7 ms in the 2012 paper; commit wait pauses for it before acknowledging a write.
Common pitfalls 4
  • Assuming R + W > N means linearizable: sloppy quorums, last-write-wins on concurrent writes and partial failures still return stale or lost data.
  • Letting the ORM route reads to replicas and silently losing read-your-writes.
  • Defaulting to "strong" everywhere and paying a cross-region round trip on every write, even for counters.
  • Load balancing reads across replicas with different lag: a comment appears, then vanishes on refresh. Use sticky routing per session.
Interview questions 2

Answer out loud first, then reveal.

What consistency would you choose for an assistant's conversation history?Show answer

Read-your-writes plus causal ordering, not global linearizability. The user must always see, and the model must always be prompted with, their own latest message, and a reply must never appear before its question. Other users rarely read that conversation, so cross-user real-time ordering buys nothing. I'd implement it with a session token, the WAL position of the last write, reading from any replica past it and falling back to the primary. Most reads stay on replicas, without the "the model ignored me" bug.

Does R + W > N give you linearizability?Show answer

No, only overlap. With concurrent writes and last-write-wins timestamps, clock skew can drop the newer value. Sloppy quorums with hinted handoff write to nodes outside the N. A write that reached only some replicas can be seen by one reader and missed by the next. Dynamo-style stores need synchronous read repair to get there, and in practice I'd use a consensus-backed store when I truly need it. Quorums give a strong likelihood of recency, not a guarantee.