System design fundamentals learning path Level 2 This level · 1 of 20
Medium system-design fundamentals

How do you keep a cache in sync with the database, and what is a cache stampede?

The 30 second answer

The default is cache-aside: read from the cache, on a miss read the database and fill the cache, and on a write update the database and delete the cache key. Every key also gets a TTL so a missed invalidation heals itself. A stampede is when a hot key expires and many requests miss at once and all hit the database; I prevent it with one request refilling while others wait, early refresh, and random jitter on TTLs.

Could you say this in an interview?

The patterns, with their trade-offs.

  • Cache-aside / lazy loading. The application checks the cache, and on a miss loads from the database and writes the result to the cache. AWS notes the upside is that only requested data is cached; the downside is a miss costs extra round trips, and data can be stale if nothing invalidates it.
  • Write-through. On every database write the application also writes the new value to the cache. Reads are always fresh, but you cache data nobody may read, and each write does two writes.
  • Write-around. Writes go only to the database; the cache fills on the next read. Good for data written once and rarely read soon after.
  • Write-behind (write-back). Writes go to the cache and are flushed to the database later. Fast writes, but you can lose data if the cache fails before flushing, so it suits counters and metrics more than orders.

Invalidation in cache-aside. On a write, update the database first, then delete the key rather than writing the new value into the cache. Deleting avoids a race where two concurrent writers put values into the cache in the wrong order. There is still a small window where a slow reader puts an old value back, which is why the TTL matters.

Always set a TTL. AWS's caching best practices recommend a TTL on every key except those kept current by write-through, even a long one of hours or days, because it catches application bugs where you forget to update or delete a key.

Cache stampede (thundering herd, dog piling). AWS describes it as many processes requesting the same key at once, all missing, and all running the same database query in parallel; the more expensive the query, the worse it is. A research paper on the topic notes that it can cascade: the slow database makes more requests miss, which adds even more load. Defences:

  • Single flight / lock: only one request rebuilds the value; others wait briefly or get the old value.
  • Early or probabilistic refresh: refresh a hot key shortly before it expires; the VLDB paper by Vattani and others gives an optimal probabilistic version.
  • Jitter on TTLs: AWS suggests adding randomness to TTLs so keys written together, for example after prewarming, don't all expire in the same window.
  • Serve stale while refreshing, like stale-while-revalidate in HTTP caches.

Common mistakes interviewers listen for.

  • Updating the cache before the database, so a failed database write leaves the cache lying.
  • No TTL "because we invalidate on write".
  • Caching without measuring hit rate; a cache with a low hit rate just adds latency and another failure point.

Redis-specific uses and hot keys are covered on Redis beyond caching.

Sources (3)
  1. AWS: Caching best practicesaws.amazon.com
  2. AWS ElastiCache: Caching strategiesdocs.aws.amazon.com
  3. Vattani, Chierichetti and Lowenstein: Optimal Probabilistic Cache Stampede Prevention (VLDB 2015)vldb.org
Previous
Next