CDN Deep Dive
How a CDN serves content from caches near users: the path through edge and shield, Cache-Control and cache keys, invalidation, dynamic content, security at the edge, and hit ratio and cost.
Overview
A content delivery network (CDN) is a set of servers in many cities that sits between users and your own servers. Each of these edge servers keeps copies of your responses. When a user asks for a file, the nearest edge answers from its copy if it has one, so the answer travels a few miles instead of across an ocean, and your servers, called the origin, never see the request.
The Fundamentals chapter showed the CDN as one box on the path of a request. This chapter opens the box. It explains how a request finds an edge, how the cache decides what it may store and for how long, how you replace old content, why a CDN speeds up even responses it cannot cache, how it protects the origin, and how to tell whether it is working.
Most of the rules come from the HTTP standard, mainly RFC 9111 (HTTP caching). CDNs build on it and add their own defaults and extensions, so the examples name CloudFront, Cloudflare and Fastly where their documentation states the behavior.
What you will be able to do
- Trace one request through the edge, the shield and the origin, on a hit and on a miss.
- Write the Cache-Control header for a hashed script, an HTML page, a shared API response and a private page, and explain each directive.
- Choose between versioned file names, short lifetimes and purges when content changes.
- Explain how a CDN speeds up responses it cannot cache, and which code belongs at the edge.
- Lock an origin behind its CDN, and estimate hit ratio, origin load and egress cost.
Key concepts
- Edge, shield and origin
- Request collapsing
- Cache-Control
- Serving stale
- Cache keys and Vary
- Purges and cache tags
- Versioned file names
- Edge compute
- Origin protection
- Hit ratio and egress
Interview questions
- Walk through what happens when a user requests an image through a CDN.
- What is the difference between no-cache, no-store and private?
- You deploy a new front-end version. How do users get it at once, without purging the CDN?
- A product's price changed, and the old price must disappear from every edge within seconds. How?
- Your CDN's cache hit ratio is 60%. How do you find the cause and raise it?
- What happens to users when the origin goes down, and how do you design for it?
- How do you stop attackers from going around the CDN to the origin?
- How would you serve paid videos through a CDN so that only subscribers can watch?
- Can a CDN help an API whose responses cannot be cached?
- Estimate the monthly CDN bill and the origin load for a site that serves 200 TB and 2 billion requests a month.
Sign up free to read the full topic
More System Design topics
Start free on Cariara · Coding problems · System design studio