Skip to content

Lesson 5 of 9 · 21m

Cache Keys, Purges, and Invalidation Discipline

The cache key is the contract. Learn what belongs in it, how purges really behave, and how to deploy without cache chaos.

Every cache bug is a cache-key bug wearing a trench coat. Either two different objects collapse into one entry, or one object fragments into thousands of useless entries. Cloudflare gives you control over the key; use it sparingly and deliberately.

The default key already says a lot

At a minimum, the cache identity includes the scheme/host and the full URI, including query string, plus relevant Vary dimensions. That means these are different cache entries:

/products?sort=new
/products?sort=price
/products?utm_source=newsletter

Yes, that last one hurts. Marketing parameters fragment the cache unless you explicitly ignore them. Cloudflare Cache Rules can ignore or sort query strings, but do it only for endpoints where ordering and irrelevant parameters genuinely do not affect the response.

Custom keys are powerful and dangerous

Workers can control keys through the Cache API:

const cache = caches.default;
const key = new Request(`${url.origin}/products?sort=${sort}`, request);
const hit = await cache.match(key);
if (hit) return hit;

const response = await fetch(request);
if (response.ok) ctx.waitUntil(cache.put(key, response.clone()));
return response;

This is normalization: strip tracking parameters before constructing the key. But never drop dimensions that change content — locale, currency, device type, experiment bucket — unless you enjoy serving the wrong language to the wrong country.

Purging is a scalpel with a lag

Cloudflare supports purging by URL, tag, prefix, host, or everything. Cache tags are the operationally mature option when your origin can label responses:

Cache-Tag: product:4821,collection:shoes,storefront:eu

Then a product update can purge product:4821 without nuking the entire site. A full purge is easy and emotionally satisfying, and it also converts your next traffic spike into an origin stress test.

Purges propagate quickly, but "quickly" is not a consistency guarantee. During an incident, assume stale copies may exist briefly in some places and design the user-visible behavior accordingly.

Deploy discipline beats heroic purging

The stable pattern is boring:

  1. Fingerprint static assets (app.8f31c2.js) and cache them nearly forever.
  2. Give HTML short TTLs or revalidation so navigation picks up new asset URLs quickly.
  3. Purge tags for content that changed, not paths you hope changed.
  4. Never require a purge for correctness if a normal header can express freshness.

Field notes

  • Ignoring all query strings is not "optimization"; it is content corruption waiting for the first filtered view.
  • Cache authenticated responses only when the key includes the correct authorization dimension. If you do not know exactly what that dimension is, do not cache.
  • Test purge automation in staging. A malformed purge call fails quietly until the deploy where it matters.
  • stale-while-revalidate is often a better answer than a shorter TTL: users get speed while the edge refreshes in the background.

Was this lesson helpful?

Sign in to rate