Lesson 2 of 16
26. CDNs

26.1 Why use a CDN?

3 min read+10 XP

In the warehouse: A shop in Guwahati waiting on Mumbai gets slow deliveries. A shop collecting from its city warehouse gets them in an hour. And the factory, freed from thousands of small orders, can focus on making biscuits.

In the system: Without a CDN, every request crosses the country (or the world) to your origin. A web page needs several round trips before anything shows: DNS, a TCP handshake, a TLS handshake, the HTML, then CSS and JS (Chapters 3 to 5). With an assumed 60 ms round trip from Guwahati to Mumbai, six round trips cost about 360 ms before a single byte of the page appears. From a CDN location in Guwahati, at about 8 ms per round trip, it's about 48 ms.

a reader in Guwahati: six round trips to Mumbai vs six round trips to a nearby edge.

Figure: distance is latency; a nearby copy removes most of it.

What a CDN gives you:

  • Lower latency, because the round trips are short.
  • Less load on the origin. With 10,000 requests a second and a 95% cache hit ratio, the origin sees 500 a second; at 99%, only 100. One point of hit ratio, five times less origin traffic.
  • Lower bandwidth cost from the origin, and often cheaper delivery per GB.
  • Absorbing spikes and attacks. A Big Book Sale or a DDoS hits the CDN's huge network first, not your servers.
  • Extras at the edge: TLS termination, HTTP/2 and HTTP/3, compression, image resizing, a web application firewall (WAF), bot protection.

What it isn't: a fix for slow uncacheable responses. A personalised cart still goes to the origin. A CDN shines for content that many people request and that doesn't change per user.

Memory code: "Stock it near the shop, and the factory can rest."