26. CDNs
The picture to keep in your head
Every question below is answered twice: first in the warehouse, so you can picture it, then in the system, so you know exactly how it works. Each answer ends with a memory code to lock it in.
The picture to keep in your head
A biscuit company has one factory in Mumbai. If every shop in India had to send a truck to Mumbai for every carton, deliveries to Guwahati would take days and the factory gates would be jammed. So the company keeps warehouses in every big city. Shops collect from the nearest warehouse. The factory only ships to a warehouse when it runs out.
Every carton carries a label (which biscuit, which size) and a use-by date. When a batch turns out to be faulty, the company sends a recall notice and every warehouse pulls it off the shelves. And to stop fifty warehouses all phoning the factory at once for a new product, there's a regional depot in between that the warehouses call first.
That's a content delivery network (CDN): copies of your files kept close to your users, with clear rules about what's the same file, how long it stays fresh, and how to recall it.
Our running example: the bookstore's website. The origin servers are in Mumbai; readers are all over India and abroad. Pages load book covers, JavaScript and CSS bundles, book pages, and the new author-interview videos. Latency and traffic numbers are assumptions for practice.
| In the warehouse | In the system |
|---|---|
| The factory in Mumbai | The origin server |
| A warehouse in every big city | Edge locations (PoPs) |
| Warehouses restock on demand vs ship in advance | Pull vs push CDN |
| Carton labels and use-by dates | Cache keys and Cache-Control headers |
| A recall notice | Purging |
| A regional depot between warehouses and factory | Origin shield |
| Warehouses that also assemble gift boxes | Dynamic content and code at the edge |
| A film delivered as a box of short reels | Video segments (HLS, DASH) |
| A warehouse chain going on strike | When the CDN fails |
Figure: keep copies close to customers, label how long they stay fresh, and know how to recall them.