Video in progress

Strong Junior 🏗️ System design

An admin changed a product's price, but the site still shows the old price for 10 minutes. What's the cause and how do you fix it?

The price sits in the cache with a TTL and the key wasn't evicted on update. The fix is to evict the exact key after the write, to account for every cache layer if there are several (local + Redis + CDN), and to shorten the TTL for important data like prices.

📖 Answer

The first step is to find exactly where the stale value is coming from. Check the layers from the bottom up: is the new price in the database? What does GET product:42 return in Redis? Doesn't the application use a local Caffeine cache? What is Cache-Control on the response — hasn't the CDN or the browser stored it?

Typical causes:

  • @CacheEvict is in place, but the method was called from inside the same class — the Spring proxy doesn't kick in and no eviction happens.
  • The key doesn't match: product:42 on read, product::42 on evict.
  • The eviction hit the local cache of only one instance, and the other 4 instances still hold the old value.
  • The data was changed directly with SQL — the application doesn't know about it at all.

With several instances that have local caches, the eviction has to reach all of them, for example with a "product:42 is stale" message over Redis Pub/Sub. Or give the local cache a very short TTL (10–30 seconds) and keep the main cache in Redis.

One more rule where money is involved: the price shown in the cart can come from the cache, but at payment time the price is always re-read from the database.