MediumCaching Strategies📖 Theory Question

What is cache invalidation and why is it considered hard?

💡

Hint

Knowing when a cached item is stale requires global coordination — the cache has no automatic knowledge that the source changed

Full Answer

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.

More Caching Strategies Questions

EasyWhat is the Cache-Control header and what are its key directives?EasyWhat is stale-while-revalidate and why is it a good default for HTML pages?EasyWhat is content-based cache busting and how does it work?EasyWhat caching strategies can a service worker implement?

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint