Загрузка...

Cache stampede: one expired key took down the database | system design interview

This is a cache stampede. The key expires, and every request that misses tries to rebuild it at the same time. 20k concurrent rebuilds of the same value, all hammering the same DB rows.

The fix, in order of effort:

1. Lock the rebuild. First request that misses takes a mutex and recomputes. The other 19,999 wait 50ms and read the fresh value. One DB query instead of 20k.

2. Serve stale while revalidating. Keep the old value past its TTL, return it while one background worker refreshes.

3. Probabilistic early refresh. Hot keys refresh themselves slightly before expiry so they never actually miss.

Production gotcha: the lock itself becomes the problem. If the rebuild takes 3s and your lock TTL is 1s, the lock expires mid-rebuild and the stampede happens anyway ❌ Size the lock TTL to your worst rebuild time, not your average.

Mutex lock or stale-while-revalidate as your default?

#systemdesign #techinterview #softwareengineering #backend #redis #caching #distributedsystems #codinginterview #faang #interviewprep

Видео Cache stampede: one expired key took down the database | system design interview канала Theberner
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять