← Building blocks

Let users see their own writes

Consistency & coordinationsenior

A user posts, refreshes, and the post is missing because the read hit a lagging replica.

Say it like this

Only the writer needs to see the write immediately, so after a write I route that session to the primary for a short window, keyed by the commit position, and leave everyone else on replicas.

Basic solution

Route reads for a short window after a write to the primary (or to a replica that has caught up), using a session marker such as the last write’s LSN or timestamp.

sticky-to-primary after writeLSN / version tokens in the sessioncausal consistency

Routing the writer to a caught-up replica

The write returns the commit position, which travels with the session. The read router sends the next read to the primary or to a replica that has applied that position.

How it works

  1. The client writes; the primary commits and returns the commit position: a Postgres LSN, a MySQL GTID, or simply a timestamp. That position identifies "the state that includes my write".
  2. The service stores that position with the user's session: in a cookie, in a response header the client echoes back, or in a Redis entry keyed by user id.
  3. On a subsequent read, the router compares the session's position with each replica's applied position, which the replica reports as the last log entry it has replayed.
  4. If a replica has applied the session's position, it has the write, so the read goes there; otherwise it goes to the primary (or waits briefly for a replica to catch up).
  5. After a few seconds, or once any replica has passed the position, the session marker is dropped and reads return to the normal replica pool.

Simpler form

  • Pin the user to the primary for N seconds after any write, where N comfortably exceeds normal replication lag. Less precise, no position tracking needed.

Worked example

News feed: a user posts and immediately refreshes

  • Replication lag p99 is 300 ms; the user's refresh arrives about 200 ms after the post, inside the lag window, so a replica read would miss the post.
  • Rule: after a write, the session carries the primary's LSN; the read router sends reads to the primary until a replica reports an LSN ≥ that value.
  • Traffic impact: 10M DAU × 2 posts/day = 20M writes/day. Each pins the next ~1 second of reads to the primary, say 2 reads. That is 40M extra primary reads/day ≈ 460/s on top of ~230 writes/s, a small fraction of what one primary serves, which is the cost of the guarantee.
  • Everyone else keeps reading from replicas; the primary stays a write box with a small read tax.
  • Alternative for the feed itself: the client inserts the new post into its local list optimistically and never needs the read at all.

Variants and when each wins

Pin-to-primary window

  • Every read from a user who just wrote goes to the primary for a fixed window. It is trivial to implement and wins as a quick fix; it over-pins when lag is small and under-pins when lag spikes past the window.

Position tracking (LSN / GTID)

  • The read goes to any replica that has applied the write's position, which is exact. It wins when primary read load matters; it needs each replica to expose its applied position and the router to check it.

Optimistic UI / client-side merge

  • The client shows its own write immediately from local state and never depends on the read path for it. It wins for feeds, comments and chat, where the client owns the view.

Causal tokens across services

  • A version token travels with every downstream call so caches and derived stores also honour the ordering, not just the database replicas. It wins when the read passes through a cache or a search index that lags on its own.

Use optimistic UI where the client owns the view; pin-to-primary for a quick win; position tracking when primary read load matters.

Failure modes

Window shorter than lag

  • The bug comes back during replica slowdowns. It happens because the pin window is a fixed 1 s while lag spiked to 5 s, so reads returned to replicas before they had the write. Fix it with position tracking, or derive the window from measured lag with a safety margin.

Caches bypass the rule

  • The user still sees stale data even though the read went to the primary. It happens because a cache in front of the primary still holds the old value. Fix it by invalidating the cache on write, or by bypassing the cache for pinned reads.

Marker lost across devices

  • The user writes on their phone and sees a stale read on their laptop. It happens because the session marker lives in a per-device cookie. Fix it by storing the position in Redis keyed by user id, so every device shares it.

Primary overload

  • Primary read load climbs with write volume. It happens because long pin windows on a write-heavy workload send a growing share of reads to the primary. Fix it by shortening the window with position tracking, or by moving the feature to optimistic UI so the read is not needed.

Derived stores lag independently

  • The search index does not show the new item even though the database read is fresh. It happens because the index is fed asynchronously from the database, so replica routing does not help it. Fix it by carrying the version token into the search service and having it wait for that version or fall back to the primary.

Interviewer follow-ups

Answer out loud before you open each one.

Why not route all reads to the primary and avoid the problem?

Because reads outnumber writes by orders of magnitude and the primary would become the bottleneck that replicas exist to relieve. Read-your-writes keeps that benefit by making only the writer, for a short window, pay for freshness.

How do you implement it with Postgres?

After the write, capture pg_current_wal_lsn() on the primary and store it in the session. Before a read, check pg_last_wal_replay_lsn() on the candidate replica; if it is at or past the stored LSN, read there, otherwise use the primary. Connection poolers can do this comparison for you.

Does this give other users consistency too?

No, only the writer. Another user may still see the old value until replication catches up, which is acceptable because they cannot know when the write happened. If other readers need it, you are asking for strong consistency, not read-your-writes.

What is monotonic reads and how does it relate?

Monotonic reads guarantees a user never sees data go backwards in time, which can happen when consecutive reads hit replicas with different lag. Sticky routing of a session to one replica gives it cheaply. Read-your-writes is a different, stronger per-writer guarantee; the two are usually offered together.

Connections

In study paths
Scaling reads
Built on
Write-ahead log
Pairs with
Spread reads across replicas · Accept a short delay for scale · Invalidate the cache when data changes
Pulls against
Keep servers stateless
Used in
News Feed (Twitter / X timeline) · Chat App (WhatsApp / Messenger)

The same parts on other clouds

The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Amazon EC2 Auto ScalingRead routerCompute Engine managed instance groupsVirtual Machine Scale SetsKubernetes Deployment with a HorizontalPodAutoscaler, Nomad
Amazon AuroraPrimary, Replica A, Replica BAlloyDB for PostgreSQL, Cloud SQLAzure Database for PostgreSQL or MySQLPostgreSQL, MySQL, CockroachDB