A CDN puts copies of your website on servers near your users, so pages load from a nearby city instead of from your data centre.
Light in a fibre cable travels about 200 kilometres per millisecond. That sounds fast, until you put a server in Frankfurt and a user in Sydney. The cable route is at least 16,000 kilometres, so a single round trip takes well over 150 milliseconds before any server has done any work. Real routes are longer, and 250 milliseconds or more is common. A new HTTPS connection needs several round trips, and a page needs dozens of files. The user waits, and no amount of bandwidth helps.
A content delivery network (CDN) solves this by moving the content instead of the user. It runs servers in hundreds of cities, close to where people are, and answers as many requests as possible right there. Only what cannot be answered locally travels to your own server, the origin.
This article explains how a request finds the nearest CDN server and what happens there. It shows how caching really works, what "the edge" means today, and how CDNs protect websites and sometimes break them. It ends with how to set up your own site so a CDN can do its job. It continues the networking series after Latency Is the New Bandwidth, DNS in Depth and HTTP/1.1 vs HTTP/2 vs HTTP/3.
1. Why Distance Still Matters
Every connection pays for distance several times. Here is what loading a page from a server 16,000 km away costs, with an optimistic 160 ms per round trip:
| Step | Round trips | Time |
|---|---|---|
| DNS lookup (not cached) | 1 | 160 ms |
| TCP handshake | 1 | 160 ms |
| TLS 1.3 handshake | 1 | 160 ms |
| HTML request | 1 | 160 ms |
| CSS, JavaScript, images (after the HTML) | 1–3 | 160–480 ms |
| Total before the page is usable | 5–7 | 0.8–1.1 s |
Now the same page from a CDN server 20 km away, with 2 ms per round trip: about 10 to 15 milliseconds for the same steps. The server does not need to be faster. It only needs to be closer.
The Latency article explains why this matters more than bandwidth. A CDN is the most direct way to act on it: it cuts the distance for every round trip at once.
2. A Short History
| Year | Event |
|---|---|
| 1998 | Akamai is founded by MIT researchers to fix the "World Wide Wait"; the first large commercial CDN |
| 1999–2000s | CDNs carry downloads, software updates and video; large sites build their own (Google, later Netflix Open Connect) |
| 2010 | Cloudflare launches with a free plan, putting a CDN and DDoS protection in front of small websites |
| 2011 | Fastly is founded around instant purging and programmable caching |
| 2017 | Edge computing goes mainstream: Cloudflare Workers and AWS Lambda@Edge run code in CDN locations |
| 2019 | A faulty WAF rule takes down much of Cloudflare's network for about 30 minutes |
| 2021 | A Fastly configuration bug takes large news sites, shops and government sites offline for about an hour |
| 2022 | HTTP/3 is published as RFC 9114; CDNs are among the first to enable it widely |
| 2025 | A malformed configuration file causes Cloudflare's worst outage since 2019, lasting several hours |
Two trends run through this history. CDNs moved from "big files for big companies" to the default front door for websites of every size. And as more of the web runs through a few providers, their failures have become events that people notice worldwide.
3. How a Request Finds the Nearest Edge
A CDN has points of presence (PoPs): data centres in many cities, each with many servers. The question is how a user's browser ends up at the right one. Two methods are used, often together.
DNS-based routing
The website's name points to the CDN, usually with a CNAME record:
www.example.com. CNAME www.example.com.cdn-provider.net.
When a resolver asks for that name, the CDN's DNS servers answer with the IP address of a PoP near the resolver. They choose it from the resolver's location, the PoP's current load and measured network performance.
The weakness: the CDN sees the resolver, not the user. A user in Munich who uses a public resolver may be sent to wherever that resolver appears to be. The EDNS Client Subnet extension lets resolvers pass a shortened part of the user's address, so the CDN can choose better, at some cost to privacy. The DNS article explains resolvers in detail.
Anycast
With anycast, the CDN announces the same IP address from all its PoPs at once. The internet's routing system (BGP) then delivers each packet to the "nearest" location, measured in network hops and routing policy, not kilometres.
┌──► PoP Frankfurt (announces 203.0.113.10)
User in Berlin ── BGP ─┤
├──► PoP Amsterdam (announces 203.0.113.10)
└──► PoP Sydney (announces 203.0.113.10)
→ routed to Frankfurt, the shortest network path
Anycast has two big advantages. Routing happens automatically, without relying on the resolver's location. And when a PoP fails or is overloaded, it stops announcing the address, and traffic usually flows to the next one within seconds. That is also why anycast networks absorb DDoS attacks well: the attack traffic is spread over the whole network instead of hitting one place.
4. What Happens at the Edge
Once the request reaches a PoP, the edge server does much more than look up a cached file:
- It ends the TLS connection. The expensive handshake happens 20 km from the user instead of 16,000 km. The certificate for your domain lives at the edge (see How HTTPS Really Works).
- It speaks the newest protocols. HTTP/2 and HTTP/3 to the browser, even if your origin only speaks HTTP/1.1.
- It answers from the cache when it can: images, scripts, styles, and often whole HTML pages.
- It compresses text with Brotli or gzip, and can resize and convert images for each device.
- It filters attacks: DDoS traffic, known exploit patterns (web application firewall) and abusive bots.
- It keeps warm connections to your origin, so even uncached requests skip the slow handshakes on the long part of the route.
The last point is often overlooked. Even a page that can never be cached usually gets faster with a CDN. The user's slow handshakes happen with a nearby PoP, and the PoP sends the request to the origin over a connection that is already open.
5. Caching: The Rules That Decide Everything
A CDN only helps as much as it is allowed to cache. Your origin decides that with HTTP headers. The caching article covers caching in general; here are the parts that matter for CDNs.
Cache-Control
The most important header. Some typical values:
| Header | Meaning |
|---|---|
Cache-Control: public, max-age=31536000, immutable | Cache for a year, everywhere, never check again. For versioned files like app.3f9a2c.js |
Cache-Control: public, max-age=60, s-maxage=300 | Browsers keep it 60 s, shared caches like the CDN 300 s |
Cache-Control: public, max-age=0, s-maxage=60, stale-while-revalidate=600 | The CDN serves it for 60 s, then may serve the old copy for up to 10 minutes while it fetches a new one in the background |
Cache-Control: stale-if-error=86400 | If the origin fails, the CDN may serve an old copy for up to a day |
Cache-Control: private, no-cache | Only the browser may store it, and must check with the server first. For personal pages |
Cache-Control: no-store | Never store it anywhere. For sensitive data |
s-maxage applies only to shared caches such as CDNs and proxies. A newer header, CDN-Cache-Control (RFC 9213), goes one step further: it applies only to CDNs, so you can give the CDN different rules without touching what browsers do.
stale-while-revalidate is one of the most useful directives. Users always get a fast answer from the cache, and the CDN refreshes the copy in the background. Only the first request after a long quiet period pays for the trip to the origin.
The cache key
The CDN stores each response under a cache key, usually the scheme, host, path and query string. Two requests with the same key get the same cached response. This is where many problems start:
- Too specific: tracking parameters like
?utm_source=newslettercreate a separate cache entry for each campaign, and the hit ratio drops. Most CDNs can ignore or sort query parameters. - Too general: if the response depends on something that is not in the key, such as a cookie or a language header, users get each other's versions. In the worst case, a logged-in page is served to a stranger.
The Vary header tells caches which request headers change the response, for example Vary: Accept-Encoding for compressed and uncompressed versions. Use it carefully: Vary: Cookie or Vary: User-Agent can make every request unique, and the cache becomes useless.
Revalidation
When a cached copy expires, the CDN does not always download it again. It asks the origin "has this changed?" and sends along the version tag it got with the copy (the ETag, in an If-None-Match header) or the copy's date (If-Modified-Since). If nothing changed, the origin answers 304 Not Modified without a body, and the CDN keeps using its copy for another period. That saves bandwidth and origin work.
Hit ratio
The cache hit ratio is the share of requests answered from the cache. It is the most important number for a CDN setup. For static files, aim for well above 90%. A low hit ratio usually means short lifetimes, a cache key that is too specific, or headers like Set-Cookie and Vary that make responses uncacheable.
6. Updating Content: Purging and Versioned File Names
If the CDN keeps a copy for a day, how do you publish a change before then? Two methods, and good setups use both.
Versioned file names for static assets. Build tools put a hash of the content into the file name: app.3f9a2c.js. A new version gets a new name, the HTML points to it, and the old file can stay cached forever. This is why such files can safely use max-age=31536000, immutable.
Purging for content that keeps its address, like an article page. The origin tells the CDN "throw away /blog/42", by URL or by a tag that groups related pages (for example blog-42, which covers the article, the list page and the feed). Modern CDNs purge worldwide within seconds.
Article edited in the admin area
→ origin saves it
→ origin calls the CDN API: purge tags "blog-42", "blog-list"
→ next request at each PoP fetches the new version
The combination is simple to remember: hashed names for files that change by being replaced, purge for pages that change in place.
7. Protecting the Origin: Tiers, Shields and Collapsing
A CDN with 300 PoPs can, in the worst case, send 300 requests for the same file to your origin: one from each PoP whose copy expired. Three techniques prevent that:
- Tiered caching / origin shield. PoPs do not go to the origin directly. They ask a few larger "parent" PoPs, and only those talk to the origin. The origin sees a handful of requests instead of hundreds.
- Request collapsing. When 1,000 users request the same expired page at the same moment, the PoP sends one request to the origin and gives the answer to all 1,000. Without it, a popular page expiring can overload the origin ("thundering herd").
- Serving stale on error. With
stale-if-error, users get the last good copy while the origin is down or restarting. For many sites this turns an outage into something visitors never notice.
Users ──► 300 edge PoPs ──► 3 shield PoPs ──► origin
(most hits here) (most misses (sees only a
end here) few requests)
8. The Edge as a Place to Run Code
Since around 2017, CDNs let you run your own code in their PoPs: Cloudflare Workers, Fastly Compute, AWS Lambda@Edge and CloudFront Functions, and the edge runtimes of hosting platforms like Vercel and Netlify. Requests are handled by small functions, usually in JavaScript or WebAssembly, that start in milliseconds.
Edge code is good at decisions that need to be fast and close to the user:
- Redirects, rewrites and A/B test assignment.
- Checking a token or a signed URL before serving a file.
- Choosing a language or region version.
- Adding or removing headers, including security headers.
- Personalising a cached page with small pieces, instead of making the whole page uncacheable.
It is weaker at anything that needs your data. If an edge function in Sydney must ask a database in Frankfurt three times, it is slower than running the code next to the database. The rule of thumb: move code to the edge only together with the data it needs, or when it needs no data at all. CDN providers now offer edge key-value stores and databases for this reason, but they come with their own consistency trade-offs.
9. Security at the Edge
Because every request passes through it, the CDN is also a security layer.
- DDoS protection. A large CDN has far more capacity than any single origin, and anycast spreads an attack across the whole network.
- Web application firewall (WAF). Rules block common attacks such as SQL injection and cross-site scripting before they reach your application.
- Bot management. Detects scrapers, credential stuffing and abusive crawlers by behaviour and fingerprints.
- Hiding the origin. If attackers find your origin's real IP address, they can bypass all of this. Allow only the CDN's addresses at the origin firewall, or connect the origin to the CDN with an outbound tunnel so it needs no open port at all. Mutual TLS, where the CDN also shows a certificate to the origin, closes the gap completely.
The CDN can also create new security problems:
- Web cache poisoning. If an attacker can change a response through a header that is not in the cache key (for example
X-Forwarded-Host), and that response is cached, every later visitor gets the attacker's version. Security researcher James Kettle showed many real cases in 2018. - Web cache deception. An attacker tricks a logged-in user into opening a URL like
/account/settings/x.css. If the application ignores thex.csspart and returns the account page, while the CDN caches anything ending in.css, the private page is now in the public cache. Omer Gil described this attack in 2017. - Private data in shared caches. A missing
privateor a forgottenSet-Cookiecan leak one user's page to the next.
The defence is similar in all three cases: cache only by explicit rules, keep personal responses private or no-store, and make sure nothing outside the cache key can change a cached response.
10. When the Edge Fails
A large part of the web runs through a few CDN providers. When one of them breaks, thousands of unrelated websites break at the same time. The incidents follow a pattern:
- Cloudflare, July 2019. A new WAF rule contained a regular expression that used up the CPU on every server. Traffic failed worldwide for about 30 minutes.
- Fastly, June 2021. A customer changed a valid setting that triggered a hidden software bug. Within minutes, most of Fastly's network returned errors. Sites such as large newspapers, shops and government portals were down for about an hour.
- Cloudflare, November 2025. A database permission change made a query return duplicate rows. A configuration file for bot detection doubled in size, exceeded a hard limit in the proxy software, and the proxies crashed as the file spread across the network. It took several hours to fully recover, and the team first suspected a DDoS attack.
- Cloudflare, February 2026. A change to how Cloudflare managed IP ranges that customers bring along ("BYOIP") withdrew about a quarter of those ranges from the internet. The incident lasted about six hours.
None of these was an attack. Each was a configuration change that spread to every location very quickly, which is exactly the strength of a CDN turned into its weakness. Cloudflare and others have since moved to slower, staged rollouts for configuration, not only for code.
What you can do on your side:
- Know your dependency. If your CDN is down, is your site down? For most sites, yes.
- Keep your status page and incident communication off that CDN, so you can still tell users what is happening.
- Have a way to bypass the CDN, for example DNS records you can switch to the origin, with a short TTL (cache lifetime) so the switch takes effect quickly. If the origin only accepts traffic from the CDN (section 9), plan how to open it for that case, and make sure it can handle the load for a while.
- Use multiple CDNs if the business really needs it. It doubles the configuration work, so it is usually worth it only for large sites.
11. Checking a Site Yourself
You can see most of this with curl. Look at the response headers of any page or file:
curl -sI https://www.example.com/assets/app.3f9a2c.js
HTTP/2 200
cache-control: public, max-age=31536000, immutable
age: 48211
cache-status: ExampleCDN; hit
server-timing: cdn-cache; desc=HIT, edge; dur=2
Agesays how many seconds the response has been in a cache. A value above 0 means it came from a cache.Cache-Status(RFC 9211) is the standard way to report hits and misses. Many CDNs still use their own headers, such ascf-cache-status,x-cacheorx-served-by.Server-Timingcan show how long the edge and the origin took.
Request the same URL twice. If the second answer is a hit with a growing Age, caching works. If every answer is a miss, look for short lifetimes, Set-Cookie, Vary or private in the response.
12. A Checklist for Your Site
- Put hashed file names on all static assets and serve them with
max-age=31536000, immutable. - Give HTML a short shared lifetime with
stale-while-revalidate, for examples-maxage=60, stale-while-revalidate=600, and purge on publish. - Add
stale-if-errorso a broken origin does not mean a broken site. - Mark personal responses
privateorno-store, and never cache responses that set cookies. - Clean up the cache key: ignore tracking parameters, avoid
Vary: CookieandVary: User-Agent. - Purge by tag from your publishing workflow, not by hand.
- Turn on tiered caching or an origin shield and request collapsing.
- Lock the origin: accept traffic only from the CDN, or use a tunnel.
- Enable HTTP/3 and TLS 1.3 at the edge; it is usually a single setting and helps mobile users most.
- Watch the hit ratio and the origin load, and plan what happens when the CDN is down.
13. Conclusion
A CDN does not make your server faster. It makes the distance shorter, for every handshake and every file, and it shields your origin from load and attacks.
- Distance is the limit. Light needs time, and every round trip pays for it. A nearby PoP cuts that cost for everything at once.
- DNS routing and anycast bring each user to a nearby PoP; anycast also spreads attacks and fails over automatically.
- Caching is controlled by your headers.
Cache-Control, the cache key andVarydecide whether the CDN can help at all. - Hashed names and tag-based purging let you cache aggressively and still publish changes in seconds.
- The edge can run code, which works best when the code needs little or no data from far away.
- The CDN is a security layer and a single point of failure. Configure caching carefully, lock your origin, and plan for the day your provider has a bad hour.
Try it with a site you know. Run curl -sI twice against one of its images and one of its pages, and compare the Age and cache headers. You will see immediately which parts of the site are served from a city near you, and which ones still travel all the way to the origin.
Sources and Further Reading
- R. Fielding, M. Nottingham, J. Reschke, RFC 9111: HTTP Caching, 2022.
- M. Nottingham, RFC 5861: HTTP Cache-Control Extensions for Stale Content, 2010.
- S. Ludin, M. Nottingham, Y. Wu, RFC 9213: Targeted HTTP Cache Control, 2022.
- M. Nottingham, RFC 9211: The Cache-Status HTTP Response Header Field, 2022.
- C. Contavalli et al., RFC 7871: Client Subnet in DNS Queries, 2016.
- M. Bishop, RFC 9114: HTTP/3, 2022.
- Cloudflare, "Details of the Cloudflare outage on July 2, 2019": https://blog.cloudflare.com/details-of-the-cloudflare-outage-on-july-2-2019/
- Fastly, "Summary of June 8 outage", 2021: https://www.fastly.com/blog/summary-of-june-8-outage
- Cloudflare, "Cloudflare outage on November 18, 2025": https://blog.cloudflare.com/18-november-2025-outage/
- Cloudflare, "Cloudflare outage on February 20, 2026": https://blog.cloudflare.com/cloudflare-outage-february-20-2026/
- J. Kettle, "Practical Web Cache Poisoning", PortSwigger Research, 2018: https://portswigger.net/research/practical-web-cache-poisoning
- O. Gil, "Web Cache Deception Attack", Black Hat USA, 2017.
- E. Nygren, R. Sitaraman, J. Sun, "The Akamai Network: A Platform for High-Performance Internet Applications", ACM SIGOPS Operating Systems Review, 2010.




