Freshness: how long a copy can be reused
When a response arrives, a cache asks one question: may I store this, and for how long can I reuse it without asking the server? The server answers with the Cache-ControlCache-ControlThe HTTP header that tells browsers, apps and CDNs whether a response may be stored and how long it stays fresh.Open in glossary → header:
| Header | Meaning |
|---|---|
Cache-Control: max-age=3600 | Fresh for 3600 seconds. Reuse it without any request during that time. |
Cache-Control: no-cache | You may store it, but check with the server every time before using it. |
Cache-Control: no-store | Don’t store it at all. Use for sensitive data. |
Cache-Control: private | Only the user’s own browser or app may store it, not a shared cache like a CDN. |
Cache-Control: public, max-age=31536000, immutable | Anyone may cache it for a year, and it will never change. |
While a stored response is fresh, the cache answers from its copy and no request leaves the device. When it becomes stale, the cache doesn’t throw it away: it asks the server whether the copy is still good, which the next part covers.
The names are confusing, so remember this pair: no-cache means “revalidate before use”, and no-store means “don’t keep it”. If a response contains a bank balance or a medical record, no-store is the one you want.
One more surprise: a response with no caching headers at all can still be cached. If it has a Last-Modified date, browsers and many clients guess a lifetime, commonly 10% of the time since the last modification. A file last changed 10 days ago may be reused for about a day without asking. So every response should say what it wants explicitly.
- Freshness lifetime, in priority order:
s-maxage(shared caches only),max-age, thenExpires(an absolute date, sensitive to clock skew), then heuristic freshness (RFC 9111 suggests up to 10% of the age sinceLast-Modified) for cacheable status codes like 200, 301 and 404. Age is the response’s currentAgeheader plus time spent in this cache. no-cachestores and always revalidates;no-storeforbids storage (memory too, ideally);must-revalidateforbids serving stale copies even when offline or when the origin is down;privateexcludes shared caches;immutabletells browsers not to revalidate on reload.- Request directives matter too: a browser reload sends
Cache-Control: max-age=0(revalidate), a hard reload sendsno-cache;fetch(url, { cache: 'no-store' })and similar options in client libraries override the default for one request. - What’s cacheable: GET and HEAD by default; POST responses only with explicit freshness, and in practice nobody caches them. Responses with
Authorizationare not stored by shared caches unlesspublic,s-maxageormust-revalidatesay so. - Browsers have several layers: memory cache, HTTP disk cache, service worker caches (programmable, ignore HTTP headers unless your code checks them), and the back/forward cache, which keeps the whole page in memory and is blocked by
Cache-Control: no-storeon the document in some browsers. - Clients outside browsers:
URLCacheon iOS (on by default forURLSession.shared, size-limited), OkHttp’sCacheon Android (off until you configure one), and no HTTP cache at all in Java’sHttpClientor Node’sfetch.
Predict the output
The server sends the response below, with no Cache-Control and no Expires. The page fetches the same URL twice, a few seconds apart. How many requests reach the server?
HTTP/1.1 200 OK
Content-Type: text/plain
Last-Modified: Thu, 05 Nov 2026 09:12:00 GMT
t=1794778959425With no explicit lifetime, the browser applies heuristic freshness: a fraction of the time since Last-Modified, about a day here. The second fetch is answered from the cache and never reaches the server, which is exactly how a 'deployed but users still see the old file' bug starts. Send Cache-Control on every response so nothing is left to a guess.
# build output with a content hash in the name: cache forever
GET /_astro/app.8d2f1c.js
Cache-Control: public, max-age=31536000, immutable
# the HTML that references those files: always check, so a release shows up at once
GET /orders
Cache-Control: no-cache
# an API response that is the same for everyone and may be a minute old
GET /api/products?category=shoes
Cache-Control: public, max-age=60
# one user's data: their device may keep it briefly, nobody else may
GET /api/me
Cache-Control: private, max-age=30
# sensitive: never written to disk
GET /api/account/statements/2026-10
Cache-Control: no-storeThe header that sounds right
Tap the line with the bug.
app.get('/api/statements/:month', requireLogin, async (req, res) => {
const statement = await loadStatement(req.user.id, req.params.month);
// statements must never be cached
res.set('Cache-Control', 'no-cache');
res.json(statement);
});Hint: Read the table again: what does no-cache allow a cache to do?
no-cache doesn’t mean “don’t cache”. It allows the browser (and any cache in between, since private is missing) to store the statement; it only requires a check with the server before reuse. The statement ends up in the disk cache of a shared computer, in a backup, or on a corporate proxy.
The directive that keeps it out of every cache is no-store:
res.set('Cache-Control', 'no-store');Use no-cache for things that may be stored but must always be fresh, like an HTML page, and no-store for anything that shouldn’t exist outside the server and the screen.
Deep dive"We deployed, but users still see the old version"Interview favorite
A classic support ticket, and the question behind it is “which cache is holding the old copy, and who told it to?”. Work through the layers from the user outward:
- The HTML document. If
index.htmlhasmax-age=3600, or no header at all (heuristic caching), browsers keep showing the old page, which references the old JavaScript files. Fix:Cache-Control: no-cacheon HTML, so every visit revalidates, which is cheap with an ETag. - Assets without versioned names.
/app.jswith a longmax-agecan’t be updated: the browser won’t ask. Fix: content-hashed names (app.8d2f1c.js, which every modern bundler produces) withmax-age=31536000, immutable, so a new release simply references new URLs. - The CDN. It may have its own TTL from
s-maxageor from dashboard rules that override the origin’s headers. Fix: purge the HTML on deploy, or keep its edge TTL short. - A service worker. It serves from its own cache regardless of HTTP headers, and a new worker version waits until all tabs using the old one close. Fix: a deliberate update strategy (prompt the user, or
skipWaitingplus reload), and never cache the service worker script itself for long. - Mobile apps don’t have this problem for their own UI, but their API responses can: an
URLCacheor OkHttp cache honouring a generousmax-ageserves an old feature-flag config. Fix: short lifetimes on config, or validators.
The general rule to state: long-lived caching only for URLs whose content never changes, short or revalidated caching for the entry points that point to them. Mention the opposite failure too: a site that sends max-age=0, must-revalidate on hashed assets makes every visit revalidate every file, wasting a round trip each, when a year-long immutable would cost nothing.
- Lifetime:
s-maxage(shared) >max-age>Expires> heuristic (~10% of age sinceLast-Modified). Always send Cache-Control. no-cache= store, revalidate every use;no-store= never store;must-revalidate= no stale serving;private= no shared caches;immutable= no reload revalidation.- Hashed assets: a year,
immutable. HTML entry points:no-cache. Sensitive data:no-store. - Clients: iOS
URLCacheon by default, OkHttp cache off until configured, JavaHttpClientand Nodefetchhave none. Service workers ignore HTTP headers.
Cache-Control: public, max-age=31536000, immutable # /assets/app.8d2f1c.js
Cache-Control: no-cache # /index.html
Cache-Control: no-store # /api/statements/...Check this part
Which header keeps a response out of every cache?
A response has no Cache-Control header but has Last-Modified a month ago. Can the browser reuse it without asking?
Hashed asset URLs get max-age=0, must-revalidate. What is the cost?
Which HTTP client caches responses by default?