Conrad caching

Cloudflare's cache

What Cloudflare caches by default and what it passes through as DYNAMIC, the CF-Cache-Status values, Cache Rules and edge TTLs, purging, and why HTML belongs at the origin when it has many variants.

From the Cloudflare caching part of your Conrad Caching note. What Cloudflare does besides caching (WAF, bots, Workers versus containers) is in Cloudflare at the edge.

Know

  • Default: Cloudflare caches responses for static file extensions only: images, scripts, stylesheets, fonts. HTML and JSON are DYNAMIC until a Cache Rule makes them eligible; headers alone never make Cloudflare cache them.
  • CF-Cache-Status: HIT, MISS, EXPIRED (lifetime over, fetched again), REVALIDATED (origin said 304, same copy, new lifetime), STALE (an expired copy was served, for example while refreshing or because the origin was unreachable), BYPASS (private, no-store, Set-Cookie or a rule), DYNAMIC (not a cacheable type).
  • Lifetimes: s-maxage or CDN-Cache-Control for the edge, max-age for the browser, or an Edge TTL set in the Cache Rule.
  • Purging: by URL, by cache tag or prefix, or everything; tags and prefixes need a rule that sets them.
  • Where it lives: each data center has its own cache (Tiered Cache adds a shared upper tier); the Cache API gives Workers access to the same per-data-center cache. Workers KV is a separate store, not the CDN cache.
  • A short shared cache on Workers: KV propagates in up to a minute, the Cache API is per data centre, a Durable Object is in one place; fine for hourly content or one region, not for a global 60-second page cache, which is why big shops keep it in Redis beside the renderers.
  • HTML at the edge: common and cheap on Cloudflare for pages that are the same for everyone, editorial and category pages for example, once a Cache Rule makes HTML eligible; pages with many variants, or personalised ones, are cached at the origin instead. Static assets go to the CDN either way.

Fact check of your notes

Gotcha· The CDN cache is not KV

"In a KV or Cloudflare Cache API (per data center)": the CDN cache is per data center, and the Cache API is how a Worker reads and writes that same cache. Workers KV is a different product, a global key-value store that a Worker uses for its own data. Nothing goes to KV unless a Worker puts it there.

Gotcha· STALE does not depend on proxy-revalidate

STALE means an expired copy was served, while a fresh one is fetched or because the origin failed. proxy-revalidate and must-revalidate are what forbid that; without them Cloudflare may serve stale in those cases.

Exercise

Exercise
  • On a site behind Cloudflare, request an image, a script, the HTML and an API call, and read CF-Cache-Status for each; explain every value.
  • Write the Cache Rule, in words, that would cache an anonymous category page for five minutes at the edge, with a cache tag per category, and say what breaks if the page depends on a cookie.

Be able to say

Be able to say· Why does our HTML show CF-Cache-Status: DYNAMIC even with a public max-age header?

"Because Cloudflare only caches static file types by default. HTML and JSON are passed to the origin as dynamic whatever the headers say. To cache them you add a Cache Rule that makes the requests eligible, and then s-maxage, CDN-Cache-Control or the rule's own TTL decides for how long. Before doing that for HTML I would check how many variants the page has, because the CDN caches by URL and a wrong variant for a logged-in user is worse than no cache."

Be able to say· How would you get a changed page out of the CDN?

"Purge it. By URL for one product, by cache tag or prefix for a whole category if a rule sets the tags, or everything as a last resort. Purging is cheap on Cloudflare, so the usual design is a short edge lifetime plus an event-driven purge when the content changes, and stale-while-revalidate so users never wait for the refresh."

Your notes

Open in Notes

Nothing yet. Notes filed here show up on this page and the section overview.