CSE Roadmap

Core tier · about 3 hours · 8 parts

Part 2 of 8 · 2 min read

Bringing the file close to the user

Why distance costs time, and what a CDN does about it.

Think of a book publisher with one warehouse in one country. Every order from anywhere ships from there. Now the publisher opens small shops in many cities, each holding the popular books. A buyer picks up a book from the local shop, and the warehouse only restocks the shops.

A CDN does this for files.

Content delivery network (CDN)

A CDN is a network of servers spread across the world that keep copies of files and deliver them to nearby users. The book "System Design Interview" describes it as geographically dispersed servers that deliver static content such as images, video, CSS and JavaScript.

Example: a shop's logo is stored once at the shop's own server. A CDN keeps a copy in 200 cities, and a visitor in Pune gets it from a city close by.

Some words come with it.

Origin and edge

The origin is the server that owns the real file. An edge server is a CDN server near the users. Edge servers copy from the origin and deliver to users.

Example: the shop's own web server (or its file storage) is the origin. The CDN's server in Mumbai is an edge server.

The reason it helps is distance, which page 1 of this level measured in round trips. No server is quick enough to beat the speed of light. The book "Understanding Distributed Systems" notes that a client on the far side of the world sees more than 100 milliseconds of delay from the network alone.

A worked example. A user is 150 milliseconds (round trip) from the origin and 15 milliseconds from the nearest edge. A fresh connection needs 1 round trip for TCP, 2 for TLS and 1 for the request, 4 in all. DNS is left out for simplicity.

  • From the origin: 4 × 150 = 600 ms before the first byte.
  • From the edge: 4 × 15 = 60 ms before the first byte.
  • Saving: 540 ms for each new connection, and 135 ms for each later request on the open connection (150 − 15).

A page with 30 images multiplies the second saving. Even with several requests in parallel, the user feels the difference.

The CDN also protects the origin. Each file the edge delivers is a request the origin never sees.

Read it as

A CDN trades some storage in many places for time, and for less load at the origin.

Check yourself:

  • What is the difference between the origin and an edge server?
  • If the round trip to the edge is 20 ms and to the origin is 180 ms, how long does a fresh connection save?