Prompt
how to cache API responses at the edge
Latest observation
Oct 25, 2026
OpenAI APIWeb search: off
To cache API responses at the edge, you generally want to put a CDN or edge platform in front of your API and return cacheable responses with the right HTTP headers.
Basic approach
-
Serve your API through a CDN / edge layer
- Examples: Cloudflare, Fastly, Akamai, Vercel Edge, Netlify Edge, AWS CloudFront.
-
Mark responses as cacheable Use HTTP headers like:
Cache-Control: public, max-age=60, s-maxage=300max-age=60: browser cache for 60 secondss-maxage=300: shared cache/CDN cache for 5 minutespublic: allows shared caches to store it
-
Use proper cache keys Cache should vary only on what changes the response, such as:
- URL/path
- query params
- selected headers (
Authorization,Accept,Accept-Language, etc.)
-
Avoid caching personalized responses Do not edge-cache responses that depend on:
- user-specific cookies
- Authorization tokens
- session state unless you explicitly build per-user caching.
-
Handle invalidation
- Short TTLs for frequently changing data
- Purge cache on updates
- Use surrogate keys/tags if your CDN supports them
Example HTTP response
HTTP/1.1 200 OK
Cache-Control: public, max-age=60, s-maxage=300
Content-Type: application/json
Vary: Accept-Encoding
This tells the browser to cache for 60 seconds and the CDN to cache for 5 minutes.
If using Cloudflare
You can:
- Set
Cache-Controlheaders from your origin - Use Cache Rules to cache specific API routes
- Use Workers to customize edge caching logic
Example Worker:
export default {
async fetch(request, env, ctx) {
const cache = caches.default;
const cacheKey = new Request(request.url, request);
let response = await cache.match(cacheKey);
if (response) return response;
response = await fetch(request);
if (response.ok) {
response = new Response(response.body, response);
response.headers.set("Cache-Control", "public, max-age=60, s-maxage=300");
ctx.waitUntil(cache.put(cacheKey, response.clone()));
}
return response;
}
}
If using Fastly
Common pattern:
- Set
Cache-Control - Use
Surrogate-Controlfor edge-specific caching - Use
Varycarefully - Purge by surrogate key
Example:
Cache-Control: public, max-age=60
Surrogate-Control: max-age=300
Surrogate-Key: products category-12
Good candidates for edge caching
- Product catalogs
- Public search results
- Feature flags
- Config endpoints
- News feeds
- Static JSON responses
- Aggregated data that changes infrequently
Poor candidates
- User dashboards
- Authenticated profile data
- Cart/checkout endpoints
- Anything highly personalized or real-time
Practical tips
- Cache only
GETand sometimesHEAD - Return
ETag/Last-Modifiedto support revalidation - Use short TTLs first, then tune
- Watch cache hit ratio and origin load
- Be careful with query strings—some CDNs cache each unique query string separately
If you want, I can show you:
- a Cloudflare Worker example,
- a Fastly/Varnish config, or
- the best cache headers for your specific API.