Quick revision
The short answer
The home page catalog key is written at 09:00 with a 24 hour TTL, so it expires at exactly 09:00 the next day, at a busy time. In that instant hundreds of concurrent requests across 30 instances miss and all run the expensive catalog query: a cache stampede. @Cacheable(sync = true) only limits it to one load per instance, which still means 30 concurrent loads. The fixes are to refresh the entry ahead of expiry on a schedule or with a refresh-after-write style policy, to serve the stale value while one caller recomputes, to add jitter to TTLs, and to use a short distributed lock or lease so one instance recomputes while the others wait briefly or serve stale data.
What to remember
In this answer, we traced the daily 09:00 CPU spike to a cache stampede: the catalog key's 24 hour TTL makes it expire at the same busy moment every day, and hundreds of concurrent misses across 30 instances all run the expensive query. sync = true reduces that to one load per instance, which is still 30. We fixed it by refreshing ahead of expiry with a single scheduled job or refresh-after-write, serving the stale value while one caller recomputes, adding jitter so expiries spread out, and using a short Redis lease so only one instance rebuilds on a miss, keeping in mind that the lease can expire early, be lost on failover or be released by the wrong holder.
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.
The home page catalog key is written at 09:00 with a 24 hour TTL, so it expires at exactly 09:00 the next day, at a busy time. In that instant hundreds of concurrent requests across 30 instances miss and all run the expensive catalog query: a cache stampede. @Cacheable(sync = true) only limits it to one load per instance, which still means 30 concurrent loads. The fixes are to refresh the entry ahead of expiry on a schedule or with a refresh-after-write style policy, to serve the stale value while one caller recomputes, to add jitter to TTLs, and to use a short distributed lock or lease so one instance recomputes while the others wait briefly or serve stale data.
Why this question is asked
A problem that happens at exactly the same time every day is a gift: it tells us the cause is a schedule, not bad luck. Here the schedule is the TTL itself. Interviewers like this question because it connects a production symptom, a daily CPU spike, with the mechanics of expiry under concurrency, and because there are several good fixes with different costs. In this answer, we will explain exactly what happens at 09:00, see why sync = true is not enough with 30 instances, and then go through refresh-ahead, serving stale data, TTL jitter and a distributed lease, before looking at how the lease itself can fail.
1What Happens at 09:00
Yesterday at 09:00 the first request after the previous expiry rebuilt the catalog and stored it in Redis with a TTL of 24 hours. So today at 09:00:00 Redis deletes it. 09:00 is also when traffic is rising, and suppose the home page is requested 2,000 times per second across the fleet. Every request that arrives before the entry is rebuilt misses. If the catalog query takes 3 seconds on an idle database, that is up to 6,000 requests starting the same query, and as they pile up the database CPU reaches 100 percent, each query takes far longer, and the miss window stretches toward a minute.
Notice that the problem renews itself. Whichever request finally completes writes a new entry at about 09:00 with a 24 hour TTL, so tomorrow the same thing happens at the same time. The TTL has locked the expiry to the busiest moment of the day.
7 messages in order. Play them, or step through them one at a time.
2Why sync = true Only Partly Helps
@Cacheable(sync = true) makes concurrent misses for the same key wait for one computation, but that coordination happens inside each JVM. With 30 instances, each one still lets one thread run the query, so the database receives 30 concurrent catalog queries instead of hundreds. That is a big improvement, and it is a one-line change worth making, but 30 copies of a heavy query may still saturate the database, and every waiting request on every instance still waits for the full query. So we need a fix that removes the expiry from the busy moment, or that keeps a value available while it is rebuilt.
3Fix 1: Refresh Ahead of Expiry
The most direct fix is to never let the hot key expire under load. A scheduled job rebuilds the catalog before it expires and overwrites the entry, so readers always hit. Only one instance should run it, which a scheduler lock such as ShedLock or a single scheduled worker provides.
The TTL stays as a safety net that is longer than the refresh interval. If the job fails a few times, the entry still expires eventually instead of being served forever. For in-process caches, Caffeine's refreshAfterWrite achieves the same idea per instance: after the refresh interval, the next read triggers a background reload while the old value is still returned.
4Fix 2: Serve Stale While One Caller Recomputes
Instead of letting the store delete the value, we keep two expiry times: a logical one stored inside the value and a longer physical TTL in Redis. After the logical expiry, the first reader that notices starts a rebuild, and everyone, including that reader if we do it asynchronously, keeps getting the old value until the new one is stored. The database sees one query, and no request waits. This is stale-while-revalidate, and it suits a home page catalog, where a value a minute old is perfectly acceptable.
5Fix 3: Add Jitter to TTLs
Jitter means adding a random amount to each TTL so expiries spread out. For this single key, jitter alone would only move the stampede to a random time each day; it helps much more when many keys were written together, for example after a cache flush or deployment, because they otherwise expire in the same second. A TTL of 24 hours plus or minus 30 minutes, or for shorter TTLs plus or minus 10 percent, is typical. It is cheap and worth having alongside the other fixes.
6Fix 4: A Short Distributed Lock or Lease
When a miss does happen, we can make sure only one instance in the whole fleet recomputes. The instance that misses tries to take a lease in Redis with SET lock:catalog <token> NX PX 30000: set only if it does not exist, with a 30 second expiry. The winner rebuilds and stores the catalog, then releases the lease. The others either wait a short time and read again, or, better, serve a stale copy if they have one.
- Read catalog from Redismiss or logically expired
- SET lock:catalog token NX PX 30000 succeeded?YesRebuild, store, release the leaseNoDo not query the database
- Stale copy available?YesServe itNoWait 100 ms and read again, a few times
- Still nothing after the wait budget?YesFall back: return a minimal page or 503NoServe the fresh value
4 steps in order. Play them, or step through them one at a time.
Combined with stale serving, the lease turns a fleet-wide miss into exactly one catalog query. On its own, it still makes other requests wait, so it works best as a backstop behind refresh-ahead.
7Follow-up questions
What are the failure modes of a distributed lock here?
If the holder crashes, the lock must expire by itself, which is why it has a PX timeout; until then nobody rebuilds, so serving stale data matters. If the timeout is shorter than the rebuild, or the holder pauses for a long garbage collection, the lease expires while it still works and a second instance starts too, so we get two queries instead of one. Releasing must check the token, with a small Lua script that deletes the key only if it still holds our token, otherwise one instance can delete another's lease. A Redis failover can lose a recently taken lock because replication is asynchronous. And if waiting is unbounded, waiters pile up. Here all of these are tolerable, because the worst outcome is a duplicate computation, not corrupted data; for correctness-critical locking we would need fencing tokens or a different tool.
Why not simply set the TTL to never expire?
Then the catalog would never be rebuilt unless something evicted it, and a change to the catalog would not appear. An eternal key plus scheduled refresh can work, but a finite TTL longer than the refresh interval is safer, because it bounds how stale the value can get if the refresh job silently stops.
8Conclusion
In this answer, we traced the daily 09:00 CPU spike to a cache stampede: the catalog key's 24 hour TTL makes it expire at the same busy moment every day, and hundreds of concurrent misses across 30 instances all run the expensive query. sync = true reduces that to one load per instance, which is still 30. We fixed it by refreshing ahead of expiry with a single scheduled job or refresh-after-write, serving the stale value while one caller recomputes, adding jitter so expiries spread out, and using a short Redis lease so only one instance rebuilds on a miss, keeping in mind that the lease can expire early, be lost on failover or be released by the wrong holder.
Open with
What is happening is a cache stampede on the catalog key. It was written at 09:00 with a 24 hour TTL, so it expires at exactly 09:00 every day, right when traffic is high.
Then, if they want more
In that instant every request on all 30 instances misses, and they all run the expensive catalog query, so the database CPU hits 100 percent, queries slow down, and the miss window stretches to about a minute. Then the rebuilt entry gets another 24 hour TTL, so it repeats tomorrow.
Adding sync = true is a quick improvement, but it only coordinates threads within a JVM, so we still get 30 concurrent loads.
The real fixes: first, refresh ahead of expiry, for example a scheduled job every few minutes, guarded by ShedLock so one instance runs it, that rebuilds and puts the catalog, with the TTL as a longer safety net.
Second, serve stale data: keep a logical expiry inside the value and a longer physical TTL, so readers get the old catalog while one caller rebuilds. Third, add jitter to TTLs so keys written together do not expire together.
Fourth, on a miss, use a short Redis lease with SET NX PX so only one instance rebuilds and the others serve stale or wait briefly. The lease has failure modes, expiry during a long rebuild, loss on failover, deleting someone else's lock, but here the worst case is a duplicate query.
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 Framework reference, Cache abstraction
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 "Every day at 09:00 the database CPU of a catalog service jumps to 100 percent for about...": https://scrivio-interview-library.netlify.app/databases/caching/8aa68fc051/