Why edge caching
Origin servers are slow because they're far from your users. A user in Tokyo hitting an origin in Virginia eats 200ms just on network round trip. Edge caching keeps a copy of your response in 300+ data centres globally, so the same user hits a Tokyo edge node and gets a 20ms response. For static content, this is a 90% TTFB cut for free.
What to cache
Cache aggressively: HTML for anonymous users, API responses for catalog data, images, fonts, CSS, JS. Don't cache: authenticated content, personalised data, real-time inventory. The hard cases are in the middle — product pages with stock counts, article pages with view counts. For those, cache the shell and load the dynamic bits separately.
Cache headers: the foundation
Set Cache-Control headers on every response. public, max-age=3600 for content that can be cached for an hour. public, s-maxage=86400, stale-while-revalidate=604800 for content that should be cached at the edge for a day but kept stale-served for a week while the edge re-fetches in the background. Get the headers right and Cloudflare's default caching handles 80% of your needs without code.
Cloudflare Cache API
For more control, use the Cache API in a Worker. Read from cache.match() before making the origin request. Write to cache.put() after. The Worker can transform requests and responses before caching — strip query parameters that don't affect the response, normalise headers, add custom cache keys. This is where edge caching becomes a real performance lever.
Cache invalidation
There are two hard problems in computer science. Cache invalidation is one of them. Use cache tags — Cloudflare lets you tag cached responses and purge by tag. When a product changes, purge the tag 'product-12345' and every cached page referencing that product clears instantly. Without tags, you're either over-purging (defeating the cache) or under-purging (serving stale content).
Stale-while-revalidate
The killer feature of modern HTTP caching. The browser or edge serves the stale cached response immediately, then re-fetches in the background to update the cache. The user gets a fast response now, and the next user gets fresh content. For content that changes hourly but is visited every second, this is the single biggest performance lever you have.
Measuring cache hit rate
Cloudflare reports cache hit rate per zone. Aim for 90%+ for static assets, 70%+ for HTML. If your hit rate is lower, audit your cache headers — usually a few high-traffic URLs are bypassing the cache because of an accidental no-cache header or a varying query parameter. Fix those and your origin load drops by half.
The pitfalls
Don't cache authenticated content at the edge — you'll leak data between users. Always include Vary: Cookie or skip caching when a session cookie is present. Don't cache responses with Set-Cookie — same risk. Don't trust user input in cache keys — it's a DoS vector. Edge caching is powerful and unforgiving; ship it carefully.