Introduction to a CDN
easy 26 minA content delivery network (CDN) is a set of servers in many places that keep copies of your content and answer users from nearby.
See it run
Follow one request from Sydney: the long trip, the first copy, the short trip, and the moment a copy expires.
Operations
Without a CDN
A user in Sydney, a server in Virginia. Assumed round trip: 200 ms.
Step 1 of 6
The user asks
- Time so far
- 0 ms
A picture is needed. Nothing is known yet.
Step 1: The user asks
dns = 20tcp = 200tls = 200request = 200 + 30total = dns + tcp + tls + request # 650 ms- Time
- 650 ms
- Origin work
- every request
- Long trips
- 3
Handshakes are round trips too. They cost as much as the answer.
In the real world
The same introduction to a cdn doing real jobs. Pick one and step through it.
Three readers on three continents.
Step 1 of 2
All cold
3 trips to the origin
Every edge must fetch its own first copy.
Step 1: All cold
Common mistakes
The bugs interviewers watch for, with the fix beside each.
A personal page in a shared cache
criticalThe next person at that edge receives somebody else's page.
Wrongheaders["Cache-Control"] = "public, max-age=600" # account pageRightheaders["Cache-Control"] = "private, no-store"No cache headers at all
majorEach CDN then uses its own default, which may surprise you.
Wrongreturn Response(body)Rightreturn Response(body, {"Cache-Control": "public, max-age=3600"})Changing a file under the same name
majorEdges and browsers keep the earlier copy until it expires.
Wrongupload("app.js")Rightupload("app." + content_hash + ".js")Origin open to the world
criticalAn attacker, or a wrong link, goes around the CDN.
Wrongallow_from = "anyone"Rightallow_from = "the CDN, with a checked identity"
Interview tips
How to explain it
- Kiosks near readers, one print shop.
- Hit: from the shelf.
- Miss: wait for the van once.
Memory trick: SPEED
- Servers spread out, Proximity.
- Edge caches, Everyone served fast.
- Delivery from nearby.
Numbers
- Three round trips for new HTTPS.
- Origin load: R x (1 - h).
- 95% to 90% doubles it.
Ask
- Same for everybody?
- How old may it be?
- How will it change?