Consistency Models

Strong, eventual, causal, read-your-writes: when and how data syncs between replicas.

Intermediate6 min read
Language
Theme
Primarywrite herereplicateReplica AReplica BStrong: read after writealways sees the latest valueEventual: replicas convergeafter a short lag (may read stale)
Key idea at a glance

Consistency is a spectrum, not a switch

"Is the data correct?" has more than two answers. After a write succeeds, when do other readers see it? Immediately? After 5 ms? Eventually? Different models make different promises, and stronger promises cost more latency and availability. Choosing the model per feature is a core design skill.

The main models, strongest to weakest

  • Strong (linearizability): every read sees the most recent write, as if there were one copy of the data. Requires coordination (single primary, consensus or quorum), so it is slower and less partition-tolerant. Needed for: balances, inventory, locks, "did my payment go through?"
  • Sequential consistency: everyone sees operations in the same order, but maybe slightly delayed. Coordination systems (ZooKeeper/etcd) work here.
  • Causal consistency: cause-before-effect is preserved: if a comment replies to a post, nobody sees the comment before the post. Independent events may appear in any order. A sweet spot for social features.
  • Eventual consistency: if writes stop, all replicas converge to the same value... eventually. No time bound promised. DNS, Cassandra, DynamoDB defaults, S3 (pre-2020 behavior). Fine for: likes, view counts, profiles, most feeds.

Read-your-writes: the guarantee users actually feel

User edits their profile (write on the primary), page refreshes (read from a lagging replica), old data shows: "the app lost my edit!" The system was eventually consistent; the user experience was broken. Fixes:

  • Read your own writes from the primary for N seconds after a write (track a "last write" timestamp or replication position).
  • Monotonic reads: a user never sees data go backwards (pin a session to one replica, or remember the highest version seen).
  • Session-level stickiness: keep critical reads on the primary for the whole session.

How replicas actually sync

Replication log: the primary records every write in an ordered log (WAL in Postgres, binlog in MySQL, oplog in MongoDB); replicas replay it. Lag = how far behind the replica's replay position is. Monitor it: lag spiking means replicas are falling behind, and reads get staler.

Quorums (Dynamo-style): N replicas, W write acks, R read acks. W + R > N makes reads see the latest write. W=1 gives fast writes with a risk window (write not yet replicated when the primary dies).

Worked example: choosing per feature

E-commerce, one page at a time:

  • Stock count at checkout: strong (overselling costs money).
  • Product description and images: eventual (stale for a minute, nobody notices; serve from cache/CDN).
  • "3,421 people bought this": eventual, updated via a counter pipeline.
  • Your own review after posting: read-your-writes (show it instantly, even if others see it seconds later).

Common interview traps

  • "We'll use strong consistency everywhere": an availability and latency disaster, and unnecessary.
  • Assuming eventual consistency is "broken": it is a deliberate tradeoff with a reconciliation story.
  • Forgetting replication lag exists until a user reports stale data.

Interview Q&A

Q: Your system shows users stale balances for up to a second. Acceptable?
A: For a balance display, no: money reads go to the primary or a strongly consistent store. For "total views on a video", staleness is invisible: eventual is fine. The skill is splitting features, not picking one model.

Q: How do you implement read-your-writes on a replicated DB?
A: After a write, record the log position (LSN) returned by the primary; reads wait until a replica has replayed at least that position, else read from the primary. Postgres calls this causal consistency via LSN; MySQL via GTID waits.

Key takeaways

Interview pointer

In a feed design say: 'the feed can be eventually consistent, but users must see their own posts immediately, so I will guarantee read-your-writes by reading from the primary for a short window after their write.' This nuance is what makes an answer senior.