Prompt

how to cache API responses at the edge

Technology / Cloud Infrastructure1 observationLast seen Oct 25, 2026

Latest observation

Oct 25, 2026OpenAI 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

  1. Serve your API through a CDN / edge layer

    • Examples: Cloudflare, Fastly, Akamai, Vercel Edge, Netlify Edge, AWS CloudFront.
  2. Mark responses as cacheable Use HTTP headers like:

    Cache-Control: public, max-age=60, s-maxage=300
    
    • max-age=60: browser cache for 60 seconds
    • s-maxage=300: shared cache/CDN cache for 5 minutes
    • public: allows shared caches to store it
  3. 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.)
  4. 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.
  5. 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-Control headers 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-Control for edge-specific caching
  • Use Vary carefully
  • 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 GET and sometimes HEAD
  • Return ETag / Last-Modified to 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:

  1. a Cloudflare Worker example,
  2. a Fastly/Varnish config, or
  3. the best cache headers for your specific API.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.