CDNs and Edge Caching
You'll learn to
- -Explain with numbers why distance, not only server speed, makes a site feel slow
- -Describe how a CDN serves from a nearby edge, and what a cache hit and a cache miss are
- -Decide what is safe to put on a CDN and what must stay off it
- -Explain why updating a cached file is hard, and the two standard fixes
Tip: the highlighter is on - just select any text below to mark it. Use the highlighter button up top to change color or turn it off. Highlights are saved on this device only. Log in to save them to your account instead.
Your servers can be fast and your code perfect, and a user on the other side of the world will still wait. The reason is not computing; it is distance. Information cannot travel faster than light, and in practice, through undersea fiber and a chain of routers, it travels a good deal slower. A Content Delivery Network, or CDN, attacks that problem directly: instead of making every user travel to your one server, it puts copies of your content near the users.
The Problem: Distance Is a Tax on Every Round Trip
Take a server in Virginia, on the US east coast, and a user in Mumbai. The cable route between them is on the order of 17,000 kilometers. Light in fiber covers roughly 200,000 kilometers per second, so even in a perfect world one trip takes about 85 ms, and a round trip about 170 ms. Real round trips on that route are typically 200 to 250 ms once routers and detours are counted.
That would be tolerable if a page load needed one trip, but it needs many. Remember the last two chapters: a first visit to an HTTPS site costs a round trip for TCP, one for TLS and one for the request, so roughly three round trips before the first byte arrives. At 250 ms each that is 750 ms of pure waiting before your server has done anything, and then a real page fetches dozens of images, scripts and stylesheets. No amount of server tuning can remove a delay that the speed of light imposes.
An Analogy: The Local Library Branch
Suppose the only copy of every book is in one national library in Virginia. Everyone, everywhere, has to travel there to borrow anything. That is a single origin server. Now the library opens a branch in every city. Each branch keeps copies of the books people in that city actually ask for. When you ask for a popular book, the branch hands it over in minutes. When you ask for a rare one it does not have, the branch orders it from the national library, gives it to you, and keeps a copy on its shelf for the next reader.
- -The national library is the origin: your real server, the one source of truth.
- -A branch is an edge server, in a location called a point of presence (PoP). Large CDNs run hundreds of them around the world.
- -A book on the branch shelf is a cache hit: served from nearby, fast, and the origin never hears about it.
- -A book the branch has to order is a cache miss: the edge fetches it from the origin, serves it, and keeps a copy, so only the first reader in each city pays the long trip.
- -Sending you to the nearest branch is done with DNS or routing tricks: the CDN's answer depends on where you are, which is the geo-routing idea from the DNS chapter.
The two pictures below show the same user, without and then with a branch library nearby.
A user in Mumbai pays the full round-trip cost to Virginia on every single request.
Most requests never leave the region; only the rare cache miss pays the long round trip.
How Much Does It Actually Help?
Two numbers describe a CDN's value. The first is latency. If the edge is in Mumbai, the round trip drops from about 250 ms to about 20 ms, so those three round trips cost 60 ms instead of 750 ms. The second is the hit ratio, the share of requests served from the edge without bothering the origin. At a 95% hit ratio, an origin that would have faced 2,000 requests per second now sees only 100. The CDN has become a shield: your servers are protected from the traffic, and a spike such as a big launch or a viral post is mostly absorbed by the edge.
What to Put on a CDN, and What to Keep Off
A CDN can only safely cache something that is the same for everyone who asks. That makes the split fairly clear:
- -Safe: images, videos, fonts, stylesheets, JavaScript bundles, downloads, and public pages or API responses that are identical for every visitor.
- -Unsafe: anything personal or private, such as a logged-in user's account page, their basket, or any response that contains their data. If the edge cached it, the next visitor could be served someone else's page.
Your server tells the CDN what it may do through a Cache-Control header on each response. In plain words: max-age=3600 means anyone may reuse this for an hour; public means a shared cache such as a CDN may store it; and no-store means never keep a copy at all. The header is the contract between your origin and every cache between you and the user.
The Hard Part: Changing Something Already Cached
Back to the library. You publish a corrected edition of a book, but every branch still has the old edition on its shelf and will keep lending it out until told otherwise. This is cache invalidation, famously one of the hard problems in computing. For a CDN it means that a file you updated can keep being served in its old version, from hundreds of locations, until the cached copy expires. There are two standard fixes, and they work in opposite ways:
- -Purge: tell the CDN to throw the old copy away. Most providers offer an API call for this, and it is the right tool for something that must change right now, like a pricing page. The cost is that it is an operation you have to remember to run, and it takes from a moment to about a minute to reach every location.
- -Version the filename: never change a file, create a new one. A script is deployed as app.3f9c1a.js, and the next release becomes app.b72e40.js, where the part in the middle is a fingerprint of the contents. The page now points at a name no cache has ever seen, so nothing stale can be served, and the old file can safely be cached for a year. This is how most large sites ship their scripts and images.
A rule of thumb: version anything that changes rarely and is cached for a long time, like bundles and images; purge, or use a short max-age, for things that change in place, like a homepage or a price.
In one line: a CDN beats distance by keeping copies near users. It is fast on a hit, costs one long trip on a miss, is safe only for content that is the same for everyone, and stays fresh by purging or by versioning filenames.
Go Deeper
CDNs cache your content at 300+ edge locations worldwide so users get sub-50ms responses instead of 500ms round trips to your origin.
Your warehouse is in Dallas. Shipping a package from Dallas to Tokyo takes 14 days. So you pre-position inventory in Tokyo, Singapore, London, and São Paulo. Now Tokyo customers get 1-day delivery from your regional warehouse. A CDN is the same idea for bits. Your origin server is the warehouse, PoPs (Points of Presence) are regional warehouses.
A CDN is a geographically distributed network of servers (called edge nodes or PoPs - Points of Presence). When a user requests an asset, DNS routes them to the nearest edge node. If the edge has the content in cache (a HIT), it serves immediately. If not (a MISS), it fetches from your origin, caches, and serves.
What to cache: Static assets - images, CSS, JavaScript, videos, PDFs, font files. Also API responses with appropriate Cache-Control headers (public, max-age=3600). Do NOT cache: authenticated responses, dynamic personalized content, session data.
Cache-Control headers are the contract between your server and the CDN: Cache-Control: public, max-age=31536000, immutable = cache forever (for content-hashed assets like bundle.abc123.js). Cache-Control: no-store = never cache. Cache-Control: public, s-maxage=0, stale-while-revalidate=86400 = CDN revalidates in background while serving stale.
Cache invalidation: When you update an asset, you need to purge the CDN cache or the old version will be served until TTL expires. Solutions: (1) Content-hash filenames (bundle.abc123.js → bundle.def456.js on change - never need to invalidate), (2) API calls to CDN purge endpoint (Cloudflare: instant, CloudFront: ~60s).
CDN as DDoS protection: A CDN absorbs volumetric DDoS attacks at the edge, far from your origin. Cloudflare has processed 71 million HTTP requests/second during DDoS events without impacting origin servers. Your 2-box origin could never survive that.
- -Use content-hashed filenames for all static assets (built by Vite/webpack automatically).
- -Set immutable on versioned assets - browsers and CDNs will never re-check them.
- -S3 + CloudFront is the standard AWS static hosting pattern.
- -CDN Geography: Cloudflare 300+ PoPs, CloudFront 400+ PoPs, Fastly 60+ PoPs.
- -HTTP/2 multiplexing and HTTP/3 (QUIC) reduce connection overhead - CDNs support these.
You just deployed a new version of your logo image, same filename. Users still see the old one. Why, and how do you fix it?
"I'd tell the CDN to clear its cache."
"Manually purging works but doesn't scale and is easy to forget. Better: serve versioned, content-hashed filenames (logo.a1b2c3.png), set Cache-Control: immutable on them, and update the reference to the new filename on deploy. The old file simply ages out of cache naturally; there's nothing to invalidate because nothing ever changes at a given URL."
What problem does a CDN primarily solve?
You now have every piece needed for Level 1: The Static App, a Client, DNS, and a CDN serving assets. Go place them on the canvas and see the request flow you just read about become something you can actually run.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!