Quick revision
The short answer
Replicas apply the primary's changes asynchronously, so a read right after a write can reach a replica that has not applied it yet. We fix it by sending that user's reads to the primary for a short window after they write, or by remembering the log position of their write (an LSN in PostgreSQL, a GTID in MySQL) and reading only from a replica that has reached it. Consistency-critical reads such as balances always go to the primary, and replica lag is monitored so lagging replicas leave the rotation.
What to remember
In this answer, we saw that asynchronous replication lets a read reach a replica before it has applied the user's write. We restored read-your-writes by sending a user's reads to the primary for a short window after they write, or by tracking the write's LSN or GTID and reading only from a replica that has applied it. Consistency-critical reads always use the primary, and lag monitoring removes lagging replicas from routing.
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.
Replicas apply the primary's changes asynchronously, so a read right after a write can reach a replica that has not applied it yet. We fix it by sending that user's reads to the primary for a short window after they write, or by remembering the log position of their write (an LSN in PostgreSQL, a GTID in MySQL) and reading only from a replica that has reached it. Consistency-critical reads such as balances always go to the primary, and replica lag is monitored so lagging replicas leave the rotation.
Why this question is asked
Moving reads to replicas is a good way to take load off the primary, but it quietly changes a promise users take for granted: that after they save something, they see it. When the profile page shows the old name for three seconds, the user saves again, files a ticket, or stops trusting the product. Interviewers ask this to see whether we understand replication lag and can restore the guarantee only where it matters, without giving up the benefit of replicas. In this answer, we will recap how replication lag happens, then go through session-based routing, log-position tracking and always-primary reads, and finish with lag monitoring.
1A Quick Recap of Replication Lag
In asynchronous replication, the primary commits a transaction and returns success to the client immediately. Its log of changes, the WAL in PostgreSQL or the binlog in MySQL, is then streamed to each replica, which applies it in order. The time between the commit on the primary and the apply on the replica is the replication lag. Normally it is a few milliseconds, but under heavy writes, long-running queries on the replica, or network trouble it can grow to seconds or minutes.
Read-your-writes consistency is the promise that a user always sees their own earlier writes. It is weaker than full consistency: other users may still see old data briefly, which is usually fine. Our job is to give each user this promise without sending every read to the primary.
6 messages in order. Play them, or step through them one at a time.
The read arrived before the replica applied the write.
2Option 1: Read From the Primary for a Short Window
The simplest fix is to remember that a user has just written and route their reads to the primary for a short time. When a write succeeds, we store a timestamp, for example in the session, in a short-lived signed cookie, or in a small Redis key per user. For the next few seconds, a little longer than the usual worst lag, every read for that user goes to the primary.
Most users write rarely and read often, so only a small share of reads move to the primary. The cost is that the window is a guess: if lag exceeds five seconds, the user may still see old data. So we pair this with lag monitoring, which takes lagging replicas out of rotation.
3Option 2: Track the Write's Log Position
A more precise approach is to remember where in the log the user's write ended up. In PostgreSQL, after the write commits, we read pg_current_wal_lsn(), the current log sequence number, and store it with the user's session. Before reading from a replica, we compare it with that replica's pg_last_wal_replay_lsn(). If the replica has replayed at least that far, it has our write; otherwise we use another replica or the primary. MySQL offers the same idea with GTIDs, global transaction ids, and WAIT_FOR_EXECUTED_GTID_SET can make the replica wait briefly until it has applied them.
This adds a query per check, so services usually cache each replica's replay position for a few hundred milliseconds. It never guesses: the read goes to a replica only when it is known to have the write.
4Option 3: Some Reads Always Go to the Primary
Some screens should never risk old data, whoever is reading: an account balance before a transfer, stock during checkout, a permission check after an admin removes access. For these we mark the read as primary-only, for example with a read-write transaction or an explicit routing hint, and accept the load on the primary. The point of replicas is to take the bulk of tolerant reads, such as product lists or history pages, not every read.
- Consistency-critical read?YesPrimaryNoCheck the user
- User wrote in the last few seconds?YesPrimary, or a replica past their LSNNoAny healthy replica
- Replica lag above the limit?YesSkip it, use another or the primaryNoRead from it
3 steps in order. Play them, or step through them one at a time.
5Monitoring Lag and Removing Slow Replicas
Every option above assumes lag stays small, so we measure it. In PostgreSQL, the primary's pg_stat_replication view shows each replica's replay lag, and on a replica now() - pg_last_xact_replay_timestamp() gives an approximate delay. We export that as a metric, alert when it passes a threshold such as 2 seconds, and make the routing layer stop using a replica above its limit until it catches up. When all replicas lag, reads fall back to the primary, so the primary needs some spare capacity for that case.
6Follow-up questions
Why not just use synchronous replication?
Synchronous replication makes the primary wait until a replica confirms the write, which slows every write and can stall the primary when the replica is slow. Even then, in PostgreSQL the default synchronous mode confirms that the replica has flushed the WAL, not that it has applied it, so a read may still miss the write unless remote_apply is used. It solves a write-durability problem more than a read-your-writes one.
Where should the recent-write marker live if the service has many instances?
Somewhere every instance can see it: a shared store like Redis, or the client itself, for example a signed cookie or a header the front end sends back. A local in-memory map breaks as soon as the next request reaches another instance.
Does a cache in front of the replicas change this?
Yes, a cache is another lagging copy. The write must evict the cached entry after commit, and the reload must not come from a lagging replica, or the old value is cached again for the whole TTL.
7Conclusion
In this answer, we saw that asynchronous replication lets a read reach a replica before it has applied the user's write. We restored read-your-writes by sending a user's reads to the primary for a short window after they write, or by tracking the write's LSN or GTID and reading only from a replica that has applied it. Consistency-critical reads always use the primary, and lag monitoring removes lagging replicas from routing.
Open with
The cause is asynchronous replication: the primary commits and returns, and the replica applies the change a moment later, so the next page load can hit a replica that does not have the edit yet.
Then, if they want more
I would not send all reads to the primary; I only need read-your-writes for the user who wrote. The simple version is to record a recent-write marker for that user after a successful write, in Redis or a signed cookie, and route their reads to the primary for a few seconds, a bit longer than the normal lag.
The precise version is to store the write's log position, pg_current_wal_lsn in PostgreSQL or the GTID in MySQL, and read from a replica only if its replay position has passed it, otherwise from another replica or the primary. Separately, some reads are consistency-critical for everyone, like balances or stock at checkout, and those always go to the primary.
Finally, all of this depends on lag being small, so I would export replica lag, alert on it, and have the routing layer drop replicas that lag past a limit, with the primary sized to absorb reads if every replica falls behind.
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
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 "After moving reads to replicas, users complain that after editing their profile the page...": https://scrivio-interview-library.netlify.app/databases/database-scaling/90b4c3a515/