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
| Layer | Examples | Best for |
|---|---|---|
| CDN / edge cache | CloudFront, Cloudflare, Fastly | Public GET responses that are the same for everyone, served close to users |
| Reverse-proxy cache | Varnish, Nginx proxy_cache | Self-hosted APIs that need custom cache rules in front of the app |
| Application cache | Redis, Memcached, a key-value store, in-memory | Expensive 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
max-age— how long browsers and clients may reuse the response.s-maxage— how long shared caches (CDNs, proxies) may keep it; overridesmax-agefor them.stale-while-revalidate— serve the old copy briefly while fetching a new one, so nobody waits.no-store— never cache. Use it for anything personal.ETag+If-None-Match— let clients revalidate cheaply and get304 Not Modified.Vary— list request headers that change the response (for exampleAccept-Language), so different versions aren't mixed up.
The cache key
A cache stores one response per key — usually method + URL including the query string. Two rules:
- Normalise query strings.
?a=1&b=2and?b=2&a=1should ideally be the same entry; drop tracking parameters that don't change the response. - Never key on secrets. If a response depends on an API key, cookie or
Authorizationheader, it must not be in a shared cache at all.
Invalidation
There are two ways to keep cached data fresh:
- Short TTLs. Seconds to minutes. Simple and self-healing; even 30 seconds removes most load on a busy endpoint.
- 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
- Responses that depend on the caller (API keys, cookies,
Authorization) - Writes —
POST,PUT,PATCH,DELETE - Errors (or cache them for a few seconds at most)
- Anything with personal or payment data in a shared cache
Choosing a service
- Public API on a managed platform: use its built-in edge cache — nothing to run.
- Self-hosted API: a CDN in front of it, plus a reverse proxy if you need custom rules.
- Per-user or computed data: an application cache (Redis or a managed key-value store).
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.