The Cache-Control header is the HTTP header that tells browsers, CDNs and proxies whether they may store a response, how long they may reuse it, and when they must check with your server again. Setting it correctly is the single most effective way to cut load times and origin traffic, and setting it wrongly is a common cause of stale pages and leaked private data.
It works in both directions: servers send it on responses, and clients can send it on requests (for example when you hard-refresh). This article focuses on the response side, which is what web developers and API authors control. It is defined in RFC 9111 (HTTP Caching).
A response is cacheable when a cache is allowed to keep a copy and answer later requests from it without contacting the origin. Cache-Control carries a comma-separated list of directives that set those rules:
There are two kinds of caches to keep in mind:
Most directives differ in how they treat these two kinds, which is why the same header can be safe for a browser and unsafe for a CDN.
| Directive | Meaning |
|---|---|
max-age=N | The response is fresh for N seconds after it was generated. |
s-maxage=N | Like max-age, but only for shared caches; it overrides max-age there. |
public | Any cache may store the response, including when the request carried an Authorization header. |
private | Only the end user's browser may store it; shared caches must not. |
no-cache | A cache may store the response but must revalidate it with the origin before every reuse. |
no-store | No cache may store any part of the response. |
must-revalidate | Once stale, the response must not be served without successful revalidation. |
immutable | The content will never change while fresh, so the browser should skip revalidation on reload. |
stale-while-revalidate=N | A stale copy may be served for up to N seconds while a fresh one is fetched in the background. |
stale-if-error=N | A stale copy may be served for up to N seconds if the origin returns an error. |
This is the most common misunderstanding. no-cache allows storage but forces revalidation (usually with an ETag or Last-Modified check that returns 304 Not Modified). If you want nothing stored at all, for example for a page with account balances, use no-store.
Responses to ordinary GET requests are cacheable by default when they carry a max-age, so public is only required to let shared caches store responses to requests that include Authorization. Do not add it by habit.
Files whose names change when the content changes (app.3f9a1c.js) can be cached for a year:
HTML references your assets, so it must update quickly. Revalidate on every use and rely on ETag to keep it cheap:
Keep them out of shared caches, and out of every cache if they are truly sensitive:
Let the CDN absorb traffic while browsers stay close to live:
In a Spring Boot controller, use CacheControl so you cannot misspell a directive:
In Node.js with Express:
You can check the result from the command line:
Freshness decides when a cache may skip the network. Validators decide how cheap the check is once a copy is stale. Send an ETag (or Last-Modified) with every cacheable response. When the cache revalidates, the browser sends If-None-Match, and the server can answer 304 Not Modified with no body. Avoid the old Expires and Pragma: no-cache headers; Cache-Control takes precedence over Expires and Pragma is only a legacy request header.
no-cache lets a cache keep the response but requires revalidation before reuse. no-store forbids keeping it at all. Use no-store for sensitive data.
Caches may apply heuristic freshness, typically a fraction of the time since Last-Modified. The behaviour is unpredictable, so always set an explicit policy.
Yes. When both are present, max-age wins, and modern caches only need Cache-Control.
Use Cache-Control: private, max-age=300. Shared caches will not store it, while the browser can.
Cache-Control sets who may cache a response and for how long. Use long max-age with immutable for fingerprinted assets, no-cache plus an ETag for HTML, private for per-user data and no-store for secrets. Add s-maxage and stale-while-revalidate when a CDN sits in front of your API, and remember that no-cache means revalidate, not "do not cache".