Building block

CDN

A worldwide network of servers that caches your content closer to users. It cuts latency from hundreds of milliseconds to tens and takes traffic off the origin.

CDN: users hit the nearest edge, misses go through a shield to the origin~15 ms~10 msmissmissUsers, KyivUsers, LisbonEdge, WarsawEdge, MadridOrigin shieldOriginVideo storage
CDN: users hit the nearest edge, misses go through a shield to the origin

The physics of latency

Light in fiber travels about 200 km per millisecond. A round trip from Kyiv to a server on the US East Coast takes roughly 110 to 130 ms, and that is before the TLS handshake, which needs extra round trips.

A CDN places edge servers in hundreds of cities. A user in Kyiv gets video lessons from the nearest node (say, Warsaw or Frankfurt) in 10 to 30 ms, and the origin only sees cache misses.

What to cache and how to control it

Static files with a hash in the name (app.3f9a.js, fonts, images) are cached forever: Cache-Control: public, max-age=31536000, immutable. HTML and APIs are cached carefully, with a short s-maxage and stale-while-revalidate, so the edge serves the old version while fetching the new one.

Video uses HLS or DASH: the file is cut into 2 to 6 second segments, and the CDN caches each segment separately. So a popular lesson watched a thousand times is served by the origin only once per region.

An origin shield (an intermediate cache tier) collects misses from all edge nodes, so even fewer requests reach the origin.

More than a cache

Modern CDNs terminate TLS, absorb DDoS attacks, filter bots (WAF) and run code at the edge (Cloudflare Workers). Your origin may not need a public IP address at all.

Invalidation: after a file changes you purge by URL or tag. It is simpler to change the file name (a hash in the name) than to clear caches in hundreds of cities.

Pros

  • Latency for distant users drops several times.
  • The origin gets only a small share of traffic.
  • DDoS protection and TLS out of the box.
  • Cheaper traffic than serving straight from the cloud.

Cons

  • Cache invalidation across hundreds of nodes is not instant.
  • A mistake in cache headers can serve personal data to another user.
  • Provider dependency: its outage affects your site.

When to choose it

  • Users are spread across countries or regions.
  • Lots of static files, images or video.
  • You need protection from DDoS and bots.
  • The origin has limited bandwidth or expensive egress.

Tools

Sources

Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).