Video in progress

The Redis cluster was fully flushed (or a new one came up) and started empty, and the DB is collapsing under the load. How do you prepare the system for such a "cold cache" situation?

You need layers that protect the DB: a concurrency limit and load shedding in front of it, cache pre-warming, merging misses (single-flight), and bringing traffic back gradually. And you test all of this beforehand with an "empty cache" drill.

📖 Answer

If the cache hit ratio is 98%, the DB is really sized to handle 2% of the traffic. When the cache goes empty, the DB gets 50 times the load, and no DB is ready for that. This happens after a failover, after an accidental FLUSHALL, after a serialization format change (all the old keys become unreadable), or when launching a new region.

The protection layers:

  1. Limits in front of the DB: a strict connection pool size and timeouts on queries. When the pool is full, reject quickly instead of waiting a long time in the queue (a bulkhead, Resilience4j). Non-critical endpoints (recommendations, statistics) are the first to go: they return a default or empty response.

  2. Single-flight: only one load per key at a time. On a cold start this reduces the requests going to the DB to the number of keys.

  3. Warm-up: the list of the hottest keys (say, yesterday's top 10,000) is known in advance. Before the new cluster is opened to traffic, a batch job loads them from the DB slowly, with a rate limit. Sometimes restoring from an RDB snapshot is the fastest warm-up.

  4. Bringing traffic back gradually: through the load balancer or a feature flag, 5% → 25% → 100%. At each stage you wait until the hit ratio stabilizes.

  5. A two-level cache: an L1 local cache keeps part of the data even while Redis is gone.

CircuitBreaker dbBreaker = CircuitBreaker.of("product-db", CircuitBreakerConfig.custom()
        .slowCallDurationThreshold(Duration.ofMillis(500))
        .slowCallRateThreshold(50)
        .build());

The most important thing is to test this beforehand. On one project we ran an exercise on staging: "flush Redis and start a load test". The DB collapsed in 40 seconds, and it was good that we found that out before it happened on prod. As a result of the exercise, we added degradation to two endpoints and wrote a warm-up script.

One more subtle point: a deploy that changes the serialization format is effectively the same as "dropping the cache to zero". Do that too with a version in the cache key and a staged rollout.