AlgoViz

Design a Content Delivery Network (CDN)

hard 45 min

Build 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

0:00 / 3:54

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

  1. Late 1990s

    The first content delivery networks appear, to relieve slow paths on the growing web.

  2. December 2006

    RFC 4786 describes how to run services on anycast addresses.

  3. May 2010

    RFC 5861 adds stale-while-revalidate and stale-if-error to HTTP caching.

  4. August 2017

    RFC 8216 publishes HTTP Live Streaming, video cut into small files that any HTTP cache can keep.

  5. 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.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

Understand it

Picture it

A puppet show in travelling vans

Played once, shown everywhere
6 hoursThe theatre is far away

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

ServiceKnown for
CloudflareCDN, security and code at the edge
AkamaiLarge enterprise CDN
Amazon CloudFrontPart of AWS
FastlyProgrammable edge, tag purges
Azure Front Door, Google Cloud CDNPart of their clouds
Netlify, VercelSite hosting with an edge network
Bunny CDN, KeyCDNSimple pay-as-you-go CDNs

The finished system

Where the design ends up

The whole system

Visitors reach the nearest PoP, where filters come before cache servers. Misses go to an origin shield and then to the customer's origin, which is outside our system. Below, the customer calls a management API, which writes the config store and the push log that reaches the PoPs. PoPs send summaries to a log stream and analytics.
Blue serves visitors, orange carries settings, purges and logs. Dashed is asynchronous or only sometimes.

Read next

Who owns which idea

IdeaIts own page
Origin, PoP, hits and missesIntroduction to CDN
Tiers, pull and push, GeoDNS, anycast, keysCDN design
TTL, hard and soft purge, tagsCDN invalidation
Metrics, warming, security headersCDN operations
One server in a PoP per keyConsistent hashing
Crowds on an expired copyThundering herd
Names and addressesHow 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.

Sources