Skip to content

    Frontend System Design

    CDN and Edge Caching From the Browser's Side

    See a CDN from the browser's side: read Age and cache-status headers, set edge and browser lifetimes apart, and avoid cache-key traps and stale deploys.

    By Published 12 min read

    • frontend-system-design
    • cdn
    • edge-caching
    • cache-control
    Share

    Standard lessonintermediateFrontend System Design

    Before this: HTTP Caching for the Front End

    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-Control and 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

    1. 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.
    2. 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.
    3. 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

    Browser cache, CDN edge cache and originBrowser cache to CDN edge cache (miss or stale); CDN edge cache to Origin (miss or stale)ONE USER'S DEVICESHARED CACHEBrowser cache: private, one userBrowser cacheprivate, one userCDN edge cache: shared, many usersCDN edge cacheshared, manyusersOrigin: HTML and APIOriginHTML and APImiss or stalemiss or stale

    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

    sequenceDiagram accTitle: A response served from a CDN with an Age header accDescr: The browser asks the CDN for the product page. The CDN holds a stored copy that is 240 seconds old and answers with Cache-Control max-age 600 and Age 240. The browser stores it and treats it as fresh for 360 more seconds. After that the browser asks the CDN again. participant B as Browser participant C as CDN edge B->>C: GET /api/products/1234 Note over C: stored copy is 240 s old C-->>B: 200, max-age=600, Age: 240 Note over B: fresh for 600 - 240 = 360 s more B->>C: GET again after 360 s

    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 sets Age on 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-Status has values such as HIT, MISS, EXPIRED, REVALIDATED, DYNAMIC (not eligible, no lookup) and BYPASS (eligible, but the origin response was not cacheable). Other CDNs use other names and values.
    • Cache-Control as 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?"

    flowchart TD accTitle: Finding which cache served an old response accDescr: A debugging tree. If DevTools shows the response from the disk or memory cache, the browser lifetime is the cause. If not, check the cache status header: a hit with a high Age means the edge still holds the old copy, so purge it or change the URL. A bypass or miss with old content means the origin sent it. If the URL differs by query string or host, look at the cache key. A{"Does DevTools say the response
    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-Language values like en-US,en and en,en-US mean the same but could be cached separately, and suggests language in the URL path. User-specific values, such as User-Agent or 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

    NOTE

    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

    Starting architecture: the same lifetime in the browser and at the edgeBrowser cache to CDN edge (after 1 hour); CDN edge to Origin (after 1 hour)Browser cache: keeps HTML 1 hourBrowser cachekeeps HTML 1 hourCDN edge: keeps HTML 1 hourCDN edgekeeps HTML 1 hourOrigin: public, max-age=3600Originpublic,max-age=3600after 1 hourafter 1 hour

    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

    1. Change one product's HTML headers and check Cache-Control and Age with curl -sI.
    2. Purge that URL and request it again; confirm the status header is no longer a hit.
    3. Open it in a browser that held the old copy and confirm a normal load shows the new price.
    4. 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 private from the origin for anything personal.
    • A 200 from a purge is a receipt, not a guarantee.

    Common mistakes

    • One max-age for both caches on content that changes.
    • ?v=2 cache 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-maxage and CDN-Cache-Control give the edge its own lifetime; Age is 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-revalidate and stale-if-error serve stale content by design.
    • Personal responses need private from 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.

    In this course:

    Official documentation

    Ask about this article

    Get answers grounded in this post. AI-generated — based on this article, and may be imperfect.

    Scaled AI Weekly

    Enjoyed this? Get more like it every Monday.

    Real architecture decisions, LLMOps patterns that survive production, and engineering leadership advice — from 12+ years of building at enterprise scale. Free. No spam. Unsubscribe anytime.

    Get new articles by email

    Free. We email a confirmation link first, and you can unsubscribe from any email.

    Join engineers building production AI systems

    Comments