Quick revision
The short answer
After the update commits on the primary and the Redis key is deleted, the next request misses and loads from a read replica that has not yet applied the update, so it caches the old display name for the full one-hour TTL. At peak, replica lag reaches 800 ms and many more misses land inside that window. The preferred fix is to read recently written keys from the primary using a short-lived marker, then a version or commit-position check that refuses to cache data older than the write, with a delayed second delete as a simpler but weaker patch, plus lag alerts and a shorter TTL.
What to remember
In this answer, we saw that the delete after commit worked, but the next miss reloaded from a lagging replica and cached the old name for an hour, which happens at peak because lag grows to 800 ms and more misses fall inside it. In order of preference, we route misses for recently written keys to the primary with a short-lived marker, or refuse to cache data older than the recorded version or commit position; a delayed second delete is a simpler patch that fails when lag exceeds it. We monitor replica lag against the margin and shorten the TTL on user-edited data.
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.
After the update commits on the primary and the Redis key is deleted, the next request misses and loads from a read replica that has not yet applied the update, so it caches the old display name for the full one-hour TTL. At peak, replica lag reaches 800 ms and many more misses land inside that window. The preferred fix is to read recently written keys from the primary using a short-lived marker, then a version or commit-position check that refuses to cache data older than the write, with a delayed second delete as a simpler but weaker patch, plus lag alerts and a shorter TTL.
Why this question is asked
Read replicas and caches both take load off a primary database, and each is fine on its own. Combined, they create a subtle bug: the cache invalidation is perfect, but the reload reads from a copy of the database that is slightly behind, so an edit seems to save and then reverts, only at busy times. Interviewers ask this to test whether we can reason about timing across three systems and rank fixes by how well they work. In this answer, we will recap replication lag, walk through the sequence, see why it appears only at peak, and go through the fixes in order of preference.
1A Quick Recap: Primary, Replicas and Lag
In PostgreSQL streaming replication, all writes go to the primary. The primary writes each change to its write-ahead log (WAL), the log of every change, and streams it to replicas, which replay it to update their own copy. Replicas answer read queries, which spreads the read load. Replay takes time, so a replica is always a little behind the primary. That delay is the replication lag; here it is usually 50 ms and up to 800 ms at peak.
Lag is normally harmless, because most reads do not care about the last second of changes. It becomes harmful when a read happens right after a write to the same row and its result is then kept for a long time, which is exactly what a cache does.
2The Sequence That Reverts the Name
A user changes their display name from Ann to Bea. The service updates the row on the primary, commits, and deletes profile:42 from Redis, all correctly. Within a few hundred milliseconds, the user's own page reloads, or another user views the profile. Redis misses, so the service loads the profile from a replica. The replica has not replayed the update yet and returns Ann. The service caches Ann with a one-hour TTL. The replica catches up a moment later, but nothing deletes the key again, so everyone sees Ann for an hour.
8 messages in order. Play them, or step through them one at a time.
The invalidation was correct; the reload read stale data and kept it.
3Why It Appears Only at Peak
Two things grow together at peak. First, the lag itself: more writes mean more WAL to replay, and replicas busy serving more reads replay it more slowly, so the window grows from 50 ms to 800 ms. Second, the chance that a miss falls inside that window: a popular profile is requested more often at peak, so a request is much more likely to arrive within 800 ms of the delete than within 50 ms off-peak. And the most likely requester is the editing user, whose page reloads right after saving. Multiply a sixteen-times-wider window by a higher request rate and a rare event becomes a common one.
4Fix 1: Read Recently Written Keys From the Primary
The most robust fix is to avoid reading stale data in the first place, but only for keys that were just written, so the primary does not take all the read load. On write, we set a small marker key in Redis with a TTL comfortably above the maximum expected lag. On a cache miss, the loader checks the marker; if it exists, it reads from the primary instead of a replica.
The marker is set before the delete, so any miss after the delete sees it. Five seconds against a maximum observed lag of 800 ms leaves a wide margin. The extra primary load is small: only misses on keys written in the last five seconds go there. This also gives the user read-your-own-writes behaviour on every page, not just through the cache.
5Fix 2: Refuse to Cache Data Older Than the Write
The second approach checks freshness instead of routing. At write time we record the row's new version, or the commit position, the WAL location of the commit, which PostgreSQL reports as an LSN, in Redis next to the key. When a loader reads from the replica, it compares: if the row's version is older than the recorded one, or the replica's replayed position, from pg_last_wal_replay_lsn(), is behind the recorded commit position, the loader does not cache the result and instead retries on the primary.
This is more precise than a time-based marker, because it works however large the lag becomes. It costs a version column or LSN tracking and a little more code, which is why it ranks second for most teams; for data where correctness matters a lot, it can rank first.
6Fix 3: A Delayed Second Delete, and Its Limit
The simplest patch is to delete the key again after the maximum expected lag, for example two seconds after commit. Any stale value cached during the lag window is removed, and the next miss reads a caught-up replica.
But it fails whenever lag exceeds the delay. If one day lag reaches three seconds, a load at 2.5 seconds still caches the old name, after the second delete has already run. It also adds a scheduled task per write that can be lost if the instance stops. So it is a quick improvement while the better fix is built, not the final answer.
| Fix | Works when lag exceeds plan? | Cost |
|---|---|---|
| Marker: misses on recent keys read the primary | Only if marker TTL > lag; set it wide | Small primary load, a few lines |
| Version or LSN check before caching | Yes | Version column or LSN tracking |
| Delayed second delete (2 s) | No | Scheduled task per write |
| Shorter TTL on user-edited data | Limits damage only | More misses |
7Monitoring and the TTL
Every fix that relies on a time margin needs to know when the margin is in danger. We monitor replica lag, from pg_stat_replication on the primary (replay_lag) or from the replica's last replay timestamp, and alert when it approaches the protection margin, for example at 2 seconds for a 5-second marker. And if the business is sensitive to stale profile data, we shorten the TTL on user-edited data from one hour to, say, five minutes, which limits the damage of any race we did not foresee.
8Follow-up questions
Why does the problem appear only at peak times?
Because both factors grow at peak. Replication lag rises from about 50 ms to 800 ms as write volume and replica load increase, and request rates rise, so a cache miss is far more likely to happen inside the lag window right after the delete. Off-peak, the window is so short that a reload almost never falls inside it.
Why not send all cache misses to the primary?
It would fix the bug, but the replicas exist to carry read load. If the cache hit ratio drops, during a deploy or a Redis restart, all misses would hit the primary at once and could overload it. Routing only recently written keys keeps the primary's extra load tiny.
Would synchronous replication solve it?
PostgreSQL's synchronous_commit = remote_apply makes a commit wait until the replica has applied it, which removes this lag window, but every write then waits for a replica, adding latency and making writes fail or stall if the replica is slow. For a profile service that is usually too expensive for the problem.
9Conclusion
In this answer, we saw that the delete after commit worked, but the next miss reloaded from a lagging replica and cached the old name for an hour, which happens at peak because lag grows to 800 ms and more misses fall inside it. In order of preference, we route misses for recently written keys to the primary with a short-lived marker, or refuse to cache data older than the recorded version or commit position; a delayed second delete is a simpler patch that fails when lag exceeds it. We monitor replica lag against the margin and shorten the TTL on user-edited data.
Open with
The invalidation is fine; the reload is the problem. The service commits the new name on the primary and deletes the Redis key, but the next request misses and loads from a read replica that has not replayed the update yet, so it caches the old name with the one-hour TTL, and nothing deletes it again.
Then, if they want more
It happens only at peak because lag grows from 50 ms to 800 ms and traffic is higher, so a miss is much more likely to land in that window, and the most likely reader is the user who just saved.
My first fix would be to read recently written keys from the primary: on write, set a marker key with a TTL well above the maximum lag, say five seconds, and on a miss, if the marker exists, load from the primary. That costs very little primary load.
Second option is a freshness check: record the row version or the commit LSN at write time and refuse to cache anything loaded from a replica that is older, retrying on the primary instead. A delayed second delete after about two seconds is a simpler patch, but it fails whenever lag exceeds the delay.
On top of that I would alert on replica lag as it approaches the margin, and shorten the TTL on user-edited data if stale names matter to the business.
About this answer
- Last updated
- 13 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 official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
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 "Explain the sequence that causes this, and give the fixes in order of preference.": https://scrivio-interview-library.netlify.app/architecture/caching-strategies/d965667475/