Skip to content

Lesson 5 of 13 · 16m

Consistency Is a Slider, Not a Switch

Linearizable, sequential, causal, and eventual are points on a spectrum — and most real reads sit somewhere between 'just committed' and 'probably replayed'.

When a Postgres read replica answers your SELECT, it answers a question about the past. The primary committed your INSERT at WAL LSN 0/17B3F40; the replica is replaying at 0/17B3C00. Your row exists in a future the replica has not reached yet, and SELECT on the replica comes back empty. Nothing failed. This is what asynchronous replication is for — and it is the daily experience of everyone who has "eventual consistency" without knowing the name for the specific guarantee they just lost.

That guarantee is read-your-writes, and it is a session property, not a cluster property. The models worth naming precisely:

  • Linearizable (Herlihy & Wing, 1990): each operation appears to take effect atomically at some instant between its invocation and its response. The strongest single-object model — it gives you recency, which is why "read the leader" is the cheap way to get it.
  • Sequential: every operation lands in some total order consistent with each node's own order — but not required to match real time. A steady drizzle of "a bit stale."
  • Causal: writes that could have caused each other are seen in that order by everyone; concurrent writes may be seen in either order.
  • Eventual: if writes stop, replicas converge. That is the whole contract. It says nothing about the value they converge to, and nothing about what you read while writes are still flowing.

Where the slider actually lives

The consistency level is a per-request knob on the systems you already run. DynamoDB ConsistentRead=true costs double the read capacity of an eventually consistent read and does not exist on global secondary indexes — GSIs are eventual only, forever. etcd reads are linearizable by default against the Raft leader, and the client can ask for Serializable to skip the quorum check and accept a stale answer for less latency. MongoDB's writeConcern: "majority" decides when a write is acknowledged; readConcern: "linearizable" decides whether the read is guaranteed to observe the majority-committed state. Every one of these is a latency-for-truth trade, priced per call.

Postgres makes the subtlest version of the trade. With a synchronous standby, synchronous_commit = on waits for the standby to flush the WAL to disk — durable, but not necessarily applied. synchronous_commit = remote_apply waits until the standby has replayed far enough that your row is visible there. Between those two settings is a window where "synchronous replication" still lets a read replica miss your write. To route reads by position instead, hand the client its commit LSN — pg_current_wal_lsn() at write time — and only let it read a replica whose pg_last_wal_replay_lsn() has passed it. That is read-your-writes built by hand, and it is what Citus, several proxies, and a lot of hand-rolled session tables implement.

The wrong mental model

"Eventual consistency means the data is eventually right." No. It means replicas converge when writes stop — and they converge to whatever the conflict-resolution rule selects, which under last-write-wins is whatever the clock skew prefers. "We use a CP database" is the other version of the same confusion: CP describes behavior during partitions, not what your per-request consistency level is on a healthy Tuesday.

What to actually do

Decide consistency per operation, not per database. A feed read tolerates seconds of staleness; a permission check before a mutation does not. For write-then-read flows — post then reload, update then refresh — route the read to the primary/leader, or carry a position token (LSN, Raft revision, Mongo clusterTime) and enforce it at the replica. Expose lag as a first-class metric — replay_delay, replication slot lag, etcd raft_index drift — because the failure mode of eventual consistency is silent staleness, and silent is the dangerous part.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.