System Design Intermediate
02 Caching
The fastest query is the one you never run. A cache buys you latency and shields the database -- and it charges you back in staleness, invalidation bugs, and brand-new failure modes.
Flash sale: the cached entry for one hot product expires at peak traffic, and 20,000 concurrent requests all miss together and hit the database, which melts. Name this failure, explain why plain TTL expiry causes it, and give two distinct mitigations, saying briefly how each works.
Hints
- The failure has a name: stampede, also called dogpile. Why do ALL the misses reach the database at the same instant?
- Mitigation family one: arrange for only ONE request to rebuild the value while the rest wait or are served something else.
- Mitigation family two: never let a hot key hard-expire -- serve stale while refreshing in the background, or spread expiries so they cannot align.