Design a CDN
Understanding the Problem
🌍 What is a CDN? A content delivery network is a fleet of caching servers spread across hundreds of locations worldwide, called points of presence (PoPs). A website points its domain at the CDN, and the CDN answers users from a PoP near them, fetching from the site's own servers (the origin) only when it doesn't already have a copy. Cloudflare, Akamai, Fastly and CloudFront are the well-known examples.
A CDN is a reverse-proxy cache at planetary scale. The interesting parts are not the cache itself but everything around it: getting a user in Tokyo to a Tokyo server rather than one in Virginia, keeping hundreds of independent caches from all hammering the same origin, and making a "delete this file" command reach every one of them quickly.
Functional Requirements
We'll scope this with our interviewer as the CDN provider: we're building the platform that many customer websites sit behind.
Core Requirements
- Users should be able to fetch a customer's content (images, scripts, video segments, API responses marked cacheable) from an edge location near them.
- Edge servers should serve cached content when they have a fresh copy and fetch from the customer's origin when they don't.
- Customers should be able to configure a distribution: their domain, their origin, and caching rules (TTLs, which headers and query parameters matter).
- Customers should be able to purge content, by URL or by tag, and have it stop being served everywhere quickly.
Below the line (out of scope)
- Billing, usage dashboards and log delivery (they read the same access logs, but they're a separate pipeline).
- Edge compute (running customer code at the PoP, like Workers or Lambda@Edge).
- A full web application firewall. We'll touch on DDoS only where it shapes routing.
Non-Functional Requirements
Ask your interviewer for the scale. We'll assume a large, general-purpose CDN: 250 PoPs, 20 million requests per second at global peak, and an average response of about 100 KB.
Core Requirements
- The system should serve cache hits with low latency, on the order of tens of milliseconds time to first byte for most users, because the PoP is close to them.
- The system should achieve a high cache hit ratio, 95%+ by requests, to keep load off customers' origins.
- The system should be highly available (99.99%+). Losing a server, or a whole PoP, should shift traffic elsewhere without customers noticing.
- Purges should take effect globally within a few seconds.
- Configuration changes should reach every PoP within a minute or so.
Below the line (out of scope)
- Strong consistency between PoPs. Two users in different cities may briefly see different versions of an object, and that's accepted.
- Live video origination and transcoding. We cache the segments; producing them is someone else's problem.
Do the math, because two numbers fall out that shape the design.
Bandwidth: 20M requests/s × 100 KB = 2 TB/s, or 16 Tbps of egress. Spread over 250 PoPs that's 64 Gbps on average, but traffic is never even: a PoP in Frankfurt or Ashburn might carry ten times that. A single modern cache server with a few 100 Gbps NICs can push somewhere in the tens of Gbps of real traffic, so a large PoP is a few dozen to a few hundred servers.
Origin load: at a 95% hit ratio, 5% of 20M is 1M requests/s going back to origins. That sounds fine until you realize it's 1M/s summed over all customers, and that every PoP misses independently. If a customer publishes a new image and it's requested in all 250 PoPs, that's 250 origin fetches for one object, per TTL. For a popular customer, the misses alone can melt a modest origin.
The second number is the one that drives the design. A CDN's job is as much about protecting origins as it is about serving users. When an interviewer asks "what does the origin see?", they're checking whether you've spotted that many independent caches multiply misses.