Quick revision
The short answer
The replica is asynchronous, so it replays the primary's WAL a little later, and the read after the save reached the replica before it had replayed that write. The options are to send a user's reads to the primary for a short time after they write, to compare the write's LSN with the replica's replay position and fall back when it is behind, or to make commits wait with synchronous_commit = remote_apply, which guarantees the read but slows every commit. In Spring, we make the routing explicit with a routing DataSource keyed on read-only transactions, and document which reads may be stale.
What to remember
In this answer, we saw that an asynchronous replica replays the primary's WAL a little later, so a read that follows a write can reach the replica before the change and return the old value. We looked at three ways to get read-your-writes: sending a user's reads to the primary for a short time after a write, comparing pg_current_wal_lsn() on the primary with pg_last_wal_replay_lsn() on the replica, and synchronous_commit = remote_apply, which gives the guarantee at the cost of slower commits and a dependency on the standby. In Spring, a routing DataSource keyed on read-only transactions keeps the choice explicit, and documenting which reads may be stale keeps it that way.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The replica is asynchronous, so it replays the primary's WAL a little later, and the read after the save reached the replica before it had replayed that write. The options are to send a user's reads to the primary for a short time after they write, to compare the write's LSN with the replica's replay position and fall back when it is behind, or to make commits wait with synchronous_commit = remote_apply, which guarantees the read but slows every commit. In Spring, we make the routing explicit with a routing DataSource keyed on read-only transactions, and document which reads may be stale.
Why this question is asked
Read replicas are a cheap way to take load off a primary, and most of the time nobody notices that they lag. The one place people notice is right after they change something: they save their profile, the page reloads, and the old value is back. Interviewers ask this because it tests whether we know what asynchronous replication promises, and whether we can pick a fix that matches the cost. In this answer, we will recap replica lag, reproduce the problem, and then go through three fixes, ending with how to wire the routing in Spring.
1A Quick Recap of Replica Lag
With streaming replication, the primary writes each change to its WAL and commits. The standby receives the WAL a moment later and replays it. In asynchronous mode, which is the default, the commit on the primary does not wait for the standby at all. So there is always a short window, usually a few milliseconds and sometimes several seconds under heavy load or a long-running query on the replica, where the primary has the new value and the replica still has the old one.
This is not a bug in PostgreSQL. It is the trade the default makes: commits stay fast and do not depend on the replica being up, and in exchange reads on the replica can be slightly out of date. The guarantee users expect, that after I write I can read my own write, is called read-your-writes, and asynchronous replicas do not give it by themselves.
2Reproducing the Problem
Here is the flow that goes wrong. The save is a write transaction, so it goes to the primary. The page reload is a read, so it goes to the replica, and it arrives within the lag window.
7 messages in order. Play them, or step through them one at a time.
The read reached the replica 300 ms before it replayed the write.
Notice that every step is correct on its own. The problem is only in the combination: one user's write and read went to two servers whose views were a second apart.
3Fix 1: Read From the Primary After a Write
The simplest fix is to send a user's reads to the primary for a short time after they write. When the service handles a write, it records a timestamp in the user's session or in a cookie, and for the next few seconds, say 5 seconds, which is well above the normal lag, that user's reads go to the primary. Everyone else keeps reading from the replica.
This is cheap and works well when writes are rare compared to reads, which is the usual case for profiles and settings. Its weakness is that the time window is a guess. If the replica falls 20 seconds behind during a heavy batch job, a 5-second window is not enough.
4Fix 2: Compare LSNs
A precise version uses the WAL position instead of a time. After the write commits, the service asks the primary for its current WAL position and stores that LSN with the user's session. Before reading from the replica, it checks whether the replica has replayed at least that far. If it has, the read is safe. If not, the service waits a few milliseconds or falls back to the primary.
This costs one small extra query per read for users who recently wrote, and gives a real guarantee instead of a guess. It is more code, so it fits best when lag varies a lot or when the stale read has real consequences.
5Fix 3: Make Commits Wait With remote_apply
PostgreSQL can give the guarantee itself. If the replica is listed in synchronous_standby_names and we set synchronous_commit = remote_apply, the commit on the primary does not return until the replica has replayed the change. Any read on that replica after the commit returns sees the new value.
But there is a catch. Every commit now waits for a network round trip and the replica's replay, which might add 2 to 10 ms per write, and much more if the replica is busy. Worse, if that replica goes down and there is no other synchronous standby, commits on the primary wait until someone changes the configuration. We can limit the cost by setting it only for the transactions that need it, with SET LOCAL synchronous_commit = remote_apply inside the profile update, but the dependency on the standby remains.
| Option | Guarantee | Cost |
|---|---|---|
| Primary for N seconds after a write | good if lag stays below N | more primary reads, simple code |
| Compare LSNs before reading | exact | one extra query, more code |
| synchronous_commit = remote_apply | exact, done by the database | slower commits, depends on the standby |
| Accept staleness | none | free, only for reads that may be stale |
6Making Routing Explicit in Spring
In a Spring service, the usual way to send reads to the replica is a routing DataSource. AbstractRoutingDataSource picks a target DataSource for each connection based on a key, and we choose the key from whether the current transaction is read-only. Wrapping it in LazyConnectionDataSourceProxy matters: it delays fetching the real connection until the first statement, by which time Spring has marked the transaction read-only.
The value of this design is that the decision is visible in one place, and each service method says whether it may read stale data. We should write down which reads are allowed to be stale, such as product listings, and which must not be, such as the page right after a payment, so the next developer does not move a read to the replica by accident.
7Follow-up questions
Why not just make all replicas synchronous?
Because every commit would then wait for the slowest replica, and any replica outage would block writes on the primary. Synchronous standbys are used for durability in high availability setups, usually one or two, not for every read replica.
How would you notice this problem in production before users complain?
By tracking replica lag as a metric, for example from pg_stat_replication replay_lag on the primary, and alerting when it passes the read-after-write window. A spike usually lines up with a heavy write batch or a long query on the replica holding back replay.
8Conclusion
In this answer, we saw that an asynchronous replica replays the primary's WAL a little later, so a read that follows a write can reach the replica before the change and return the old value. We looked at three ways to get read-your-writes: sending a user's reads to the primary for a short time after a write, comparing pg_current_wal_lsn() on the primary with pg_last_wal_replay_lsn() on the replica, and synchronous_commit = remote_apply, which gives the guarantee at the cost of slower commits and a dependency on the standby. In Spring, a routing DataSource keyed on read-only transactions keeps the choice explicit, and documenting which reads may be stale keeps it that way.
Open with
This is replica lag. The replica is asynchronous, so the primary commits without waiting, and the replica replays the WAL a few milliseconds or sometimes seconds later. The save went to the primary, the reload went to the replica inside that window, so the user saw the old value.
Then, if they want more
Asynchronous replicas do not give read-your-writes on their own. My first option is to route that user's reads to the primary for a few seconds after a write, using a timestamp in the session; it is simple and fits profile pages where writes are rare.
If lag varies a lot, I would store pg_current_wal_lsn after the write and, before reading, check that pg_last_wal_replay_lsn on the replica has reached it, otherwise read from the primary.
The database can also guarantee it with synchronous_commit set to remote_apply and that replica as a synchronous standby, but then every commit waits for the replica, and if it goes down commits block, so I would only use it with SET LOCAL for the few transactions that need it.
In Spring I would use an AbstractRoutingDataSource behind a LazyConnectionDataSourceProxy, keyed on whether the transaction is read-only, so @Transactional(readOnly = true) methods go to the replica unless the user just wrote. And I would document which reads are allowed to be stale.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Your Spring service reads from a streaming replica to offload the primary. Users...": https://scrivio-interview-library.netlify.app/databases/postgresql/d90fc96aeb/