- Published on
HTTP Caching Headers Explained
- Authors

- Name
- Amit Bisht
Contents
Introduction
Caching is one of the highest-leverage performance tools on the web, and HTTP already has a rich, standardized protocol for it built right into every browser, reverse proxy, and CDN. Yet a few subtle misunderstandings—like thinking Cache-Control: no-cache disables caching, or forgetting Vary on compressed responses—regularly cause stale pages, broken deployments, or zero CDN cache hits.
HTTP caching (standardized in RFC 9111) comes down to two questions:
- Freshness: Can the cache reuse a stored response right now without contacting the origin server?
- Validation: If the stored copy is stale, has it actually changed on the origin?
This post walks through every major caching header, how browsers and CDNs interpret them together, and copy-paste header recipes you can use in production.
Private vs. Shared Caches
Before looking at headers, it helps to separate the two places a response can be cached:
| Cache Type | Examples | Who Sees It |
|---|---|---|
| Private cache | Browser memory/disk cache | A single user on one device |
| Shared cache | CDN (Cloudflare, CloudFront), reverse proxy (Nginx, Varnish) | Many users behind the same edge/proxy |
Many caching bugs happen when a header intended for a user's browser accidentally gets applied to a shared CDN, or vice versa. Several HTTP headers exist specifically to separate the two.
The Caching Decision Flow
When a client or proxy makes a GET or HEAD request, a cache follows this lifecycle:
flowchart TD
Req["GET /styles.css"] --> Lookup{"Stored response matching URL + Vary?"}
Lookup -- No --> Fetch["Fetch from origin"]Freshness Headers: How Long Can a Copy Be Reused?
Cache-Control (The Primary Header)
Cache-Control is the main HTTP/1.1+ header for defining caching policy. Multiple directives are comma-separated, and it can appear on both responses (server policy) and requests (client overrides).
Response Directives
| Directive | Meaning |
|---|---|
max-age=<seconds> | Freshness lifetime in seconds for all caches (private and shared). |
s-maxage=<seconds> | Overrides max-age (and Expires) only for shared caches like CDNs. |
public | Any cache may store the response—even if it would normally avoid doing so (for example, requests carrying an Authorization header). |
private | Only a private cache (the browser) may store the response. Shared caches must not store it. |
no-cache | May be stored in a cache, but must be revalidated with the origin before every reuse. |
no-store | Never store the request or response in any cache (memory or disk). Use for sensitive data. |
must-revalidate | Once stale, a cache must never serve the response without successful origin validation—even if the origin is unreachable. |
proxy-revalidate | Same as must-revalidate, but applies only to shared caches. |
immutable | Tells browsers the resource will never change while fresh, skipping conditional revalidation even on manual page reload. |
stale-while-revalidate=<sec> | Allows serving a stale response immediately for up to <sec> seconds while refreshing it asynchronously in the background. |
stale-if-error=<sec> | Allows serving a stale response for up to <sec> seconds if the origin returns a 5xx error or times out. |
The Biggest Myth: no-cache vs. no-store
Cache-Control: no-cache does not mean "do not cache." It means "you may cache this, but check with the origin first before using it."
- Use
no-cachewhen you want fast304 Not Modifiedrevalidation (e.g.,index.htmlor an evolving API resource paired with anETag). - Use
no-storewhen the payload must never touch a disk or shared cache (e.g., auth tokens, banking balances, PII).
Request Directives
Clients and browsers can also send Cache-Control on outgoing requests:
max-age=0orno-cache: Forces caches along the path to revalidate with the origin (this is what happens when you hit reload in a browser).no-store: Asks caches not to store the request or response.only-if-cached: Tells the cache to return a stored copy or fail with504 Gateway Timeoutwithout contacting the origin.min-fresh=<seconds>/max-stale[=<seconds>]: Client tolerances for remaining freshness or staleness.
Date and Age
Freshness is not measured from when your browser downloaded a file—it is measured from when the origin generated it.
Date: Timestamp set by the origin when the response was created.Age: Number of seconds the response has already sat in a shared cache (such as a CDN edge).
Suppose an origin sends:
HTTP/1.1 200 OK
Date: Fri, 27 Nov 2026 16:00:00 GMT
Cache-Control: public, max-age=3600
If a CDN serves that cached object 20 minutes later, it adds Age: 1200. A browser receiving it sees max-age=3600 and Age: 1200, so the response only has 2400 seconds (40 minutes) of remaining freshness left—not a full hour.
Expires (Legacy HTTP/1.0)
Expires specifies an absolute date/time after which the response is stale:
Expires: Fri, 27 Nov 2026 18:00:00 GMT
If Cache-Control contains max-age or s-maxage, Expires is completely ignored. Modern applications rely on Cache-Control and only include Expires if compatibility with ancient HTTP/1.0 proxies matters.
Validation Headers: Conditional Requests and 304 Not Modified
When a cached response is stale (or marked no-cache), the client doesn't necessarily need to download the whole file again. If the origin provided a validator, the cache sends a conditional request.
How Conditional Requests Work
sequenceDiagram
participant Browser
participant Origin
Browser->>Origin: GET /api/stats
Origin-->>Browser: 200 OK (ETag "v1", body)
Note over Browser: Cache entry expires or has no-cache
Browser->>Origin: GET /api/stats (If-None-Match "v1")
Origin-->>Browser: 304 Not Modified (no body)
Note over Browser,Origin: Resource changes on server ("v2")
Browser->>Origin: GET /api/stats (If-None-Match "v1")
Origin-->>Browser: 200 OK (ETag "v2", new body)1. Initial response from origin:
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-cache
ETag: "33a64df551425fcc55e4d42a148795d9"
Last-Modified: Fri, 27 Nov 2026 15:30:00 GMT
{"posts": 42}
2. Subsequent request once cached:
GET /api/stats HTTP/1.1
Host: api.example.com
If-None-Match: "33a64df551425fcc55e4d42a148795d9"
If-Modified-Since: Fri, 27 Nov 2026 15:30:00 GMT
3. Origin response if nothing changed:
HTTP/1.1 304 Not Modified
Cache-Control: no-cache
ETag: "33a64df551425fcc55e4d42a148795d9"
Notice there is no response body on a 304. The cache updates its metadata (like Date and Cache-Control) and reuses the stored body.
ETag and If-None-Match
An ETag (Entity Tag) is an opaque identifier representing a specific version of a resource—typically a content hash or version token.
- Strong ETag (
"abc123"): Guarantees byte-for-byte identity. Required for HTTP range requests (Range/206 Partial Content). - Weak ETag (
W/"abc123"): Indicates semantic equivalence, not exact byte identity. CDNs and servers (like Nginx) often downgrade a strong ETag to a weak one when applying on-the-flygziporbrotlicompression because the compressed bytes differ from the uncompressed file.
Last-Modified and If-Modified-Since
Last-Modified is a timestamp indicating when the resource was last changed. It has two main limitations:
- 1-second granularity: Multiple edits within the same second cannot be distinguished.
- Timestamp-based: Touching a file or rebuilding an identical artifact changes the timestamp even if the bytes didn't change.
If both If-None-Match (ETag) and If-Modified-Since (Last-Modified) are present in a request, If-None-Match takes precedence per RFC 9111.
Write preconditions: The inverse headers—
If-MatchandIf-Unmodified-Since—are used onPUT,PATCH, orDELETErequests to implement optimistic concurrency control. If the resource changed since you read it, the server rejects the write with412 Precondition Failed.
Cache Keys and Multi-Tier Caching
Vary: Controlling the Cache Key
By default, a cache keys stored responses by HTTP method + target URL. Sometimes, however, the same URL returns different representations depending on request headers—most commonly compression (Accept-Encoding), language (Accept-Language), or CORS (Origin).
Vary tells caches which request headers must also match before reusing a cached response:
Vary: Accept-Encoding, Origin
With Vary: Accept-Encoding, a CDN keeps separate cached entries for Accept-Encoding: br, Accept-Encoding: gzip, and uncompressed clients, preventing a Brotli-compressed file from being served to a client that only understands gzip.
Common Vary pitfalls:
Vary: *: Treats every request as unique and effectively disables shared caching.Vary: User-AgentorVary: Cookie: Explodes cache cardinality because there are thousands of distinctUser-Agentstrings and unique user cookies, crushing CDN hit rates. Normalize headers at the edge (e.g.,device-type: mobile | desktop) instead.
CDN-Cache-Control (RFC 9213) and Surrogate-Control
When you want a CDN to cache aggressively while keeping browser TTL short (so you can purge the CDN cleanly on deploy), s-maxage works—but it still leaks into any intermediate proxy and ties directives together.
RFC 9213 introduced targeted cache control headers so origins can speak directly to CDNs without affecting browsers:
Cache-Control: public, max-age=60, must-revalidate
CDN-Cache-Control: max-age=86400, stale-while-revalidate=3600
- Browser: Caches for
60seconds, then revalidates. - CDN: Caches for
86400seconds (1 day), serves stale while revalidating for another hour, and strips/ignoresCDN-Cache-Controlfor downstream browsers.
Many CDNs also support vendor-specific variants (e.g., Cloudflare-CDN-Cache-Control) and the older Surrogate-Control header (used by Fastly/Varnish).
flowchart LR
Browser["Browser Cache (max-age=60)"] -->|"Cache miss after 60s"| CDN["CDN Edge Cache (CDN-Cache-Control max-age=86400)"]
CDN -->|"Cache hit for 24h (adds Age header)"| Browser
CDN -->|"Miss or background revalidate (stale-while-revalidate=3600)"| Origin["Origin Server"]Cache Invalidation and Legacy Headers
Why You Can't "Purge" User Browsers
CDNs provide purge APIs to evict content from edge servers, but you have no way to reach into a user's browser and delete a fresh cached file. If you ship styles.css with max-age=31536000 and deploy a new styles.css five minutes later, returning visitors will keep using the old cached file for up to a year.
That is why static asset pipelines use content hashing (cache busting):
- Immutable assets get a hash in the filename (
app.9f8e7d6c.js) andCache-Control: public, max-age=31536000, immutable. - The HTML entrypoint (
index.html) keeps the stable URL and usesCache-Control: no-cache+ETag, so browsers always verify whether a new HTML file references a new JS/CSS hash.
Clear-Site-Data
When you genuinely need a browser to wipe cached data for your origin (for example, on user logout), the server can send:
Clear-Site-Data: "cache"
Deprecated / Obsolete Headers
Pragma: no-cache: An HTTP/1.0 request header. It was never reliably standardized for responses—always useCache-Control.Warning: Obsoleted in RFC 9111; modern caches should not emit it.
Production Recipes (Cheat Sheet)
1. Fingerprinted static assets (JS, CSS, fonts, hashed images)
Cache-Control: public, max-age=31536000, immutable
Cached for 1 year in browsers and CDNs; no revalidation even on refresh because the filename changes when content changes.
2. HTML documents and unhashed entrypoints
Cache-Control: no-cache
ETag: "v2026-11-27-1"
Always checks with the origin/CDN, receiving a fast 304 Not Modified unless a new deploy changed the HTML.
3. Public pages or APIs behind a CDN with fast deploys
Cache-Control: public, max-age=60
CDN-Cache-Control: max-age=86400, stale-while-revalidate=3600
Vary: Accept-Encoding
Browsers refresh every minute; the CDN absorbs traffic for a full day and can be purged immediately on publish.
4. Authenticated, user-specific responses
Cache-Control: private, no-cache
ETag: "user-42-rev-9"
Stored only in the user's browser (never on a CDN) and revalidated before reuse.
5. Sensitive responses (tokens, billing, PII)
Cache-Control: no-store
Never written to disk or stored in any shared cache.
Debugging Caching Headers
You can inspect caching behavior quickly from the terminal with curl:
# Check response caching headers
curl -sI https://example.com/assets/app.9f8e7d6c.js
# Test conditional validation with an ETag
curl -i -H 'If-None-Match: "33a64df551425fcc55e4d42a148795d9"' https://example.com/api/stats
In browser DevTools (Network tab):
200 (memory cache)/200 (disk cache): Served locally without touching the network (fresh).304 Not Modified: Revalidated with the origin or CDN edge (stale, but unchanged).Age+ vendor headers (likecf-cache-status: HIT,x-cache: Hit from cloudfront): Confirm whether a shared CDN served the response or forwarded it to your origin.- Watch out for "Disable cache" checked in DevTools—when enabled, the browser automatically injects
Cache-Control: no-cacheandPragma: no-cacheon every request, masking your real caching behavior.