Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

After a Redis cluster restart, the database behind a product service became overloaded and the service went down. Explain what happened and how to prevent it.

When Redis restarted, the cache came back empty, so every request missed and went to the database at the same moment. That is a cache stampede: the database was sized for the few percent of requests that normally miss, not for all of them. We prevent it by coalescing loads so only one request per key loads from the database, for example with Caffeine's loading cache or @Cacheable(sync = true) per instance, adding jitter to TTLs so keys do not expire together, protecting the database with limits on concurrent loads and load shedding, and warming critical keys before taking traffic after a restart.

Why this question is asked

A cache makes a service fast, but it also hides how much load the database would get without it. With a 98 percent hit ratio, the database sees 2 percent of the traffic; when the cache is suddenly empty, it sees 100 percent, fifty times its normal load. Interviewers ask this debugging question to see whether we understand that a cache is part of the capacity plan, not just an optimisation. In this answer, we will reconstruct the incident, then go through four protections: load coalescing, TTL jitter, protecting the database, and warming.

1Reconstructing What Happened

Before the restart, the product service handled 20,000 requests per second with a 98 percent hit ratio, so the database answered about 400 queries per second, comfortably. Redis restarted without persistence, or with persistence that took time to reload, and came back empty.

Now every request missed. The first request for product 42 went to the database, and so did the next 300 requests for product 42 that arrived before the first one finished and filled the cache. The database suddenly received thousands of queries per second, many of them identical. Queries slowed down, so each one held its connection longer, the connection pools filled, request threads waited, and the service stopped answering health checks. Because the database was too slow to fill the cache, the cache never warmed up, and the service stayed down.

Step throughA cache stampede after Redis comes back empty
RequestsProduct serviceRedisDatabase
300 requests for product 42
GET product::42 (x300)
miss (x300), cache empty
300 identical SELECTs
overloaded: every product at once
slow answers, pools full
timeouts, health checks fail

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

Without coalescing, every concurrent miss for the same key becomes its own database query.

2Fix 1: Coalesce Loads per Key

The most important fix is that concurrent misses for the same key should wait for a single load instead of all going to the database. This is called request coalescing, or single flight.

Within one instance, Spring's @Cacheable(sync = true) makes other threads wait while one thread computes the value for a key. Caffeine's loading cache does the same natively: get(key, loader) runs the loader once per key, and other callers for that key wait for the result.

Java
@Cacheable(cacheNames = "products", key = "#id", sync = true) // one load per key per instance
public ProductView find(long id) { return repository.loadView(id); }
// Or Caffeine directly, as a local layer in front of Redis
LoadingCache<Long, ProductView> local = Caffeine.newBuilder()
.maximumSize(20_000)
.expireAfterWrite(Duration.ofSeconds(30))
.build(id -> loadFromRedisOrDb(id)); // concurrent gets for 42 share one load
// 300 concurrent requests for 42 on one pod -> 1 load

Notice the limit: this coalesces per instance. With 40 instances, a key can still be loaded 40 times, which is far better than one load per request. If that is still too much, a short lock in Redis per key, taken before loading, coalesces across instances, at the cost of more complexity and of handling a lock holder that dies.

3Fix 2: Add Jitter to TTLs

A restart empties everything at once, but a similar stampede happens every day without one: if we warmed the cache at 9:00 and every entry has a TTL of exactly 1 hour, a large share of keys expire together at 10:00. Adding jitter, a small random variation to each entry's TTL, spreads expiry over time.

Java
// With Caffeine: each entry lives 50 to 70 minutes instead of exactly 60
Caffeine.newBuilder().expireAfter(new Expiry<Long, ProductView>() {
public long expireAfterCreate(Long k, ProductView v, long now) {
long minutes = 50 + ThreadLocalRandom.current().nextLong(21); // 50..70
return TimeUnit.MINUTES.toNanos(minutes);
}
public long expireAfterUpdate(Long k, ProductView v, long now, long cur) { return expireAfterCreate(k, v, now); }
public long expireAfterRead(Long k, ProductView v, long now, long cur) { return cur; }
}).build(this::loadFromRedisOrDb);
// For Redis, set each key's TTL with jitter when writing it (recent Spring Data Redis
// versions accept a TTL function per entry; check your version).

4Fix 3: Protect the Database

Even with coalescing, a cold cache means a lot of distinct keys loading at once. The database must be protected so that it keeps answering some requests well instead of all requests badly. We cap the number of concurrent cache loads per instance, for example with a semaphore or a bulkhead from Resilience4j, so at most 20 loads run at once per pod. Requests beyond that either wait briefly or are shed: they get a fast 503 or a degraded response, such as a simplified product page, instead of queueing until they time out.

The connection pool itself is a limit too, and a small one is a good thing here: it caps what each pod can send to the database. Short query timeouts make sure slow queries do not hold connections forever.

Java
private final Semaphore loads = new Semaphore(20); // at most 20 DB loads per pod
ProductView loadFromDb(long id) throws InterruptedException {
if (!loads.tryAcquire(50, TimeUnit.MILLISECONDS)) {
throw new TooBusyException(); // 503 quickly, client may retry later
}
try { return repository.loadView(id); }
finally { loads.release(); }
}

5Fix 4: Warm Critical Keys Before Taking Traffic

Finally, we avoid sending full traffic to an empty cache. After a Redis restart, a warm-up job loads the hottest keys, for example the top 50,000 products by traffic yesterday, at a controlled rate before traffic is sent at full strength; during warm-up, rate limiting at the edge or gradually shifting traffic keeps the load manageable.

We also make restarts less empty. Redis persistence, snapshots or an append-only file, lets a restarted node reload its data, and a replicated setup with failover means a replica with a full copy takes over instead of an empty node. The runbook for planned Redis maintenance then includes warming as an explicit step.

Step throughRecovering safely after Redis restarts
  1. Redis node has its data (replica or persistence)?
    YesFail over, cache stays warmNoTreat the cache as cold
  2. Limit incoming trafficedge rate limit or gradual shift
  3. Warm the top keys at a controlled ratefor example 50,000 products
  4. Coalesced loads and DB load limit stay onprotect against the rest
  5. Restore full trafficwatch DB CPU and hit ratio

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

6Follow-up questions

Why did the service not recover on its own once Redis was back?

Because the overloaded database was too slow to fill the cache. Each query took longer, requests timed out before their results were stored, and the cache stayed empty. Breaking that loop needs load shedding or traffic limits, so some requests succeed and fill the cache.

Is stale-while-revalidate useful here?

For expiry-driven stampedes, yes: serving the old value while one request refreshes it in the background means expired hot keys never cause a burst of misses. Caffeine supports this with refreshAfterWrite. It does not help after a restart that lost the data, because there is no stale value to serve.

How do you test that the protections work?

In a load test, run at peak traffic and flush the cache, or restart the Redis node, and watch database CPU, connection pool pending counts and error rates. The service should degrade briefly and recover within minutes, rather than collapse.

7Conclusion

In this answer, we saw that the outage was a cache stampede: after the Redis restart the cache was empty, every request missed, and the database received many times its normal load, including many identical queries, so it never got the chance to refill the cache. We prevent it by coalescing loads per key with @Cacheable(sync = true) or a Caffeine loading cache, adding jitter to TTLs, protecting the database with limits on concurrent loads and load shedding, and warming critical keys, ideally from a replica or persistence, before full traffic returns.

What to say in the interviewAbout 80 seconds aloud

Open with

This was a cache stampede. With a 98 percent hit ratio the database only saw about two percent of the traffic, so it was sized for that. When Redis came back empty, every request missed at once, and because nothing coalesced the misses, hundreds of concurrent requests for the same product each sent the same query.

Then, if they want more

The database slowed, pools filled, health checks failed, and since the database was too slow to fill the cache, it never recovered on its own.

To prevent it, first I would coalesce loads per key: Cacheable with sync true, or a Caffeine loading cache, so concurrent misses on a pod wait for one load; that is per instance, so forty pods means at most forty loads per key, and a Redis lock can coalesce across pods if needed.

Second, jitter on TTLs so keys do not all expire together. Third, protect the database: a semaphore or bulkhead capping concurrent loads per pod, a small pool, short query timeouts and fast 503s or degraded pages beyond the limit, so some requests succeed and refill the cache.

Fourth, warm the hottest keys at a controlled rate before restoring full traffic, and use Redis replication or persistence so a restart is rarely cold.

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 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 "After a Redis cluster restart, the database behind a product service became overloaded...": https://scrivio-interview-library.netlify.app/spring-boot/scalability/80fa2abef9/