call(back)

Prevent a Cache Stampede on a Hot Key

A read-heavy service caches database results in Redis with a time-to-live. One key is extremely hot, say the detail page of a product during a sale. The moment that key expires, every in-flight request misses the cache and queries the database for the same row, and the database falls over.

Explain why this happens and design the protections. Then distinguish this problem from two neighbours that interviewers ask about in the same breath: requests for keys that do not exist, and a large number of keys expiring together.

Hints

Hint 1

The damage is the duplication: a thousand identical queries where one would do.

Hint 2

Make rebuilding the value a job for exactly one caller. What do the others do while they wait?

Hint 3

Expiry does not have to mean the value disappears. Separate the moment a value should be refreshed from the moment it must not be used.

Asked at

Similar Problems5