Cache-Control patterns that survive production
Cache-Control is one header with a dozen directives, and most teams settle on a single value copied between projects. The result is either a cache that never hits or a private response served to the wrong user. Three patterns cover almost every API response.
The directives that matter
public allows shared caches to store the response. private restricts it to the browser. max-age sets freshness in seconds for every cache. s-maxage overrides max-age for shared caches, so a CDN can hold a response longer than a browser. no-store forbids storage entirely, and must-revalidate forbids serving stale after expiry.
| Response | Header | Why |
|---|---|---|
| Public catalogue | public, max-age=60, s-maxage=600, stale-while-revalidate=60 | CDN absorbs traffic, browsers stay fresh |
| User profile | private, max-age=0, must-revalidate | Never shared, always checked |
| Static JSON config | public, max-age=31536000, immutable | Versioned URL never changes |
| Payment result | no-store | Must not remain on any disk |
stale-while-revalidate is the quiet win
Adding stale-while-revalidate=60 lets a cache serve a slightly stale copy while it refreshes in the background. Users get an immediate response, the origin sees one refresh instead of a burst of misses, and freshness never lags by more than the window plus the refresh time. For catalogue and listing endpoints this single directive often removes the worst latency spikes at cache expiry.
What no-store actually costs
no-store is correct for payment data and anything sensitive, but it is frequently applied to whole APIs out of caution. Every request then travels to the origin, every byte is billed, and the CDN does nothing but add a hop. If the reason for no-store is "we are not sure", the better fix is to make the response genuinely private with private, no-cache, which still requires revalidation but allows storage.
Cache keys and Vary
A cached response is only correct if the cache key includes everything that changes it. If the response varies by language, add Vary: Accept-Language; if it varies by encoding, add Vary: Accept-Encoding. A missing Vary header is one of the most common causes of a user receiving another user's language, or a compressed body delivered to a client that asked for identity.
Finally, version what you cache. A URL with a content hash or a schema version can be cached for a year with immutable; a URL without one should be short-lived. Mixing the two is how teams end up with a "cache invalidation problem" that is really a URL design problem.
Testing that a cache actually caches
Header changes are easy to deploy and easy to get wrong. Verify with the response itself: request the same URL twice and compare the Age header, the cache status header your CDN provides, and the timing difference. If the second request is as slow as the first and the cache status says miss, the header is not doing what you think, often because a Set-Cookie or an Authorization header is disqualifying the response.
Check the behaviour at expiry too. A response with max-age=60 should be served from cache for a minute and revalidated afterwards, producing a 304 if nothing changed. A 200 with a full body on every request means the validator is missing or the client is not sending it, and both are fixable by adding ETag or Last-Modified.
- Look for Set-Cookie on responses you expect to cache publicly.
- Confirm 304 revalidation, not just the first cache hit.
- Test with a real CDN, not only with a local proxy.
One header per response class
Write the policy down as a short table and apply it in code, not by memory. Public catalogue, user profile, static asset and payment result cover most cases, and any new endpoint should be classified before it ships. That single habit prevents the most common failure: a new endpoint that copies a neighbour's header without inheriting its reasoning, then quietly caches something it should not.
Open the calculators and put these numbers to work on your own API.
Open API BenchFrequently asked questions
What is the difference between no-cache and no-store?
no-cache allows storage but requires revalidation before every use. no-store forbids storage entirely. no-cache is usually what teams mean when they write no-store for ordinary API responses.
Does s-maxage affect browsers?
No. It applies to shared caches such as a CDN. Browsers follow max-age. Setting s-maxage longer than max-age is the normal way to let the CDN hold a response while browsers revalidate.
When is immutable safe?
Only when the URL changes whenever the content changes, typically through a content hash or version segment. On a stable URL, immutable prevents revalidation and users keep stale data indefinitely.
Sources
Related guides
- Capacity planning with Little's LawTurn a target request rate and three latency percentiles into the concurrency, threads and instances an API actually needs.
- Tail latency: why P95 and P99 decide your timeoutAverages hide the requests that hurt. How to read percentiles, calculate what they cost, and set timeouts that do not cut off healthy traffic.
- Retry storms: how a 1% error becomes 300% loadRetries are new traffic. Calculate the amplification, recognise the cliff, and design retry budgets that fail politely.
- REST vs GraphQL: measuring the payload differenceOver-fetching, under-fetching and query text all show up in the bytes. A practical way to compare the transfer volume of both designs.