CDNs
Why distance sets a floor on latency, how a CDN routes users to a nearby edge and what it caches there, and the cost and failure trade-offs of putting one in front of your origin.
The problem
Light in optical fibre covers about 200,000 km per second. That sounds fast until you divide: every 1,000 km costs about 5 ms each way, before any router adds its own delay. Sydney to a data centre in northern Virginia is roughly 15,700 km in a straight line, and real cables do not run straight. A round trip of about 200 ms is typical.
Now count round trips. A new HTTPS connection needs one for the TCP handshake, one for TLS 1.3, and one for the request itself. That is about 600 ms before the first byte of the page arrives, and TCP slow start then spreads a large file over several more. Nothing you do to your servers changes this. The origin could answer in zero milliseconds and the user would still wait.
The late 1990s web had a second problem. When a news story or a product launch sent a crowd to one site, every request landed on one origin, which fell over. Engineers bought bigger servers and more bandwidth, which helped until the next, bigger crowd.
Both problems have the same answer: keep copies of the content on servers near the users. Akamai, founded in 1998 by a group from MIT that included Tom Leighton and Danny Lewin, built the first large content delivery network on that idea.
How a CDN works
A CDN operates points of presence (PoPs): clusters of caching reverse proxies in hundreds of cities, often inside or next to ISP networks. Requests for your hostname go to a nearby PoP instead of to your origin.
Step 1 of 8
Without a CDN, a user in Sydney talks straight to an origin in Virginia. A round trip takes about 200 milliseconds, and a new HTTPS connection needs three of them before the first byte of the response arrives.
Read the steps as text
- Without a CDN, a user in Sydney talks straight to an origin in Virginia. A round trip takes about 200 milliseconds, and a new HTTPS connection needs three of them before the first byte of the response arrives.
- With a CDN, the asset hostname leads to a nearby point of presence instead. Either the CDN's DNS answers with the address of a close PoP, or one anycast address is routed to the closest one.
- The user connects to a PoP five milliseconds away, so the TCP and TLS handshakes are cheap. Then it asks for a script. This PoP has never served that file, so it is a cache miss.
- The PoP fetches the file from the origin, over a connection it already holds open. The origin's response says how long the file may be kept, here one year, because the file name changes whenever the content does.
- The PoP stores the file and answers the user. This first request paid for the long trip once, on a warm connection, which is still faster than going direct.
- A second user in Sydney asks for the same file. The PoP answers from its cache in a few milliseconds. The origin never hears about this request, or the next million like it.
- Every other PoP starts cold, though. A popular file is fetched from the origin once per PoP, and again whenever a PoP evicts it. Origin shielding puts one more cache in between to absorb those repeats.
- The CDN moved both the bytes and the handshakes close to the user, and left the origin serving a small fraction of requests. That fraction, one minus the cache hit ratio, is the number to watch.
On a hit the PoP answers from its own disk or memory. On a miss it fetches from the origin, stores the response if your headers allow, and answers. The share of requests served without touching the origin is the cache hit ratio, and it is the main number that decides both speed and cost.
How long an object stays, and what counts as “the same object”, is governed by cache headers and the cache key. This lesson keeps that brief. CDN caching covers Cache-Control, revalidation and purging in detail.
Pull and push
With a pull CDN you point the CDN at your origin and do nothing else. The first request for each object at each PoP is a miss and pulls it in. This is the default, and it suits almost every website.
With push you upload content to the CDN’s storage ahead of time. That suits large files you know about in advance, such as a game patch or a video library, where you do not want the first users to pay for the miss. You then own the lifecycle: what is uploaded, and when it is removed. In practice the common modern form is a pull CDN in front of object storage that you populate.
How requests reach an edge
Two mechanisms steer a user to a PoP.
- DNS-based routing. Your asset hostname is a CNAME to the CDN. The CDN’s name servers answer with the address of a PoP chosen for that query, by location, PoP load and network health. It gives the CDN fine control, but it inherits the limits of DNS: the CDN sees the resolver, not the user, and answers are cached for the TTL.
- Anycast. Every PoP announces the same IP address, and internet routing delivers each packet to the nearest one. Failover is quick, because a PoP that stops announcing stops receiving, and attack traffic is spread across all PoPs. The CDN has less control, since “nearest” is decided by BGP.
Many CDNs combine the two.
What to put on a CDN
| Content | Cache it? | How |
|---|---|---|
| Scripts, styles, fonts, images | Yes, for a long time | Content hash in the file name, lifetime of a year |
| Video segments, downloads | Yes | Large objects, often pushed or served from object storage |
| Public pages and API responses | Yes, briefly | Seconds to minutes, and purge when the data changes |
| Personalised or private responses | No | Mark as private, and pass through to the origin |
| Writes | Never | Still routed through the edge for TLS and protection |
The fingerprinted file name is what makes long lifetimes safe. app.9f3c.js never changes, and a new release has a new name, so nothing needs purging.
The dangerous row is personalised content. If a per-user page is cached under its URL, the next visitor receives someone else’s data. Make sure such responses are explicitly uncacheable.
Dynamic acceleration
A CDN helps even when nothing can be cached. The user’s TCP and TLS handshakes finish at a PoP a few milliseconds away. The PoP then forwards the request over a connection to your origin that is already open and warmed up, often across the CDN’s own backbone. The one unavoidable long round trip is the request itself. The edge can also speak HTTP/3 to the user whatever your origin supports.
Most CDNs also run code at the edge (Cloudflare Workers, Lambda@Edge and similar), which suits small decisions such as redirects, header rewriting, A/B bucketing or rejecting unauthenticated requests before they travel anywhere.
Origin shielding
Each PoP has its own cache. With 200 PoPs, one popular object can be fetched from your origin 200 times, and again each time it expires.
Origin shielding, also called tiered caching, routes edge misses through one designated PoP near the origin. The edges fill from the shield, and the origin sees roughly one fetch per object. Good CDNs also collapse concurrent requests, so that a thousand simultaneous misses for one object become a single origin fetch. The cost is an extra hop on a miss, and a shield that becomes a point of concentration.
Cost and failure
Cost. CDNs charge mainly per gigabyte delivered and per request, with prices that vary by region. A low hit ratio means you pay twice, for CDN delivery and for origin egress. At very large volume, companies negotiate rates, split traffic over several CDNs, or build their own, as Netflix did by placing its Open Connect appliances inside ISP networks.
Failure. Think through four cases.
- The CDN fails. On 8 June 2021 a fault at Fastly made many major sites unreachable for about an hour. A second CDN with DNS switching is the defence. It is expensive, and you must keep both configurations in step.
- The origin fails. A CDN can keep serving stale copies while the origin is down, if you allow it. This is one of the cheapest availability gains you can get.
- The cache empties. A purge-everything or a switch to a new CDN sends full traffic to an origin sized for a fraction of it. See cache stampedes.
- The origin is exposed. If the origin accepts traffic from anywhere, attackers go round the CDN. Accept only the CDN’s address ranges, or require a credential that only the CDN holds.
When to choose it and when not to
Use a CDN when users are far from your origin, when much of the traffic is static or shared between users, when traffic comes in spikes, or when you want DDoS absorption and TLS at the edge. For a public consumer product that covers nearly every case, which is why a CDN is in almost every design.
It earns less when all your users sit close to the origin and every response is per-user, for internal service-to-service traffic, and when data may not be handled by a third party. Do not claim it speeds up writes or database queries. It shortens the network path and removes repeat work, and that is all.
In the interview
Mention the CDN when you draw the client: “static assets and media go through a CDN, and the API goes through it too for TLS termination and protection, uncached”. Then show that you know its edges: what is cached and for how long, how a new release reaches users (fingerprinted names, or a purge), what the origin sees on a cold cache, and what happens if the CDN or the origin goes down. For video or image-heavy systems, add origin shielding and a rough cost per gigabyte, because that is where the money goes.
Key takeaways
- Latency has a floor set by distance. A CDN removes it by answering from a point of presence near the user.
- A pull CDN fetches from your origin on the first request and caches by your headers. Push means uploading content ahead of time.
- Users reach an edge through the CDN's DNS, through anycast, or both.
- Cache static assets for a long time under fingerprinted names, cache public pages briefly, and never cache personalised responses by accident.
- Even uncacheable requests gain from the edge, because handshakes happen nearby and the CDN reuses warm connections to the origin.
- Origin shielding raises the hit ratio and protects the origin. The CDN is still a dependency that can fail, and an origin left reachable from the internet can be attacked around it.
Check yourself
0 of 5 answered
Flashcards
These 6 cards are in your review deck. Tap a card to see the answer.
Why does a CDN make a site faster even with a fast origin?
Distance sets a latency floor of about 1 ms of round trip per 100 km of fibre. Serving from a nearby PoP shortens every round trip, including the TCP and TLS handshakes.
Pull CDN versus push CDN?
Pull fetches from your origin on the first request and caches according to your headers. Push means you upload content to the CDN in advance. Pull is the default, and push suits large files known ahead of time.
What are the two ways a user is routed to a nearby PoP?
DNS-based routing, where the CDN's name servers return the address of a close PoP, and anycast, where one IP address is announced from every PoP and BGP delivers packets to the nearest.
What is origin shielding?
A designated cache between the edge PoPs and the origin. Edge misses go to the shield first, so the origin sees one fetch per object instead of one per PoP.
What should never be cached at a CDN by accident?
Personalised or private responses. Mark them private or no-store, or make the cache key include the user, otherwise one user's page can be served to another.
What is dynamic acceleration?
Speeding up uncacheable requests by terminating TCP and TLS at the edge, reusing warm connections from edge to origin, and routing over the CDN's own network.