How Web Caching and CDNs Work

Fractional CTO · Founder of Injuria.ai
The fastest request is the one you never make. Almost every technique for making the web feel quick comes back to that idea: storing a copy of something so you do not have to compute it, fetch it, or send it across the world again. Caching sounds simple, and the basic mechanics are. What makes it interesting is that it contains one of the genuinely hard problems in all of computing, and the whole system is built to manage that problem carefully.
Caching happens in layers
When your browser shows a page, the data for it may have been served from any of several caches, each closer to you than the last. There is the cache inside your own browser. There are caches run by content delivery networks spread around the world. There can be caches sitting in front of the origin server, and caches inside the application itself.[6] A request travels outward only as far as it has to before something can answer it.
The goal at every layer is the same. Avoid repeating work that has already been done, and avoid moving bytes that have already been moved. The closer the answer lives to the user, the faster it arrives.
The browser cache
The first and cheapest cache is the one on your own device.[7] After the browser downloads an image, a stylesheet, or a script, it can keep a copy and reuse it on the next page instead of downloading it again.[8] The server controls this behavior with response headers, and the main one is Cache-Control.[5][9]
Cache-Control: public, max-age=31536000, immutable
That header tells the browser it may keep the file for a year and that the file will never change, so it should not even bother revalidating, even on a reload.[4] This looks reckless until you see the trick that makes it safe. Files that can be cached forever are given names that include a hash of their contents, something like app.9f2a1c.js. When the file changes, its name changes, so the browser treats it as a brand new file and fetches it. The old cached copy is simply never requested again. This is why build tools rename your assets on every deploy.
HTML is usually handled differently, with a short or zero cache lifetime, because the HTML is what points at those hashed filenames. You want the document itself to be fresh so it can reference the latest assets.
Validation: checking without re-downloading
Not everything can be cached for a year, and for those cases there is a middle ground. Instead of blindly reusing a copy or blindly re-downloading, the browser can ask the server a cheap question: has this changed since I last saw it?
The server supports this with a validator, usually an ETag, a short fingerprint of the content.[3][9]
ETag: "a1b2c3"
On the next request, the browser sends that fingerprint back with If-None-Match. If the content has not changed, the server replies with a 304 Not Modified and no body at all. The browser reuses its stored copy, and only a few bytes crossed the network instead of the whole file. It is a small round trip that avoids a large download.
CDNs and the edge
Your origin server lives in one place. Your users do not. Physics sets a floor on how fast data can travel, and a request crossing an ocean pays for that distance on every trip. A content delivery network solves this by keeping copies of your content on servers in many locations, often called edge nodes.[10]
When a user requests a file, they are routed to the nearest edge node. If that node already has a copy, it serves it immediately, and the request never reaches your origin. If it does not, the edge node fetches it from the origin once, stores it, and serves everyone else in that region from the local copy.[11] A CDN turns one slow trip to a faraway server into many fast trips to a nearby one, and it absorbs huge amounts of traffic that would otherwise hammer your origin. Modern CDNs cache more than static files. They can cache full HTML pages and even run small amounts of code at the edge, which blurs the line between a cache and an application server.
Cache invalidation, the hard part
There is an old joke in software that there are only two hard problems, and cache invalidation is one of them. The joke survives because it is true. Storing a copy is easy. Knowing when that copy has gone stale, and getting rid of it at the right moment, is not.
The danger runs in both directions, and one direction is genuinely dangerous. Hold a cached copy too long and users see outdated prices or old articles. Clear caches too aggressively and you lose the entire benefit. But the failure that should keep you up at night is caching something personalized in a shared cache: serve one logged-in user's account page from a CDN node and the next visitor in that region gets served that user's private data. This is not hypothetical, it is one of the most common ways a caching mistake becomes a security incident, and it is why the private directive (store only in the user's own browser, never in a shared cache) exists and why the Vary header, which tells caches a response depends on specific request headers, has to be set correctly.[2] My rule is simple and absolute: anything that changes per user is private or is not cached at all, and I would rather re-fetch a thousand times than leak one account page once.
The strategies for everything else are a spectrum. Content-hashed filenames sidestep the problem entirely for assets, since a changed file is a new URL. For content that changes on its own schedule, you set a time-to-live and accept the cache may be a little behind for that window. When you need something gone right now, you issue an explicit purge to the CDN.
Stale-while-revalidate
One pattern deserves a mention because it resolves a common tension nicely. stale-while-revalidate lets a cache serve a slightly out-of-date copy immediately while it fetches a fresh one in the background.[2]
Cache-Control: max-age=60, stale-while-revalidate=600
For ten minutes after the first minute of freshness, the user gets the cached version instantly, and the cache quietly updates itself for the next visitor. Nobody waits on a slow fetch, and the content is never badly out of date. It trades a small, bounded amount of staleness for a large gain in perceived speed, which is often exactly the right deal.
The mindset
Good caching is less about any single header and more about a habit of thinking.[1] For every piece of content, ask two questions in this order: who is allowed to see it, and how often does it really change. The first question is a safety gate and comes first on purpose, because getting it wrong leaks data, while getting the second one wrong only costs freshness. Answer both honestly and the layers do the rest: assets that never change can live forever under a hashed name, personalized responses stay private or uncached, and the broad middle does well with a short lifetime and background revalidation. The web gets faster not by working harder but by carefully, and safely, doing less.
Sources (11)
- MDN Web Docs: HTTP caching
- RFC 9111: HTTP Caching
- RFC 9110: HTTP Semantics (ETag, conditional requests)
- RFC 8246: HTTP Immutable Responses
- MDN Web Docs: Cache-Control
- Daniel A. Menascé, Virgilio A. F. Almeida, Capacity Planning for Web Performance (Prentice Hall PTR, 1998)
- Peter Smith, Professional Website Performance (John Wiley & Sons, 2012)
- Stoyan Stefanov, Web Performance Daybook Volume 2 ("O'Reilly Media, Inc.", 2012)
- Chi-Hung Chi, Maarten van Steen, Craig Wills, Web Content Caching and Distribution (Springer, 2004)
- Michael Rabinovich, Oliver Spatscheck, Web Caching and Replication (Addison-Wesley Professional, 2002)
- S.V. Nagaraj, Web Caching and Its Applications (Springer Science & Business Media, 2006)