Cache invalidation is the process of removing or marking stale cached entries when the underlying data changes. Phil Karlton famously said: "There are only two hard things in Computer Science: cache invalidation and naming things."
Why it's hard:
- No automatic notification — the cache doesn't know the origin changed. You must explicitly tell it.
- Distributed state — content may be cached in browsers (worldwide), CDN edge nodes (hundreds of PoPs), and application caches simultaneously. Purging all of them atomically is difficult.
- Dependency chains — a product page cache depends on the product, its price, its stock level, and the user's locale. Any of those changing should invalidate the page, but tracking all dependencies is complex.
- Stale reads — during the window between a data change and cache invalidation, some users see stale data.
Common patterns to manage it:
- Content-hashed URLs — invalides automatically when content changes (no explicit purge needed).
- Short TTLs — accept stale windows rather than trying to purge proactively.
- CDN purge API — Cloudflare, Fastly provide tag-based purging (purge all edges caching a given tag).
- Cache-Control:
no-cache + ETag — always revalidate but serve from cache on 304.