Resources / Hosting

Caching API responses: choosing a web cache service (CDN, proxy or app cache)

How to cache API responses: CDN and edge caching, reverse-proxy caches and in-app caches compared — with Cache-Control, cache keys and invalidation done right.

Caching is the fastest way to make an API faster and cheaper: a response served from a cache doesn't run your code, query your database or call a paid third-party API. The question is where to cache — and how to keep cached data correct.

Three places to cache

LayerExamplesBest for
CDN / edge cacheCloudFront, Cloudflare, FastlyPublic GET responses that are the same for everyone, served close to users
Reverse-proxy cacheVarnish, Nginx proxy_cacheSelf-hosted APIs that need custom cache rules in front of the app
Application cacheRedis, Memcached, a key-value store, in-memoryExpensive lookups inside your code, including per-user data

Most APIs use the edge for public responses and an application cache for everything else.

Edge caching

A CDN keeps copies of responses in data centres around the world. Repeat requests are answered in milliseconds without reaching your server at all — which also absorbs traffic spikes and reduces load. It only works for responses that don't depend on who's asking. More in edge caching for APIs.

The headers that control it

Cache-Control: public, max-age=60, s-maxage=300, stale-while-revalidate=30

The cache key

A cache stores one response per key — usually method + URL including the query string. Two rules:

Invalidation

There are two ways to keep cached data fresh:

  1. Short TTLs. Seconds to minutes. Simple and self-healing; even 30 seconds removes most load on a busy endpoint.
  2. Purge on change. When data or code changes, purge the affected keys. Precise, but you must remember to do it everywhere data changes.

Start with short TTLs and add purges where staleness actually hurts.

Application caching

For data that's expensive to compute but differs per user — or for calls to slow third-party APIs — cache inside your code with a TTL:

import { kv } from 'wisp';

export async function handler(req) {
  const city = (req.query.city ?? '').toLowerCase();
  const cached = await kv.get('weather:' + city);
  if (cached) return { status: 200, body: cached };
  const fresh = await fetchWeather(city);           // the slow, paid call
  await kv.set('weather:' + city, fresh, { ttl: 600 }); // reuse for 10 minutes
  return { status: 200, body: fresh };
}

What never to cache

Choosing a service

In Wisp

Each GET endpoint has an Edge caching setting, from 1 second to 1 day. Only successful responses of the live version are cached; requests with an API key and draft (?test) requests never are. Cached hits don't run your code or use Run credits, publishing a new version purges the cache automatically, and wisp purge clears it on demand. For per-user or computed data, use the built-in key-value store with a ttl, as in the example above.

Build it with Wisp — describe an API, get it live.

Related