This site is not affiliated with or endorsed by Cloudflare, Inc. It showcases deployable experiments across most Cloudflare products.

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.

Person working on a laptop with notes nearby
Photo by LinkedIn Sales Solutions on Unsplash

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

ProductRoleDocs
Cache APIProgrammatic put/match in WorkersRuntime API
WorkersDecide what to cacheRuntime
Cache RulesZone-level CDN caching without codeCDN cache
Cache purgeInvalidate CDN/cache assetsPurge
R2Durable object storageObjects
KVTiny cached strings / flagsKV

Cache API vs CDN cache vs R2

ToolRoleWhen
Cache Rules / CDN cacheCache based on URL + headers for zone trafficStatic assets, HTML at the zone
Workers Cache APIProgrammatic put/match inside a WorkerComputed JSON, proxied APIs, custom keys
R2Durable blobsScreenshots, 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

  1. Only cache GET (or idempotent reads you fully control)
  2. Vary your cache key on anything that changes the body (auth, locale, query)
  3. Do not cache personalized private data under a shared key
  4. Set explicit TTLs - accidental year-long caches hurt more than MISS latency
  5. Purge strategy - know how you will invalidate (purge cache for CDN; key versioning for Cache API)
  6. Clone responses before put/return when bodies are one-shot streams

Pairing with other experiments

ScenarioPattern
Cache proxied third-party JSONEdge Cache
Durable screenshot reuseScreenshot API + R2 + optional Cache API
Validate origin still up before cachingIs It Down

Observability

  • Add X-Cache: HIT|MISS response 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

  1. Read Workers Cache API
  2. Open Edge Cache
  3. Deploy and call the same URL twice
  4. Confirm HIT on the second request
  5. Change the cache key inputs and watch MISS return
  6. Port the helper into your proxy or API Worker
  7. 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

  1. Cache only safe GETs.
  2. Include every body-affecting input in the cache key.
  3. Set explicit TTLs.
  4. Add X-Cache: HIT|MISS for debugging.
  5. Prefer Cache Rules for static zone assets.
  6. 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

Cloudflare Experiments is an independent open-source catalog - not affiliated with Cloudflare, Inc.