Design a Content Delivery Network (CDN)
hard 45 minBuild the service that other websites use to answer their visitors from nearby.
Video lesson · Chapter 1 of 5
The origin never sees a warm request
A CDN in 90 seconds
1:30Reel coming soon
Theory
Read or listen first. About 2 min.
What it is
A content delivery network is a set of machines in many cities that keep copies of other people's web content and hand them to visitors nearby. The machine that holds the original is called the origin.
Designing one means running it as a service for millions of websites. Each customer sets their own rules, expects their own privacy, and wants a changed file to disappear everywhere within seconds.
Why it exists
Distance costs time. Light in glass covers about 200 km in a millisecond, so Tokyo to New York and back, about 10,850 km each way in a straight line, needs at least 108 ms. Real routes are longer, and a new secure connection needs several trips.
Copies also take load away from the origin and spread attacks over many places. A small team with one server can survive a day of fame.
A short history
Late 1990s
The first content delivery networks appear, to relieve slow paths on the growing web.
December 2006
RFC 4786 describes how to run services on anycast addresses.
May 2010
RFC 5861 adds stale-while-revalidate and stale-if-error to HTTP caching.
August 2017
RFC 8216 publishes HTTP Live Streaming, video cut into small files that any HTTP cache can keep.
June 2022
RFC 9111 becomes the HTTP caching standard, and RFC 9213 gives CDNs a header of their own.
Words to know
- PoP
- Point of presence: one location with our machines and network links.
- Origin
- The customer's server or storage with the original content.
- Origin shield
- A second cache that all PoPs ask before the origin.
- Anycast
- The same IP address announced from many places.
- Cache key
- The text that decides whether two requests get the same copy.
- Purge
- An order to stop using a copy before its time is up.
- Data plane
- The part that serves visitors.
- Control plane
- The part that customers configure.
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
Understand it
Picture it
A puppet show in travelling vans
The vans keep copies, the depot keeps the theatre calm, and the radio keeps the tapes up to date.
Scope
What are we building?
A general-purpose CDN, a service like Cloudflare or Akamai: customers put their site behind it, and visitors get pages, images, downloads and video from nearby. It is not a description of any company's real system.
Route
Send each visitor to a near and healthy PoP.
Cache
Answer from memory or disk, with the right key.
Purge
Remove a copy in 300 places within seconds.
Protect
Attacks and crowds stop before the origin.
Similar systems
| Service | Known for |
|---|---|
| Cloudflare | CDN, security and code at the edge |
| Akamai | Large enterprise CDN |
| Amazon CloudFront | Part of AWS |
| Fastly | Programmable edge, tag purges |
| Azure Front Door, Google Cloud CDN | Part of their clouds |
| Netlify, Vercel | Site hosting with an edge network |
| Bunny CDN, KeyCDN | Simple pay-as-you-go CDNs |
The finished system
Where the design ends up
The whole system
Read next
Who owns which idea
| Idea | Its own page |
|---|---|
| Origin, PoP, hits and misses | Introduction to CDN |
| Tiers, pull and push, GeoDNS, anycast, keys | CDN design |
| TTL, hard and soft purge, tags | CDN invalidation |
| Metrics, warming, security headers | CDN operations |
| One server in a PoP per key | Consistent hashing |
| Crowds on an expired copy | Thundering herd |
| Names and addresses | How DNS works |
For the interview go to Approach, then Scope, Numbers, API, Design and Deep dives. If you are revising, start with the Mock.
Summary
The whole topic in one minute.
A CDN is a hierarchy of caches with three jobs around it: send each visitor to a good place, protect the origin, and change everything quickly when a customer asks. Caches are easy to lose, so the data path can keep running when almost anything else is down.
Our design grows from one server to one proxy, to 300 PoPs, to a shield. A control plane pushes settings and purges outwards, and logs flow back in small summaries. Each part is forced by a number or a failure.
- 50M requests a second at peak is 40 Tb/s: network, not CPU, decides the number of servers.
- 90% hits at the edge and 80% at the shield leave 2% for origins.
- A wrong cache key shows one visitor another's page. A wide one destroys the hit rate.
- A purge is a message in a log: places that were away must read it before they serve.
- Data plane and control plane fail separately.