CDN and content delivery
- Lesson 8 of 40
- 8 min
- Beginner
- 10 steps
Move the files everyone downloads to servers near your users and stop fighting the speed of light.
- Latency in London
- 10 ms
- Latency in Sydney
- 12 ms
- Origin load
- 540 req/s
- Edge hit rate
- 94%
- request
- response
- cache miss
Step 1 of 10
Problem
Your users live far from the server
Your server sits in Virginia. London is about 5,900 km away and Sydney about 15,700 km: in fibre, light cannot make the round trip in under 59 ms and 157 ms. With real routers and routes, the example uses ≈ 75 ms and ≈ 200 ms.
Loading a page takes many trips, so in Sydney the wait easily goes past a second. And a single origin serves every image to every user: 9,000 req/s.
Step 2 of 10
Concept
Most of the page is the same for everyone
Open a page and look at what it downloads. The HTML depends on who asks, but images, JavaScript, CSS and fonts are exactly the same for every visitor.
In the example, only 40 of 2,000 KB are personal: 98% could be stored anywhere in the world, because it does not depend on who is asking.
Step 3 of 10
Solution
Copies on edge servers
A CDN (content delivery network) runs edge servers in many cities. You put your static files behind it and each user gets the copy from the nearest edge.
London drops to ≈ 10 ms and Sydney to ≈ 12 ms. Only when the edge lacks the file (a miss) does the request travel to Virginia: the origin falls from 9,000 to 900 req/s.
What changed
- Edges in London and Sydney
- Edge hit rate: 0 → 90%
- Origin load: 9,000 → 900 req/s
Step 4 of 10
Technique
A new edge starts empty
An edge works as a pull-through cache: when it lacks a file, it fetches it from the origin, keeps a copy and hands it to everyone who comes next.
London has been running for a while and is warm. The Sydney edge just started: half its requests miss and pay the trip to Virginia, so the average climbs to ≈ 112 ms until it fills up.
What changed
- Sydney edge: cold, half misses
- Sydney: 12 → 112 ms on average
- Origin load: 900 → 2,700 req/s
Step 5 of 10
Problem
You change the logo and nobody sees it
You upload a new logo.png to the origin. But the edges stored the old one with a one-day TTL, and keep serving it in London and Sydney for hours.
You can purge the CDN, which usually takes seconds, but it relies on someone remembering every time. And a purge never reaches the copies browsers already kept.
What changed
- Origin: new logo.png
- Edges still serve the old one
Step 6 of 10
Solution
Each version gets its own name
Add a fingerprint of the content to the name: logo.3f9a.png. When the file changes, so does the name, so an old copy can never pass for the new one.
Those files can now be cached for a year. The HTML, with a short TTL or no cache, always points at the current names. Fewer misses: 96% hit rate and 360 req/s at the origin.
What changed
- logo.png → logo.3f9a.png
- TTL: 1 day → 1 year
- Origin load: 900 → 360 req/s
Step 7 of 10
Concept
The edge takes the spike
A launch goes viral and traffic grows 20 times: from 9,000 to 180,000 req/s. Since almost everyone asks for the same files, the edges hit 99% of the time.
The origin only gets the 1% that misses: 1,800 req/s. The same spread-out capacity also helps soak up denial-of-service attacks (DDoS).
What changed
- Traffic: × 20
- Edge hit rate: 96 → 99%
- Origin load: 1,800 req/s
Step 8 of 10
Problem
Real case: someone else's cart
Someone sets the CDN to cache every page for five minutes. Ana opens her cart and the edge stores it; Luis opens his shortly after and gets Ana's.
Responses that depend on who is asking carry Cache-Control: private (or no-store) and the edge always passes them to the origin. In the example they are 2% of traffic: 94% hit rate, origin at 540 req/s.
What changed
- Cart and account: Cache-Control: private
- Edge hit rate: 94%
Step 9 of 10
Technique
A shield in front of the origin
A real CDN has hundreds of edges. When a new file gets popular, every edge misses once and hundreds of identical requests hit the origin together.
An origin shield is a middle cache: edges ask it, and only it asks the origin, once per file. If it answers 4 out of 5 misses, the origin drops from 540 to 108 req/s.
What changed
- Shield between the edges and the origin
- Origin load: 540 → 108 req/s
Step 10 of 10
Summary
Summary
Distance sets a floor on latency; edges bring content close to whoever asks for it.
A CDN is a pull-through cache: misses fill it. Version file names to cache for a year and still update instantly.
The edge soaks up spikes and protects the origin. Never cache personal pages publicly and, at scale, add a shield.
← → step · R replay · Space pause
You will learn
- Why distance sets a floor on latency (covered)
- How a CDN serves copies from the nearest edge (covered)
- What a cold cache is and why to version files (covered)
- How the edge absorbs traffic spikes (covered)
- What must never be cached and what an origin shield is for (covered)