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.
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.
| Route | Distance | Best-case RTT | Typical RTT |
|---|---|---|---|
| New York ↔ London | 5,600 km | 56 ms | 70–80 ms |
| San Francisco ↔ Tokyo | 8,300 km | 83 ms | 100–120 ms |
| London ↔ Sydney | 17,000 km | 170 ms | 250+ 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
- Static assets: images, video, JavaScript, CSS and fonts. These are often 80–95% of requests and most of the bytes.
- Cacheable API responses: public, rarely changing data such as product catalogues or a public timeline, with short TTLs.
- Dynamic acceleration: even uncacheable requests get faster. TLS ends at the nearby edge, and the edge keeps warm, pooled connections back to the origin.
Controlling the cache with headers
max-ageis the browser TTL;s-maxageoverrides it for shared caches like CDNs.stale-while-revalidateserves the old copy instantly while fetching a fresh one in the background.Varylists the request headers that change the response. Vary on too much and the hit rate collapses.
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
- Put static media in object storage (S3, GCS) behind a CDN.
- Use content-hashed filenames with long TTLs, and a short TTL for the HTML.
- Explain what happens on a miss, and add an origin shield for big fan-out.
- Say whether any API responses are cacheable at the edge.
- Remember that dynamic requests still benefit from TLS ending at the edge.
Level 6: Photo Wall
Put this into practice against live traffic. Your progress in the game is saved in your browser.
Play level 6Further reading
- Martin Kleppmann, Designing Data-Intensive Applications (O'Reilly). The standard book on replication, partitioning and consistency.
- Site Reliability Engineering, Google's free book on running reliable systems.
- The Amazon Builders' Library: practical articles on timeouts, retries, caching and load shedding.
- The System Design Primer: a broad, community-maintained overview.