Hot keys
0-day streakReview

Your progress

Level 1

0 XP100 XP to level 2
0day streak
0best streak
0topics started
0cards reviewed
    Review cards

    Progress is stored only in this browser. No account is required.

    Hot keys, cache stampede and invalidation: the 60-second story

    Hot keys

    One samosa tray empties, and forty guests rush the kitchen.

    The cook fries one batch at a time, so the kitchen jams. Smarter, send one waiter, serve cold samosas meanwhile, refill early.

    400k/s

    reads on one key, a live IPL score

    12 million apps poll every 30 s. One node manages about 100,000 ops a second, and more shards don’t help. They spread keys, not requests.

    Give the hot key fewer reads and more owners.

    A 1-second in-process cache in each of 200 pods cuts Redis to about 200 reads a second. Split the key into 8 copies on different shards.

    A key read 20,000 times a second just expired.

    Recomputing takes 200 ms, so 4,000 identical queries hit the database. They slow it down, which lets even more requests miss.

    The key expired. Who recomputes?

    Bad: Every miss

    4,000 identical queries, and a slower database lets in more.

    Good: One caller, the rest get stale

    A lock or single-flight, plus soft and hard TTLs, so nobody waits.

    XFetch lets each read refresh a little early at random, likelier as expiry nears.

    The trick

    The first refresh starts about 1.7 s early, about 2.7 recomputes instead of 4,000, and no miss.

    Stampede, avalanche, penetration. Name them precisely.

    One hot key missing; many keys expiring at once, so jitter TTLs; or keys that exist nowhere, so cache the absence behind a Bloom filter.

    Invalidation at scale: reach every copy

    1. One change event; every layer subscribes
    2. Near caches: broadcast, or a 1–5 s TTL
    3. Tags: one purge clears every dependent page
    4. Namespace bump: one INCR invalidates millions

    Remember

    Spread a hot key with an L1 and copies. Let one caller recompute, serve stale and refresh early. Invalidate with one event per change, fanned out to every copy, and jitter every TTL.