Design a CDN
A Content Delivery Network caches static assets at edge locations close to users, cutting latency and offloading the origin. The design covers request routing to the nearest edge, a caching hierarchy, and invalidation.
Infrastructure, 8 components, about 35 min
1. Scope
In scope
- • Route users to nearest edge
- • Cache static assets at the edge
- • Origin offload + shielding
- • Cache invalidation
Out of scope
- • Dynamic content acceleration deep-dive
- • Video packaging
- • DDoS scrubbing internals
2. Functional requirements
Must have
- • Serve assets from a nearby edge
- • Fetch from origin on miss
- • Invalidate/refresh content
- • Protect origin from herds
Nice to have
- • TLS at edge
- • Compression/image optimization
- • WAF at edge
3. Non-functional requirements
4. Back-of-envelope estimation
Assumptions
- • Global user base
- • Asset ~100 KB average
- • High hit ratio for static content
- • PoPs in many regions
Derived numbers
5. API design
Served from edge cache; origin-pull on miss
Invalidate (or use versioned URLs instead)
6. High-level design
What each box in the reference diagram is responsible for:
7. Data model
8. Deep dives
Request routing
Two common approaches: GeoDNS returns the IP of the nearest PoP based on the resolver's location; Anycast advertises the same IP from many PoPs and lets BGP route to the nearest. Anycast handles failover gracefully (routes shift automatically) but gives less control; GeoDNS is simpler and more precise but slower to react to outages.
Cache hierarchy & origin shielding
Edge → regional shield → origin. On an edge miss, instead of every edge hitting the origin (a thundering herd for a newly popular asset), edges in a region pull through a single shield cache. Only the shield contacts the origin, collapsing many concurrent misses into one origin fetch.
Invalidation via versioned URLs
Purging caches globally is slow and racy. The cleaner pattern: make asset URLs content-addressed (app.a1b2c3.js). New content = new URL, so caches never serve stale bytes and you never purge — old URLs simply age out. HTML (which references the hashed assets) gets a short TTL.
What to cache and for how long
Immutable hashed assets cache for a long time (hours/days). Frequently changing resources use short TTLs or revalidation (ETag/If-None-Match). Set Cache-Control deliberately per asset class.
9. Bottlenecks
- Cold-asset herds → origin shielding
- Stale content → versioned URLs
- Edge memory → LRU eviction, tiered SSD
10. Trade-offs
Routing users to an edge
Choose: Anycast: the same IP announced from every PoP, BGP routes users to a nearby one.
Instead of: DNS-based geo routing: finer control per user, but depends on resolver location and DNS TTLs for failover.
Anycast fails over in seconds when a PoP withdraws its route; many CDNs combine both.
Cache tiers
Choose: Edge caches backed by regional shield caches in front of origin.
Instead of: Edges pull directly from origin: simpler, but each edge miss hits origin.
The shield turns N edge misses into one origin request.
Invalidation
Choose: Versioned URLs (hash in the file name) for static assets, so no purge is needed; purge APIs for the rest.
Instead of: Purge everything on deploy: slow and leaves windows of mixed versions.
Immutable URLs can be cached for a year safely.
11. Failure modes
Origin down
Misses fail.
Mitigation: Serve stale content (stale-if-error) and keep popular objects longer.
Cache key includes a high-cardinality header
Hit ratio collapses.
Mitigation: Normalize cache keys (strip tracking query parameters, limit Vary).
DDoS on one PoP
Users near it degrade.
Mitigation: Anycast spreads attack traffic; rate limiting and scrubbing at the edge.
12. What interviewers probe
Answer each one yourself before opening it.
How do you cache personalized pages?Show a strong answer
Cache the shell and fetch personal fragments separately, or use edge compute to assemble; never cache responses with Set-Cookie for everyone.
What is the hit ratio and how do you raise it?Show a strong answer
Measured per byte and per request; raise it with longer TTLs on immutable assets, a shield tier, and key normalization.
How does TLS work at the edge?Show a strong answer
Certificates provisioned to edges (often automatically), TLS terminated at the edge, and re-encrypted to origin.
How quickly can you purge?Show a strong answer
Large CDNs propagate purges globally in seconds using a fan-out messaging layer.
Sign in to track this guide in your curriculum progress.