Course outline · 0% complete

0/25 lessons0%

Course overview →

CDNs: moving the content closer

lesson 8-2 · ~9 min · 23/25

You cannot beat the speed of light

A request from Sydney to a server in Virginia crosses about 16,000 km of fiber. Even at light speed, the round trip costs about 200 ms, and the lesson 1-2 journey needs several round trips (TCP handshake, TLS handshake, then the request itself). Distance is latency, and no amount of server power fixes it.

A CDN (Content Delivery Network) fixes it the only way physics allows: by moving copies of the content closer to users. A CDN operates hundreds of edge servers around the world. DNS steers each user to the nearest edge, and the edge serves cached copies of the site's files:

  • Cache hit: the edge already has the file (someone nearby requested it recently) and answers in ~20 ms.
  • Cache miss: the edge does not have it, fetches it once from the origin (the site's real server), stores it, then serves everyone nearby from the copy.

What may a CDN store? Exactly what lesson 8-1 taught: responses marked public with a max-age. That is why those headers matter beyond your own browser. You can spot a CDN in response headers like X-Cache: HIT or Server: cloudflare.

UserSydneyCDN edgeSydney, cachedOriginVirginiahit: ~20 msmiss only: ~200 msmost requests stop at the edge, only misses travel on to the origin
A CDN in action: the short gold trip (user to nearby edge) happens on every request, the long trip to the origin happens only on a cache miss.

Quiz

A news site's homepage image gets 100,000 views per hour from Tokyo through a CDN with max-age=300. Roughly how often does the origin server actually serve that image to Tokyo?

Code exercise · python

Your turn. Put numbers on the quiz above. With max-age=300, an edge's copy expires every 300 seconds, so the origin serves that file to that edge about 3600 // max_age times per hour — every other request is a cache hit. Compute origin_fetches, then the hit ratio as a percentage rounded to 3 decimals, printing the two lines shown.

Problem

The first visitor in Sydney requests a file and the local edge server does not have a copy yet, so it must fetch from the origin. What is this cache event called? (Two words, or just the second word.)