CDN vs Direct Serving
When to put a content delivery network in front of a response and when to serve it straight from your own servers: what the edge saves, what is safe to cache, how to keep it fresh, and the mistakes that leak or stale data.
Overview
A content delivery network (CDN) is a set of servers spread across many cities, called edges, that sit between users and your own servers, the origin. A user's request goes to a nearby edge. If the edge has a fresh copy of the response, it answers at once (a hit); if not (a miss), it fetches the response from the origin, returns it, and may keep a copy for the next user. Direct serving means the user's request travels all the way to your load balancer and servers every time.
A CDN buys three things: shorter distance for the user, fewer requests reaching your servers, and a large, spread-out front door that can absorb traffic spikes and attacks. It costs control over freshness. Anything cached at the edge can be out of date, and a mistake in what is cached can show one user another user's data.
So the real design question is not whether to use a CDN, since nearly every public site has one, but which responses go through its cache, for how long, and how you change them when the data changes.
What you will be able to do
- Explain where a CDN saves time, using round trips and distance.
- Work out origin load from traffic and hit ratio.
- Pick Cache-Control headers for static files, public pages, API responses and private data.
- Choose between versioned URLs, purges and short TTLs to keep content fresh.
- Say when serving directly is the better choice, and how to avoid caching personal data by accident.
Key concepts
- Edge and origin
- Round trips and distance
- Cache hit ratio
- Cache-Control directives
- Versioned URLs
- Purge and TTL
- stale-while-revalidate
- Cache key and Vary
- Signed URLs
Interview questions
- Why does a CDN make pages faster for distant users, even on a cache miss?
- What would you cache at the edge for an online store, and with what headers?
- Compare versioned URLs, purging and TTLs for keeping cached content fresh.
- How can a CDN leak one user's data to another, and how do you prevent it?
- Your origin handles 10,000 requests a second behind a CDN with a 90% hit ratio. How do you cut origin load?
- When would you not put a response through a CDN cache?
- How do you serve paid videos from a CDN without making them public?
- What does stale-while-revalidate do, and why is it useful?
Sign up free to read the full topic
More System Design topics
Start free on Cariara · Coding problems · System design studio