Learn / Questions

Why does a cold cache overload a database?

A 90% hit rate means the database is sized for a tenth of the reads. Flush the cache and it gets all of them at once.

A cache in front of a database does not make the database faster. It makes the database see fewer requests. With a 90% hit rate, nine reads in ten are answered from memory and one in ten reaches the database, so a system taking 2,500 reads a second sends the database about 250. Over months, the database gets sized for 250: a small instance, a 40-connection pool, enough CPU for that load and a little headroom.

A flush, a deploy that restarts the cache, or a new cache node with nothing in it sets the hit rate to zero. The database now receives all 2,500 reads a second, ten times what it was sized for. Its connection pool fills in well under a second. Queries queue behind the pool, response times climb from milliseconds to seconds, callers hit their timeouts and retry, and the retries add to the queue. The cache refills only as fast as the database answers, so the warm-up that should take seconds takes much longer, or never completes while the overload lasts.

This is why the incident looks like a database failure when nothing about the database changed. The cache was carrying load the database could never have carried alone, and the flush handed it back.

The numbers

Reads per second2,500
Cache hit rate90%
Database load with a warm cache250 reads/s
Database load with a cold cache2,500 reads/s (10×)
Connection pool40, full within a second

The fix

Give the database headroom for a cold cache (read replicas or more CPU), and stop a flush from becoming a stampede: let one request refill each missing key while the others wait or are served stale, and warm the cache before it takes traffic.

Try it: Cold cache after a deploy

  1. Run it and watch Postgres at second 12, when the deploy flushes the cache.
  2. Add a read replica ("add a read replica") and rerun the same traffic.
  3. Compare before and after: success, p99 and the monthly cost of the replica.

Opening and running a problem is free. Changes by command cost 1 credit each; you have 25 free credits a day.

Related questions