HTTP caching lets browsers and CDNs reuse responses instead of fetching them again. It is controlled almost entirely by a few response headers. Get them right and repeat visits are instant and your servers do less work; get them wrong and users see stale pages or nothing is cached at all.
Cache-Control directives
| Directive | Meaning |
|---|---|
max-age=N | Browsers may reuse the response for N seconds |
s-maxage=N | Shared caches (CDNs) may reuse for N seconds; overrides max-age for them |
public / private | May be stored by shared caches / only by the browser |
no-cache | May be stored, but must be revalidated before each use |
no-store | Must not be stored anywhere |
immutable | Will never change during max-age; do not revalidate |
stale-while-revalidate=N | Serve stale for up to N seconds while refreshing in the background |
stale-if-error=N | Serve stale for up to N seconds if the origin errors |
Validation: ETag and Last-Modified
When a cached response expires, the browser can ask "has it changed?" with If-None-Match (ETag) or If-Modified-Since (Last-Modified). If not, the server answers 304 Not Modified with no body, which is much cheaper than resending everything.
Recipes
Versioned static assets (file names contain a content hash):
Cache-Control: public, max-age=31536000, immutable
HTML for logged-out pages (cache briefly at the CDN, refresh in the background):
Cache-Control: public, max-age=0, s-maxage=300, stale-while-revalidate=86400
Personalized pages and API responses with user data:
Cache-Control: private, no-store
Public API responses that change slowly:
Cache-Control: public, max-age=60, stale-while-revalidate=300
ETag: "v42-9f1c"
Vary
Vary tells caches which request headers change the response, such as Accept-Encoding or Accept-Language. Be careful: Vary: Cookie or Vary: User-Agent can make a CDN cache almost useless, because every variation is cached separately.
Purging
With long CDN TTLs, publish-time purges keep content fresh: when an article is updated, purge its URL (and the listing pages that show it). That gives you long cache lifetimes and immediate updates. See caching for content sites.
Worked example
A blog served HTML with Cache-Control: no-cache. Every view hit the origin, and TTFB at p75 was 900 ms for visitors far from the server. Switching logged-out HTML to s-maxage=300, stale-while-revalidate=86400 with a purge on publish made almost every view a CDN hit; real-user TTFB p75 dropped to 120 ms and LCP improved accordingly. Logged-in pages kept private, no-store.
Debugging caching
curl -sI https://example.com/ | grep -iE 'cache-control|age|etag|vary|cf-cache-status|x-cache'
The Age header and the CDN's cache status header (names vary by provider) show whether a response came from cache.
Monitoring the effect
Watch CDN hit ratio, origin request rate and real-user TTFB per page. VitalRix real user monitoring shows TTFB and LCP from real visitors, and APM shows origin load. Related: how to reduce TTFB and caching strategies.
Common mistakes
- Caching personalized responses publicly, leaking one user's page to another.
- Long max-age on un-versioned files, so users keep old CSS for days.
- no-store everywhere, giving up all caching benefits.
- Vary on headers with many values, fragmenting the cache.
Caching and APIs
API responses can be cached too, at the CDN or in the browser, when they are the same for everyone: product catalogues, public configuration, exchange rates updated every few minutes. Use short max-age values with stale-while-revalidate, ETags for cheap revalidation, and never cache responses that depend on the user's identity unless the cache key includes it. For data that must always be current, such as balances or stock at checkout, send no-store and use application-level caching with careful invalidation instead; see caching strategies.
A quick audit
Run the curl command above against your home page, a product page, a CSS file, an image and a main API endpoint. Each should match one of the recipes; anything with no Cache-Control header at all is leaving the decision to browsers and CDNs, which rarely ends well.
What is the difference between no-cache and no-store?
no-cache allows storing but requires revalidation before use; no-store forbids storing at all.
How long should I cache static assets?
With content-hashed file names, a year with immutable. Without hashes, much shorter, and prefer adding hashes.
Can I cache HTML?
Yes for content that is the same for everyone, with short CDN TTLs, stale-while-revalidate and purges on publish.
Try VitalRix free
Uptime monitoring, APM, logs, real user monitoring and status pages in one place. No credit card needed.