Consistency Models

Linearizability, sequential, causal, read-your-writes and eventual consistency explained with concrete examples, and how to choose the right model per use case.

What it is

A consistency model is a contract between a data system and its clients about which values a read may return after writes, especially when data is replicated. Stronger models are easier to program against but cost latency and availability.

Model Guarantee Example of what is prevented
Linearizable (strong) Every operation appears to take effect atomically at a single point between its start and end; reads see the latest completed write Reading an old balance after a transfer completed
Sequential All clients see operations in the same order, consistent with each client's program order Two observers disagreeing on order of events
Causal Causally related writes are seen in order by everyone; concurrent writes may be seen in different orders Seeing a reply before the comment it replies to
Read-your-writes A client always sees its own writes Posting a comment and not seeing it on refresh
Monotonic reads A client never sees older data after newer data Balance going up then down then up on refresh
Eventual If writes stop, replicas converge eventually Nothing during updates; only convergence later

[!FACT] Isolation is different from consistency Database isolation levels (read committed, repeatable read, snapshot, serializable) are about concurrent transactions on one logical database. Consistency models are about replicas. "C" in ACID means application invariants hold, which is a third, different meaning.

Real-world example

A social app uses:

  • Linearizable for username registration (two users must not claim the same handle) via a unique constraint in a strongly consistent store.
  • Read-your-writes for a user's own posts by routing that user's reads to the primary (or the region they wrote to) for a short time after writing.
  • Eventual for like counts and follower counts shown to others.
sequenceDiagram
  participant U as User
  participant P as Primary (region A)
  participant R as Replica (region B)
  U->>P: write post v2
  P-->>U: ack
  P--)R: async replicate (lag 200 ms)
  U->>R: read own posts (sticky routing off)
  R-->>U: v1 (stale: violates read-your-writes)
  Note over U,R: Fix: route the author's reads to the primary for N seconds,<br/>or pass a version token and wait until the replica catches up

Advantages and disadvantages

Stronger consistency Weaker consistency
Simpler application logic Lower latency, higher availability
Correct invariants (no double spend) Must handle stale reads and conflicts
Higher latency for geo-replication Scales writes across regions more easily

Technology options

  • Strong: Spanner, Aurora DSQL, CockroachDB, etcd, ZooKeeper, single-primary RDBMS reads from primary.
  • Tunable: Cassandra (consistency levels per query), Cosmos DB (strong, bounded staleness, session, consistent prefix, eventual), DynamoDB (eventual or strongly consistent reads within a region).
  • Conflict-free replicated data types (CRDTs) for multi-writer eventual consistency: Riak, Redis Enterprise Active-Active, Automerge/Yjs for collaborative apps.

Architect-level considerations

[!ARCHITECT] Choose consistency per operation, not per system. Money movement, inventory reservation and uniqueness need strong guarantees; feeds, counts, recommendations and search can be eventual. Write down the user-visible effect of staleness for each eventual path.

[!DEVELOPER] Know your database's default isolation level (PostgreSQL defaults to read committed; MySQL InnoDB to repeatable read) and the anomalies it allows, such as lost updates and write skew.

Interview questions and answers

Is eventual consistency acceptable for a shopping cart?

Usually yes for adding items (Amazon's Dynamo paper famously chose availability for the cart and merged conflicting versions). It is not acceptable at checkout for price and stock confirmation, which should be validated against the source of truth.

What is session consistency in Cosmos DB?

Within a client session, reads honor read-your-writes, monotonic reads and writes; other sessions may see eventual results. It is the default level because it gives users a coherent experience at low cost.

How do you give read-your-writes with async replicas?

Sticky routing to the primary for a short window after a write, version/LSN tokens that the replica must reach before serving the read, or reading from the primary for the user's own data.