Consistency Models

Difficulty: Intermediate

Question

What is eventual consistency, how does it differ from strong consistency, and when is it acceptable to use?

Answer

Eventual consistency means that after a write, replicas may briefly return old data, but if no new writes happen, all replicas will converge to the same value. Strong consistency means every read sees the most recent successful write, as if there were only one copy of the data. Eventual consistency gives lower latency and higher availability; strong consistency gives simpler correctness at the cost of coordination.

Where the inconsistency comes from

Distributed databases keep multiple copies of data on different machines, often in different zones or regions. When a write arrives:

  • With asynchronous replication, the primary or coordinator acknowledges the write before every replica has it. A read routed to a lagging replica returns the old value.
  • With synchronous or quorum replication, the write is acknowledged only after enough replicas confirm, so later reads can be guaranteed to see it.

The gap between write and convergence is usually milliseconds, but during network problems or heavy load it can stretch to seconds or more.

Consistency in real systems

  • PostgreSQL / MySQL read replicas: the primary is strongly consistent, but async replicas are eventually consistent. Reading your own profile update from a replica right after saving it can show stale data.
  • Amazon DynamoDB: reads are eventually consistent by default; setting ConsistentRead: true gives a strongly consistent read at double the read cost.
  • Cassandra: consistency is tunable per query. With replication factor N, writing at QUORUM and reading at QUORUM gives R + W > N, so reads overlap the latest write.
  • Amazon S3: has provided strong read-after-write consistency for all operations since December 2020.
  • Spanner and CockroachDB: provide strong consistency across regions using consensus, with higher write latency.

The CAP and PACELC view

CAP says that during a network partition a system must choose between consistency and availability. PACELC adds the everyday case: even with no partition, you trade latency against consistency. Eventual consistency is the "favour availability and latency" choice.

When eventual consistency is acceptable

AcceptableNeeds strong consistency
Like counts, view countsWallet or bank balance
Social feeds and timelinesSeat or inventory reservation
Product reviews, recommendationsUnique username registration
Analytics dashboardsPayment status for a UPI transaction
Search index updatesPermission and access revocation

The test: what is the cost if a user sees a value that is a few seconds old? If the answer is "a slightly wrong counter", eventual is fine. If it is "we sold the same seat twice", it is not.

Weaker guarantees that fix common complaints

Pure eventual consistency can feel buggy. Systems add session guarantees:

  • Read-your-writes: after you post a comment, you always see it, by routing your reads to the primary for a short window or tracking the replication position.
  • Monotonic reads: you never see data go backwards, by pinning a user to one replica.
  • Causal consistency: a reply is never visible before the message it replies to.
Stale read from an async replica. The replica catches up shortly after the write is acknowledged.
  1. Client → Primary: Write name = 'Priya'
  2. Primary → Client: Write acknowledged
  3. Client → Replica: Read name
  4. Replica → Client: Returns old value 'Pri'
  5. Primary → Replica: Replicate change
  6. Client → Replica: Read again, gets 'Priya'

Code examples

Choosing consistency per read in DynamoDB

import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, GetCommand } from "@aws-sdk/lib-dynamodb";

const db = DynamoDBDocumentClient.from(new DynamoDBClient({ region: "ap-south-1" }));

// Default: eventually consistent, cheaper, may be slightly stale
const feed = await db.send(new GetCommand({
  TableName: "posts", Key: { postId: "p_123" },
}));

// Strongly consistent: reflects all writes acknowledged before the read
const wallet = await db.send(new GetCommand({
  TableName: "wallets", Key: { userId: "u_42" },
  ConsistentRead: true,
}));

The same database offers both models. Strong reads cost twice the read capacity and are not supported on global secondary indexes.

Key points

  • Eventual consistency: replicas converge if writes stop; reads may be briefly stale
  • Strong consistency: every read sees the latest acknowledged write
  • Async read replicas in PostgreSQL and MySQL are eventually consistent
  • Quorum rule R + W > N gives overlap between reads and the latest write
  • Session guarantees like read-your-writes fix most user-visible staleness

Common follow-up questions

  • Explain the CAP theorem and where PACELC extends it.
  • How would you implement read-your-writes on top of async read replicas?
  • What happens in Cassandra when two replicas have conflicting values?
  • How do CRDTs help in eventually consistent systems?

Interview tips

  • Define both models in one sentence each before going deeper
  • Use an 'acceptable vs not acceptable' example pair, like likes vs wallet balance
  • Mention read-your-writes; it shows you understand the user-facing impact

Concepts covered

Eventual Consistency, Strong Consistency, Replication Lag, Quorum, Read-Your-Writes

More Database Design questions