Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

A product catalogue cache in Redis uses a fixed TTL of 10 minutes. Every 10 minutes the database CPU jumps to 100 percent for a few seconds. Explain the problem and give at least three mitigations.

The popular product keys were all cached at about the same time with the same 10-minute TTL, so they expire together and many concurrent requests miss at once and all hit the database, a cache stampede or thundering herd. We mitigate it by adding random jitter to TTLs so expiries spread out, by collapsing concurrent loads so only one request recomputes a key (a short SET NX lock across instances, or @Cacheable(sync = true) within one instance), by refreshing hot keys ahead of expiry or serving stale data while refreshing, and by protecting the database with bulkheads or rate limits on the loader path.

Why this question is asked

A cache is supposed to protect the database, so it is alarming when the cache itself causes regular spikes. The pattern in this question, a spike exactly every TTL, is one of the clearest symptoms in performance work, and interviewers ask it to see whether we recognise it and know several mitigations with their trade-offs. In this answer, we will recap how cache-aside reads behave on a miss, explain why the spikes line up with the TTL, and then work through four mitigations in code, finishing with how to combine them.

1A Quick Recap of Cache-Aside Misses

In cache-aside, the application asks Redis for a key first. On a hit, it returns the cached value. On a miss, it loads the value from the database, writes it to Redis with a TTL, and returns it. With a 10-minute TTL, each key is reloaded at most every 10 minutes, which keeps the database load low as long as misses are spread out over time.

The weak spot is the moment a popular key expires. Between the expiry and the moment the first request has written the new value back, every request for that key misses. If 2,000 requests per second ask for the catalogue's top products and the database query takes 300 ms, around 600 requests may miss and run the same query during that window.

2Why the Spikes Come Every 10 Minutes

The keys were not written at random times. Perhaps the catalogue was warmed at startup, or a deploy cleared the cache, or a nightly import reloaded everything at once. Whatever the cause, thousands of keys were written within seconds of each other with the same TTL, so they expire within seconds of each other, ten minutes later, and again ten minutes after that, because each reload rewrites them together.

So it is not one key stampeding but thousands at the same moment, each with its own herd of concurrent misses. The database receives a burst of tens of thousands of identical and similar queries, its CPU goes to 100 percent, queries slow down, which widens the miss window, which lets even more requests miss. The pattern repeats forever because nothing breaks the synchronisation.

Step throughA synchronised expiry
Many requestsRedisDatabase
12:10:00 GET product:1..5000
all expired: miss
thousands of identical loads at once
CPU 100 percent, each query slower
results after seconds
SET each key EX 600, all at 12:10:0x
next expiry together at 12:20

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

Reloading the keys together keeps them synchronised forever.

3Mitigation 1: Add Jitter to the TTL

The simplest fix breaks the synchronisation. Instead of exactly 600 seconds, each key gets 600 seconds plus a random amount, say up to 20 percent more. Keys written together then expire spread over two minutes, and after a cycle or two the reloads are spread evenly.

Java
Duration ttlWithJitter(Duration base) {
long extra = ThreadLocalRandom.current().nextLong(base.toSeconds() / 5 + 1); // 0..120 s
return base.plusSeconds(extra);
}
redisTemplate.opsForValue().set("product:" + id, json, ttlWithJitter(Duration.ofMinutes(10)));
// product:1 -> 637 s, product:2 -> 702 s, product:3 -> 611 s ...

With Spring's RedisCacheManager, newer Spring Data Redis versions let us compute a TTL per entry through a TtlFunction in RedisCacheConfiguration, which we can use to add jitter; check your version for the exact method. Jitter does not help a single very hot key, but it removes the synchronised burst, which is the main cause here.

4Mitigation 2: Collapse Concurrent Loads

Even one hot key can stampede. Request collapsing means only one request recomputes a key while the others wait for its result or serve slightly stale data. Within one JVM, Spring offers @Cacheable(sync = true): concurrent callers for the same key in that instance wait while one of them runs the method.

Java
@Cacheable(cacheNames = "product", key = "#id", sync = true)
public Product find(long id) { return repository.findById(id).orElseThrow(); }
// 200 threads in one instance ask for product 7 at once: 1 database query
// 20 instances: up to 20 database queries for product 7
Step throughLoading a missed key with a Redis lock
  1. GET product:7cache-aside read
  2. Hit?
    YesReturn the valueNoTry the lock
  3. SET lock:product:7 token NX PX 5000 succeeded?
    YesLoad from the database and SET with TTLNoWait 50 ms, read the cache again
  4. Release the lock with a compare-and-deleteonly if the token is ours

4 steps in order. Play them, or step through them one at a time.

Only one instance reloads each key; the lock expires on its own if that instance dies.

Notice the limit in the last comment: sync works per instance. Across a fleet, we need a lock in Redis itself. The first request to miss takes a short lock with SET NX and an expiry; the others see the lock and either wait briefly and read the cache again, or return the stale value if we kept one.

5Mitigation 3: Refresh Ahead or Serve Stale

Rather than waiting for a key to expire, we can refresh it before it does. One way stores a soft expiry inside the value: the Redis TTL is, say, 15 minutes, but the value carries a refreshAfter time of 10 minutes. A request that reads a value past its refreshAfter returns it immediately and triggers one background refresh, guarded by the same lock, so users never wait for the database and the herd never forms.

Java
record Cached<T>(T value, Instant refreshAfter) {}
Cached<Product> c = cache.get(id); // TTL in Redis: 15 min
if (c != null && Instant.now().isAfter(c.refreshAfter())) {
refresher.refreshAsync(id); // one background reload, lock-guarded
}
return c != null ? c.value() : loadWithLock(id); // stale-while-revalidate

Another way is a scheduled job that recomputes the few hundred hottest keys every few minutes. Both cost extra loads for keys that might not have been read again, so we apply them to hot keys, not to everything.

6Mitigation 4: Protect the Database

Whatever we do, a bug or a cold start can still send a burst of misses. A bulkhead, a limit on how many concurrent loader calls may run, for example a semaphore of 20 in each instance or a Resilience4j bulkhead, keeps the database within capacity. Calls beyond the limit wait briefly, or fail fast and serve stale data or a fallback. A rate limiter on the loader path does the same in requests per second.

In practice we combine them: jitter on every TTL, a Redis lock or sync for hot keys, refresh ahead for the top items, and a bulkhead as the last line of defence.

7Follow-up questions

What are the limits of @Cacheable(sync = true) across instances?

It only coordinates threads inside one JVM, because the lock is local to the cache implementation in that instance. With 20 instances, up to 20 loads of the same key can still run at once. It also only works with a single cache name and no unless condition. For fleet-wide collapsing we need a distributed lock, such as SET NX in Redis, or a refresh-ahead design that avoids the miss.

Why not just use a longer TTL?

A longer TTL makes the stampede less frequent but not smaller, since the keys are still synchronised, and it serves staler data. Jitter and collapsing fix the cause; the TTL should be chosen by how stale the data may be.

What if the lock holder dies during the load?

That is why the lock has a short expiry, such as 5 seconds. If the instance holding it dies, the lock disappears on its own and the next request takes it. Waiting requests should also have a time limit, after which they load the data themselves or return a fallback, so a stuck lock never blocks users for long.

8Conclusion

In this answer, we saw that synchronised writes with a fixed 10-minute TTL make thousands of keys expire together, so concurrent misses hit the database at once in a cache stampede, every 10 minutes. We broke the synchronisation with TTL jitter, collapsed concurrent loads with @Cacheable(sync = true) per instance or a SET NX lock across the fleet, refreshed hot keys ahead of expiry or served stale data while refreshing, and protected the database with bulkheads or rate limits on the loader path.

What to say in the interviewAbout 80 seconds aloud

Open with

This is a cache stampede, or thundering herd. The popular keys were written at about the same time, for example after a warm-up, a deploy or an import, all with exactly ten minutes of TTL, so they expire together.

Then, if they want more

In that moment every request for those keys misses, and many concurrent requests all run the same database queries, which pushes the CPU to 100 percent. Because they are all rewritten together, they stay synchronised and it repeats every ten minutes. I would use several mitigations.

First, add random jitter to the TTL, for example ten minutes plus up to two minutes random, so expiries spread out.

Second, collapse concurrent loads so only one request recomputes a key: within one instance @Cacheable with sync true does it, but it is per JVM, so across instances I would use a short Redis lock with SET NX and an expiry, and let other requests wait briefly or serve stale data.

Third, refresh ahead: keep a soft expiry inside the value and refresh hot keys in the background before they expire, serving the stale value meanwhile, or recompute the top keys on a schedule. Fourth, protect the database with a bulkhead or rate limit on the loader path, so even a cold cache cannot overload it.

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
Redis 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 "A product catalogue cache in Redis uses a fixed TTL of 10 minutes. Every 10 minutes the...": https://scrivio-interview-library.netlify.app/databases/redis/c3fbfa0abd/