Blog18 min read
Cache HTTP responses at the edge with the Cache API
Deep Workers Cache API guide - caches.default, cache keys, TTLs, vs CDN Cache Rules vs R2, HIT/MISS debugging, and the Edge Cache experiment.
Not every response needs a fresh origin fetch. The Workers Cache API (caches.default) lets you store Response objects at the edge with explicit cache keys - perfect for expensive GETs that are safe to reuse.
The Edge Cache experiment demonstrates HIT/MISS behavior so you can see caching work before wiring it into production routes.
Cloudflare products in this pattern
| Product | Role | Docs |
|---|---|---|
| Cache API | Programmatic put/match in Workers | Runtime API |
| Workers | Decide what to cache | Runtime |
| Cache Rules | Zone-level CDN caching without code | CDN cache |
| Cache purge | Invalidate CDN/cache assets | Purge |
| R2 | Durable object storage | Objects |
| KV | Tiny cached strings / flags | KV |
Cache API vs CDN cache vs R2
| Tool | Role | When |
|---|---|---|
| Cache Rules / CDN cache | Cache based on URL + headers for zone traffic | Static assets, HTML at the zone |
| Workers Cache API | Programmatic put/match inside a Worker | Computed JSON, proxied APIs, custom keys |
| R2 | Durable blobs | Screenshots, PDFs, long-lived artifacts |
Use the Cache API when your Worker decides what to cache. Use Cache Rules when the zone can express the policy without custom code. Use R2 when you need durable objects, not a short TTL HTTP cache.
See also: How Cache works on Cloudflare.
Minimal pattern
const cache = caches.default;
const cacheKey = new Request(url, { method: "GET" });
let response = await cache.match(cacheKey);
if (response) {
return new Response(response.body, response);
}
const origin = await fetch(url);
response = new Response(origin.body, origin);
response.headers.set("Cache-Control", "s-maxage=60");
ctx.waitUntil(cache.put(cacheKey, response.clone()));
return response;Details and caveats: Cache · Workers. The experiment surfaces HIT/MISS clearly so you can debug keys and TTLs without guessing.
Rules that prevent footguns
- Only cache GET (or idempotent reads you fully control)
- Vary your cache key on anything that changes the body (auth, locale, query)
- Do not cache personalized private data under a shared key
- Set explicit TTLs - accidental year-long caches hurt more than MISS latency
- Purge strategy - know how you will invalidate (purge cache for CDN; key versioning for Cache API)
- Clone responses before put/return when bodies are one-shot streams
Pairing with other experiments
| Scenario | Pattern |
|---|---|
| Cache proxied third-party JSON | Edge Cache |
| Durable screenshot reuse | Screenshot API + R2 + optional Cache API |
| Validate origin still up before caching | Is It Down |
Observability
- Add
X-Cache: HIT|MISSresponse headers in your Worker for browser DevTools - Log cache key hash + colo from
request.cf - Alert on sudden MISS storms after deploys (often key-format changes)
Deploy Edge Cache
- Read Workers Cache API
- Open Edge Cache
- Deploy and call the same URL twice
- Confirm HIT on the second request
- Change the cache key inputs and watch MISS return
- Port the helper into your proxy or API Worker
- Use Cache Rules for pure static assets at the zone when code is unnecessary
Caching is one of the highest-leverage edge features - this experiment makes HIT/MISS observable so you can ship it with confidence on Cloudflare’s Cache API.
Production checklist
- Cache only safe GETs.
- Include every body-affecting input in the cache key.
- Set explicit TTLs.
- Add
X-Cache: HIT|MISSfor debugging. - Prefer Cache Rules for static zone assets.
- Use R2 for durable artifacts, Cache API for short TTL HTTP responses.
FAQ
Why am I always missing?
Key mismatch after deploys is the usual culprit - log the key hash. Also confirm you clone() before put.
Related experiments
These deployable references implement the patterns in this article. Open the docs for API details, then Deploy or clone the source.
- Edge Cache - Fetch and cache URLs with the Workers Cache API at the edge - Cache API reference pattern for Cloudflare Workers
- R2 Storage API - Upload and serve files with Cloudflare R2 - private and public bucket patterns, object CRUD, and edge file storage reference
- Is It Down? - Check if a website is reachable from Cloudflare's edge network - uptime and latency probe API for any URL
- Screenshot API - Capture website screenshots with Cloudflare Browser Rendering - full-page or viewport PNG/JPEG screenshot API