Scrivio Interview library
DesignAdvanced7 min read · updated 13 November 2026

A service runs in three regions, each with its own Redis cache. The database primary is in one region and replicates to read replicas in the others with 1 to 2 seconds of lag, while invalidation messages reach all regions within about 200 ms.

Why can remote regions end up caching stale values even though invalidations arrive quickly, and how would you design invalidation across regions?

Try it yourself

Cache-aside: one read, three cache states

See what a cache hit saves, what a miss costs, and why a write deletes the cached entry instead of updating it.

Read this example as text

When the entry is in the cache (a hit)

  1. A request arrives. A caller asks the service for product 42. The cache already holds it.
  2. Ask the cache first. The service always asks the cache before the database. It sends GET product:42.
  3. Hit: the value comes back. The entry is there and still fresh, so the cache answers at once. The database is never touched.

1 cache call, 0 database reads.

When it was never cached (a miss)

  1. A request arrives. A caller asks the service for product 42. Nobody has asked for it before, so the cache holds nothing.
  2. Ask the cache first. The service always asks the cache before the database. It sends GET product:42.
  3. Miss: nothing comes back. The key was never stored. The cache answers nil, and the service must find the value itself.
  4. Read the database. The service runs SELECT ... FROM products WHERE id = 42. This is the slow, authoritative read the cache exists to avoid.
  5. The row comes back. The database returns the row. The service now has the value, but the cache is still empty.
  6. Store it for next time. Before answering, the service writes the value into the cache with a time to live: SET product:42 ... EX 300. The next read inside 300 seconds will be a hit.
  7. Answer the caller. The caller gets the value. It cost one database read, which a hit would have saved.

2 cache calls, 1 database read.

When its time to live has run out

  1. A request arrives. A caller asks the service for product 42. The cache held it once, but its time to live ran out 12 seconds ago.
  2. Ask the cache first. The service always asks the cache before the database. It sends GET product:42.
  3. Miss: nothing comes back. An expired key is the same as a missing one: Redis no longer returns it, and removes it. The cache answers nil, and the service must find the value itself.
  4. Read the database. The service runs SELECT ... FROM products WHERE id = 42. This is the slow, authoritative read the cache exists to avoid.
  5. The row comes back. The database returns the row. The service now has the value, but the cache is still empty.
  6. Store it for next time. Before answering, the service writes the value into the cache with a time to live: SET product:42 ... EX 300. The next read inside 300 seconds will be a hit.
  7. Answer the caller. The caller gets the value. It cost one database read, which a hit would have saved.

2 cache calls, 1 database read.

When the price changes afterwards

  1. The price changes. Later, someone updates the price to 25. The service writes the database first: UPDATE products SET price = 25 WHERE id = 42.
  2. Delete the cached entry. Then it deletes the key: DEL product:42. It does not write 25 into the cache. Deleting is simpler and safer: the next read misses and loads whatever the database holds then.
  3. The next read will miss. The cache is empty for this key again, so the next request repeats the miss path and stores 25.

3 cache calls, 1 database read, 1 database write.

What this example leaves out
  • One request at a time. Two requests missing at the same moment (a stampede) are not shown.
  • The write deletes the key. Some systems update the cache instead, which this example does not model.
  • The numbers are counts of round trips, not measured times.

The invalidation reaches a remote region in 200 ms, but the next read there misses and loads from the local replica, which is still 1 to 2 seconds behind, so the old value is cached again for the full TTL. The fix is to invalidate relative to the data each region reads: drive a region's invalidations from its own replica's progress, or carry the new version in the invalidation and refuse to cache older values, or at least delete again after the maximum replica lag. Reads right after a user's own write go to the primary region or wait for a version token.

Why this question is asked

Multi-region systems surprise people because each piece works as designed: invalidations are fast, replication is fast, and still users see old data for minutes. Interviewers ask this to see whether we can reason about ordering between two independent pipelines. In this answer, we will walk through the race, see why speed does not help, look at three ways to tie invalidation to replication, handle read-your-writes, and decide what to monitor.

1Walking Through the Race

Region A holds the database primary. Regions B and C each have a read replica and their own Redis cache. A user in A updates a product at time 0. The change is committed in A, and an invalidation message is published to all regions.

At 200 ms, region B receives the invalidation and deletes product:5001 from its cache. At 300 ms, a user in B reads the product. The cache misses, so the service loads it from B's replica. But the replica will only apply the change at around 1,500 ms, so it returns the old row, and the service caches it with a 10-minute TTL. The invalidation has already been spent, and region B now serves the old product for 10 minutes.

Step throughFast invalidation, slow replica
Region A primaryInvalidation busRegion B cacheRegion B replica
t=0 commit, publish invalidate 5001
t=200 ms DEL product:5001
t=300 ms miss, SELECT product 5001
old row (replica still behind)
cache old row for 10 minutes
t=1500 ms replica applies the change

6 messages in order. Play them, or step through them one at a time.

The invalidation arrived before the data did, so the old value was cached again.

2Why Faster Invalidation Does Not Help

The intuition says that invalidation should be as fast as possible. Here that intuition is wrong. The invalidation is useful only if, after it, a reload sees the new data. In region B, reloads read the replica, so the invalidation must happen after the replica has the change, not after the primary has it. Making the message faster only widens the window in which a reload fetches old data.

So the principle is: invalidate relative to the source your cache loads from. Each of the designs below is a way to apply that principle.

3Invalidating From Each Region's Replica

The most robust design generates each region's invalidations from that region's own replica. If the change stream is read from B's replica, an invalidation can only exist once B's replica has applied the change, so the race disappears by construction. PostgreSQL 16 and later can run logical decoding on a standby, which makes this possible with a CDC connector per region (check your version and connector support).

A lighter variant keeps one central change stream but makes each region wait. The invalidation message carries the commit's log position, its LSN, and the regional invalidator holds it until the local replica has replayed past that position.

SQL
-- message: {"key": "product:5001", "lsn": "3/A1C0F2E8"}
SELECT pg_last_wal_replay_lsn() >= '3/A1C0F2E8'::pg_lsn AS replayed; -- false at 300 ms
-- ... the invalidator checks again a few hundred milliseconds later
SELECT pg_last_wal_replay_lsn() >= '3/A1C0F2E8'::pg_lsn AS replayed; -- true at 1500 ms
-- now: DEL product:5001 in region B

The invalidator batches these checks, one query covers every waiting message, so the cost is small. Region B's invalidation now arrives at about 1,500 ms instead of 200 ms, which looks slower but is the first moment at which deleting the key actually helps.

4Versions in the Invalidation, or a Second Delete

If changing the pipeline is not possible, two cheaper defences help. The first carries the new version of the row in the invalidation. Region B stores a short-lived marker, min:product:5001 = 18, and when a reload produces a row older than 18, the service returns it to this one request but does not cache it. The next reload, after the replica catches up, gets version 18 and caches it. The cost is a version column on the data and an extra key read per miss.

The second is the delayed double delete: delete at 200 ms as now, and again after the maximum expected replica lag, for example at 3 seconds. Anything cached from the stale replica in between is removed by the second delete. It is simple, but it is a bet on the lag bound; when the replica falls 30 seconds behind during a heavy migration, the second delete fires too early as well.

Cross-region invalidation options
DesignGuaranteeCost
Change stream per replicaInvalidate only after replica has dataCDC per region
Wait for replica LSNSame, with one central streamLSN in messages, replay checks
Version floor on reloadNever caches an older versionVersion column, extra read on miss
Delayed second deleteWorks if lag stays under the delaySimple; fails under large lag

5Read-Your-Writes for the User Who Wrote

Even with correct invalidation, the user who just saved a change in region B expects to see it at once, and B's replica may not have it for a second or two. Two common answers exist. Route that user's reads to the primary region for a few seconds after a write, for example by setting a cookie with the write time that the routing layer checks. Or return a version token, such as the commit LSN, with the write response; the client sends it with later reads, and region B serves from cache or replica only once its replica has reached that position, otherwise it waits briefly or reads from the primary.

Both affect only the writer's own requests, so the rest of the traffic keeps the low latency of local reads.

6What to Monitor

Replica lag per region is the first number, because it is the size of the race window. The second is the stale-read rate: a sampler that reads a small fraction of cached keys and compares them with the primary shows how often a region serves old data, which is the number users feel. And for data where cross-region staleness is costly, such as prices or permissions, TTLs should be short enough that even a missed invalidation heals quickly.

7Follow-up questions

What happens to this design during a regional failover?

If region A fails and B's replica is promoted, writes that A committed but had not yet replicated, the last second or two, are lost, while caches in some regions may already hold values from those lost writes. LSN tokens and version floors from the old timeline also no longer compare cleanly. The safe step after a failover is to bump a global cache namespace version, or flush the caches that could hold lost data, and to reset version floors, accepting a period of cold caches in exchange for consistency with the new primary.

Why not just use a short TTL in remote regions?

It limits the damage, but a TTL short enough to hide a 2-second race would be a few seconds, which destroys the hit ratio. Tying invalidation to replication keeps long TTLs and still avoids caching stale values.

Could we update the cache instead of deleting it?

Writing the new value into remote caches from the primary region avoids the reload from the stale replica, but it creates its own ordering problem: two updates in quick succession can arrive out of order and leave the older value cached. Deletes are idempotent and order-insensitive, which is why most designs delete and fix the timing instead.

8Conclusion

In this answer, we saw that fast invalidation across regions can make things worse, because the remote cache reloads from a replica that has not yet received the change and caches the old value for a full TTL. The fix is to invalidate relative to the source each region reads from: generate invalidations from each replica's change stream or wait for its replay position, or carry versions and refuse to cache older rows, with a delayed second delete as a simpler fallback. Writers get read-your-writes through primary routing or version tokens, and we monitor replica lag and stale-read rates.

What to say in the interviewAbout 80 seconds aloud

Open with

The invalidation arrives in the remote region after 200 milliseconds, but the data arrives after one or two seconds. So the remote cache deletes the key, the next read misses, loads from the local replica that is still behind, and caches the old value again for the whole TTL.

Then, if they want more

Faster invalidation does not help; it makes the window bigger. The rule is to invalidate relative to the source the cache loads from.

The cleanest design generates each region's invalidations from its own replica, either with change data capture on the replica, which PostgreSQL supports on standbys since version 16, or by putting the commit LSN in the message and having the regional invalidator wait until pg_last_wal_replay_lsn has passed it.

Cheaper options are to carry the new version in the invalidation and refuse to cache a loaded row with a lower version, or to delete a second time after the maximum replica lag, which fails if lag spikes.

For the user who just wrote, I would route their reads to the primary region for a few seconds, or give them a version token the remote region must reach before serving. And I would monitor replica lag and a sampled stale-read rate per region, with short TTLs for data where staleness is costly.

About this answer

Last updated
13 November 2026
Versions it names
PostgreSQL 16
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 "Why can remote regions end up caching stale values even though invalidations arrive...": https://scrivio-interview-library.netlify.app/architecture/caching-strategies/5e11b47592/