Load/Bearing
Fundamentals · 07 of 22

CDNs and edge caching

You can't make light faster, but you can shorten the distance by serving bytes from a city near each user.

A content delivery network is a fleet of caching servers, called points of presence (PoPs), spread across hundreds of cities. A user's request goes to a nearby PoP. If that PoP has the file, it answers in a few milliseconds. If not, it fetches the file from your origin once, stores it, and serves everyone nearby from then on.

USERSydneyEDGE PoPSydney · ~10 msSHIELDregionalORIGINVirginia
On a hit, the edge answers locally. On a miss, it asks a regional shield cache, and only then the origin.

Physics sets the floor

Light in optical fibre travels at about 200,000 km per second, roughly two-thirds of its speed in a vacuum. That works out to about 1 ms of round-trip time per 100 km, before routers and indirect cable paths add more.

RouteDistanceBest-case RTTTypical RTT
New York ↔ London5,600 km56 ms70–80 ms
San Francisco ↔ Tokyo8,300 km83 ms100–120 ms
London ↔ Sydney17,000 km170 ms250+ ms

A new HTTPS connection costs several round trips before the first byte arrives (TCP handshake plus TLS handshake). From Sydney to an origin in Virginia that's half a second of waiting, and no amount of server capacity fixes it. Only distance does.

What CDNs serve

Controlling the cache with headers

Cache-Control: public, max-age=31536000, immutable # hashed assets Cache-Control: public, max-age=60, stale-while-revalidate=300 # semi-fresh API Cache-Control: private, no-store # per-user data

Cache busting beats invalidation

Purging a file from hundreds of PoPs is slow and sometimes incomplete. The better pattern is content-hashed filenames: app.3f9a1c.js can be cached forever, because a new build produces app.b72e04.js and the HTML points to the new name. Only the HTML needs a short TTL.

Pull vs push

A pull CDN fetches from the origin on the first miss. That's the common choice, with no upload step. A push CDN has you upload content ahead of time, which suits large, predictable files like video releases. An origin shield adds one regional cache layer so that 300 PoPs missing at once send one request to your origin, not 300.

In the game: the CDN serves 95% of static requests at 14 ms for everyone, including overseas users, who otherwise pay +130 ms on every request. In Photo Wall, 88% of traffic is static, so one CDN does more than nine app servers would.

Interview checklist

Practise it

Level 6: Photo Wall

Put this into practice against live traffic. Your progress in the game is saved in your browser.

Play level 6

Further reading