What you'll learn: By the end of this lesson you will be able to explain what sits between a browser and an origin when a CDN is in front, read
Age,Cache-Controland a vendor's cache-status header to tell a hit from a miss, give the edge and the browser different lifetimes, see why the URL is the lever you control in the cache key, choose between purging and new URLs for a deploy, and keep personalised responses out of the shared cache.
Your home bookshelf, a branch library and the central archive form a chain. You check your shelf first; if the book is not there, the branch lends you a copy stamped with the day it left the archive. A copy that sat at the branch for a month is already a month old at home, and a correction printed at the archive reaches the branch only when someone removes the old copy there. It never reaches your shelf.
A CDN is the branch, the browser cache is your shelf and the origin is the archive. This lesson is the view from the shelf: what the response tells you about the branch, what you can tell it, and what you cannot reach.
The running example is a product page at https://shop.example/products/1234 with HTML, a hashed script bundle, product images and a JSON API, all invented. Cache basics (freshness, validation, Cache-Control) are in HTTP Caching for the Front End; this lesson adds the shared layer.
The core idea
- A CDN is a second cache, shared by many users, between the browser and the origin. Responses can come from it without touching your servers.
- Two caches read one set of headers, with two clocks. What the CDN has stored for a while counts against what the browser may keep.
- The front end steers the cache key mostly through the URL. What counts as "the same request" is decided at the edge, and vendors default differently.
Figure 1. Two caches on the path
How to read this: a request travels right only when the cache on its left cannot answer. You can see the first hop in DevTools. The second hop is visible only through response headers.
Two clocks, one set of headers
RFC 9111 gives a shared cache its own lifetime. The s-maxage directive overrides max-age (and Expires) for shared caches and is ignored by private ones. private tells a shared cache not to store the response at all, while a browser may keep it. A shared cache may not store a response to a request carrying Authorization unless the response allows it (public, s-maxage or must-revalidate).
RFC 9213 adds CDN-Cache-Control, a response header aimed at CDN caches with the same directives. A cache that supports it uses it and ignores Cache-Control and Expires for that response; a cache that does not support it ignores it. Cloudflare's documentation confirms its precedence: CDN-Cache-Control: no-store gives a bypass even when Cache-Control says public, max-age=3600.
The clocks add up through Age. The header is the sender's estimate of the time since the response was generated or validated at the origin, and RFC 9111 defines it as the sum of the time the response has been resident in each cache on the path plus transit time. MDN states the consequence for browsers: if a cache stored the response for 100 seconds, the browser cache deducts 100 seconds from its freshness lifetime.
Illustration 1: the minimal example
The arithmetic, ignoring transit time. Run in Node:
const freshLeft = ({ maxAge, age }) => Math.max(0, maxAge - age);
for (const age of [0, 240, 1800]) {
console.log(`max-age=600, Age: ${String(age).padEnd(4)} -> fresh for ${freshLeft({ maxAge: 600, age })} more seconds`);
}
Output, from running it in Node 26.13.1:
max-age=600, Age: 0 -> fresh for 600 more seconds
max-age=600, Age: 240 -> fresh for 360 more seconds
max-age=600, Age: 1800 -> fresh for 0 more seconds
The last row is RFC 9213's own example: a response stored by a CDN with Age: 1800 and Cache-Control: max-age=600 is fresh for the CDN if it also carries CDN-Cache-Control: max-age=3600, but already stale for every other cache. The RFC warns that such a cache can serve responses that look prematurely stale downstream.
Figure 2. The edge's age is deducted in the browser
How to read this: the browser never sees how long the copy sat at the edge except through Age, and it subtracts it from its own lifetime.
Reading the edge from the browser
The browser cannot look inside the CDN, but the response says a lot.
Age. RFC 9111 says its presence implies the response was not generated or validated by the origin for this request, and its absence does not prove the origin was contacted. Cloudflare's documentation says it setsAgeon hits, stale and updating responses but not on misses or bypasses, so check how your vendor behaves.- A vendor status header. There is no standard one. Cloudflare's
CF-Cache-Statushas values such asHIT,MISS,EXPIRED,REVALIDATED,DYNAMIC(not eligible, no lookup) andBYPASS(eligible, but the origin response was not cacheable). Other CDNs use other names and values. Cache-Controlas received. The value in DevTools is the one the browser acts on. If it differs from what your origin sends, something in between changed it, although RFC 9111 says a proxy must pass cache directives through.
Check from a terminal, then in the Network panel:
curl -sI https://shop.example/assets/app.3f9c2e1a.js
curl -sI https://shop.example/products/1234
Look for Cache-Control, Age and your CDN's status header; a second HIT with a higher Age is the edge doing its job.
Figure 3. "Why do I still see the old version?"
came from the browser cache?"} A -->|"yes"| B["Browser lifetime: check max-age,
immutable, URL did not change"] A -->|"no"| C{"Cache status header
and Age at the edge?"} C -->|"HIT, high Age"| D["The edge holds the old copy:
purge it or change the URL"] C -->|"MISS / BYPASS / DYNAMIC"| E["The origin sent it: look behind the CDN"] C -->|"no header"| F["Check the cache key:
query string, host, Vary"]
How to read this: go from the nearest cache outward. The first question separates your own cache from the shared one.
The cache key: what the front end controls
A cache key is the information a cache uses to choose a response: at a minimum the method and target URI, plus any request headers named by Vary. RFC 9111 says a cache must not reuse a stored response without revalidation unless the headers named by Vary match, and a Vary: * response never matches.
What a CDN adds to that is its own configuration, and the defaults differ:
| Default cache key | Consequence for the front end | |
|---|---|---|
| Amazon CloudFront | Distribution domain and URL path; query strings, headers and cookies are not included unless a cache policy adds them | ?v=2 and ?v=3 reach the same cached object by default |
| Cloudflare | Scheme, host and URI with query string, plus the Origin header and some override and forwarded-host headers |
Every distinct query string is a separate entry, so tracking parameters can split one page into many |
So the same page can behave oppositely on two CDNs. Three habits survive both.
- Put identity in the path, not in a header or a query. CloudFront's guide notes that
Accept-Languagevalues likeen-US,enanden,en-USmean the same but could be cached separately, and suggests language in the URL path. User-specific values, such asUser-Agentor per-session cookies, are poor candidates for a key. - Fingerprint static files in their names (
app.3f9c2e1a.js) instead of?v=. The name changes with the bytes on every CDN, whatever its key policy. - Keep tracking parameters out of cache-critical URLs, or ask whoever runs the CDN to exclude them from the key.
Illustration 2: a realistic example
The product page, with illustrative lifetimes that suit a store whose prices change a few times a day:
| Resource | Example Cache-Control |
Reasoning |
|---|---|---|
/assets/app.3f9c2e1a.js, /img/p1234.7a8b21.webp |
public, max-age=31536000, immutable |
The URL changes with the bytes, so every cache may keep it for a year |
/products/1234 (HTML, same for everyone) |
max-age=0, s-maxage=300, purged when the price changes |
The browser always asks; the edge absorbs most asks for five minutes |
/api/products/1234 (JSON) |
max-age=30, s-maxage=60, stale-while-revalidate=30 |
Short in the browser, longer at the edge |
/api/cart (per user) |
private, no-store |
Never in a shared cache, never stored at all |
CDN-Cache-Control could carry the edge lifetime instead of s-maxage, but only where your CDN supports RFC 9213. The immutable directive is described by MDN as telling a cache the response will not be updated while fresh.
Deploys: purge or new URLs
A purge reaches the CDN, not the browser. CloudFront recommends file versioning primarily for frequently updated files and says that after an invalidation a user might keep the old version until it expires from local or corporate caches. Cloudflare documents several purge methods, recommends purge by URL, and says that a 200 from the purge API does not confirm that content was evicted; it suggests requesting the asset afterwards and checking that the status is no longer HIT.
The rule that follows: give static files a new URL, and keep the HTML that names them uncacheable in the browser (no-cache or max-age=0). Purge what cannot change name, and give it a short browser lifetime so a purge can reach everyone soon.
Serving stale on purpose
RFC 5861 (an Informational RFC) defines stale-while-revalidate, which lets a cache serve a stale response for a number of seconds while it revalidates in the background, and stale-if-error, which lets a cache use a stale response when it would otherwise return a 500, 502, 503 or 504. For a user this means content older than its max-age, by design. RFC 5861 notes that background revalidation only happens if a request arrives inside the window. MDN says the request form of stale-if-error is not supported by any browser, so the useful place for it is the edge, if your CDN honours it.
Personalised responses and the shared cache
MDN says to add private to personalised content, "especially for responses received after login," and that forgetting it can let a shared cache reuse one user's response for many. Cloudflare's documentation lists origin private or no-store as causing a bypass and says a Set-Cookie header can cause one depending on configuration, but do not rely on a CDN's rule to protect user data; send the right header from the origin.
Illustration 3: a failure
A logged-in product page sends Cache-Control: public, max-age=300 because a developer copied the policy from the anonymous page. The edge stores it and serves shopper A's greeting and saved address to shopper B. Nothing errors, and the Age header looks healthy. The cause is visible only in the origin's header and the fix is private, plus a purge of the leaked entry.
Trade-offs
| Choice | Gain | Cost |
|---|---|---|
Long s-maxage plus purge on change |
Few origin requests | A purge pipeline to build, and to verify |
| Hashed names for assets | Safe year-long caching everywhere | Old files must outlive the HTML that names them |
| Short browser lifetime for HTML | A purge reaches users soon | More requests reaching the edge |
stale-while-revalidate |
Hides revalidation latency | Users may see older content |
| Fewer values in the cache key | Higher hit ratio | A page that varies on an omitted value is served wrongly |
Build it
This lesson has no lab: a CDN is a provider's service, and nothing here can be exercised by a small deterministic program without inventing its behaviour. The single Node snippet above is plain arithmetic and was run. To see the headers on a real site you operate, run the two curl commands above against it; this lesson shows no captured response because none was taken.
The other side of this topic
The back-end view covers how a CDN is built and operated: tiers and shielding, cache keys at scale, purge models and edge compute, in CDN and Edge Networking. This lesson stays with what the browser sees and the front end controls.
In practice: a price that stays old after a purge
This is a realistic scenario, not a case history. It is a composite built from common production patterns. It describes no specific company or product, and every number in it is an illustrative assumption.
The situation
A price correction is published and the team purges the CDN, yet some shoppers see the old price for up to an hour. Support asks for a faster purge.
Starting architecture
Figure 4. One lifetime for both caches
How to read this: the purge empties the middle box, but a shopper's browser copy, shown in the warning colour, is not reachable.
Options considered
| Option | What it fixes | Cost or risk |
|---|---|---|
Shorten max-age to a few minutes |
Old prices disappear sooner | More requests, still a window |
Browser max-age=0, edge s-maxage plus purge on change |
The purge reaches everyone on their next load | The edge answers many more asks |
| Version the page URL | No stale copies | Not possible for a page people bookmark |
The decision
| Field | Decision record |
|---|---|
| Context | One long lifetime serves both caches; a purge cannot reach browsers. |
| Decision | HTML uses max-age=0, s-maxage=300. A price change purges that URL. Assets keep hashed names. |
| Consequences, good | A purge now reaches every shopper on the next load. |
| Consequences, bad | More browser requests to the edge, and a purge step that can fail. |
| Revisit when | Edge load or origin cost grows, or the CDN supports CDN-Cache-Control and the team wants to separate the headers. |
Rollout and verification
- Change one product's HTML headers and check
Cache-ControlandAgewithcurl -sI. - Purge that URL and request it again; confirm the status header is no longer a hit.
- Open it in a browser that held the old copy and confirm a normal load shows the new price.
- Roll out to the rest and watch the origin request rate.
The 2 a.m. failure
A purge call returns 200, yet the old price still shows. The page is reached through a campaign link with a tracking query string, and the CDN keys on the full URL: the purged URL is not the one shoppers loaded.
First three checks: (1) the cache status header and Age of the failing request; (2) the exact URL, query string and host in the Network panel; (3) whether the purge covers that variant. Mitigation: purge by prefix or tag where the CDN supports it, or exclude the tracking parameter from the key. Follow-up: a post-purge check that requests the real URLs and fails if it still sees a hit.
Architect's notes
- Think in two lifetimes: one for the browser you cannot reach, one for the edge you can purge.
- Know your CDN's default key before relying on query strings.
- Send
privatefrom the origin for anything personal. - A
200from a purge is a receipt, not a guarantee.
Common mistakes
- One
max-agefor both caches on content that changes. ?v=2cache busting on a CDN that ignores the query string.- Purging and expecting browsers to follow.
- A public lifetime on a logged-in page.
- Trusting the vendor's status header name across CDNs.
Key takeaways
- A CDN is a shared cache; the browser sees it only through response headers.
s-maxageandCDN-Cache-Controlgive the edge its own lifetime;Ageis deducted from the browser's.- The URL is the main part of the cache key; defaults differ between CDNs.
- A purge reaches the CDN, not the browser: change the URL for static files.
stale-while-revalidateandstale-if-errorserve stale content by design.- Personal responses need
privatefrom the origin.
Practice exercises
Exercise 1: Starter (30 min). For Cache-Control: max-age=900 and Age: 600, how long is a browser's copy fresh? Check with the snippet above.
Exercise 2: Intermediate (2 hours). Run curl -sI twice against a CDN-fronted page you operate and explain each change in Cache-Control, Age and the status header.
Exercise 3: Advanced (half day). For your own product page, list each resource, its browser and edge lifetimes and how a change reaches users. Mark every row where a purge alone is not enough.
Related reading
In this course:
- HTTP Caching for the Front End: freshness, validation and the browser cache.
- Caching Across the Stack: the layers and how their clocks stack.
- DNS, TCP, TLS and the Request Lifecycle: the cost of an extra origin for the CDN.
- Edge Rendering: running code at the edge.
- CDN and Edge Networking: the back-end view of the same layer.
Official documentation
- RFC 9111: HTTP Caching: shared caches,
Age,Vary,s-maxage,private. - RFC 9213: Targeted HTTP Cache Control:
CDN-Cache-Control. - RFC 5861: HTTP Cache-Control Extensions for Stale Content:
stale-while-revalidate,stale-if-error. - MDN: Cache-Control: directives,
Ageinterplay, cache busting. - Cloudflare: Cache responses and Cache keys: cache status values and the default key.
- Amazon CloudFront: Understand the cache key and Invalidate files: the default key and versioning.