Stackbook Logo
data-consistencyestablished · low operational burden

Read-Your-Writes Consistency

Also known as: read-after-write, session-consistency

Intent

Guarantee that a client always sees its own prior writes even when reads are served from lagging replicas.

Problem

With asynchronous replication, a user writes to the leader and then reads from a replica that has not yet received the update. They see their change vanish and assume it failed. Global strong consistency is too costly just to fix this.

Forces

  • •Replicas lag behind the primary
  • •Users notice their own stale data immediately
  • •Reads should still scale across replicas
  • •Requests may hit different servers or devices

Solution

Overview

Read-your-writes, or read-after-write, consistency ensures a client sees the effects of its own earlier updates. It is a session guarantee that sits between eventual consistency and linearizability, and fixes the common confusion where a saved edit seems to disappear after reload.

How it works

The write returns a position marker; subsequent reads only use replicas that have reached it.

POST /profile  -> primary commits at LSN 4821
               <- cookie: minLsn=4821
GET /profile   -> router picks replica R
   if R.replayLsn >= 4821: read from R
   else: read from primary (or wait briefly)

Simpler variants route a user's reads to the primary for N seconds after any write, with N longer than typical replica lag. Dynamo-style stores achieve the effect by using quorum reads or sticky sessions. For multi-device users, store the last write position server-side keyed by user id.

Pitfalls

  • A fixed time window fails when lag spikes during load or maintenance; position-based checks adapt.
  • Reading everything from the primary after writes can overload it during write bursts.
  • Different devices share no cookie, so the token must live server-side.
  • Reads of resources written by other users can still be stale; this is not monotonic reads or causal consistency.
  • Caches in front of replicas can serve old values; invalidate or version them on write.

Key takeaways

  • Pass a write position with the session and check replicas against it.
  • Fall back to the primary when no replica is fresh enough.
  • Monitor replication lag, since the guarantee degrades into primary load when lag grows.

✓ When to Use

  • •User-facing apps with read replicas and async replication
  • •Profile edits, posts and settings that users view immediately
  • •Systems where eventual consistency is acceptable for others viewing data

✗ When Not to Use

  • •Fully synchronous or single-node databases
  • •Data where users never read back their own writes quickly
  • •Needing guarantees across different users

Pros

  • +Removes the most visible stale-read surprise
  • +Keeps most reads on replicas
  • +Cheap compared with strong consistency

Cons

  • −Requires session or token plumbing
  • −Cross-device guarantees need shared state
  • −Does not give monotonic reads for others

Cost Profile

Infrastructure

Little beyond replica routing

Operational

Low to medium: monitor replica lag

Cognitive

Medium: tracking positions

Failure Modes

  • Token lost across devices or after cookie expiry exposes stale reads

  • Lagging replica causes primary fallback spikes

  • Clock-based windows too short for actual replication lag

Real-World Examples

Alternatives

    Related Patterns

    • read-replica
    • replication
    • caching

    Competency Domains

    data staterequirements constraints