This week in caching: a playbook to stop a cache stampede
When a hot key expires, 10k requests hit your DB at once. Here's the fix order.
— Add a lock on miss — first request to find an empty key takes a short mutex, recomputes, and writes; the rest wait or serve stale. This is the core move.
— Use probabilistic early expiration — recompute a key slightly before TTL with rising probability (XFetch algorithm) so it never expires under everyone simultaneously.
— Serve stale during recompute — keep the old value readable while one worker rebuilds; nobody sees a miss.
— Jitter your TTLs — if you cache 500 keys at deploy with identical TTLs, they all expire together; add random ±10%.
— Cap recompute concurrency — a semaphore on the expensive query is your backstop.
Credit to the XFetch paper (Vattani et al.) for probabilistic early expiration.
Bookmark: Redis docs on the lock-on-miss pattern — the canonical recipe.
Cache Catch
@CacheCatch
This week in caching: a playbook to stop a cache stampede
Этот пост опубликован в Telegram-канале Cache Catch. Подписаться можно по ссылке: @CacheCatch.