System Design · Core
CDN
Edge caches that put static (and some dynamic) content near users: PoPs, Cache-Control, invalidation, and origin shield.
1. What a CDN is
A Content Delivery Network is a fleet of edge servers (points of presence) distributed worldwide. Users fetch assets from a nearby PoP instead of your origin in one region.
2. What to put on a CDN
- Images, CSS, JS, fonts, downloadable files.
- Video segments (HLS/DASH) with short or medium TTL.
- Sometimes cacheable API GETs (public, versioned, low personalization).
Do not blindly cache personalized HTML or authenticated API responses without Vary / cache keys that include the right dimensions.
3. Request flow
- DNS for
cdn.example.comreturns an edge Anycast/VIP near the user. - Edge looks up object by cache key (URL + selected headers).
- Hit → serve; Miss → pull from origin (or parent shield), store, serve.
- Respect TTL / revalidation (
ETag,If-None-Match).
4. Cache-Control and related headers
| Header / directive | Role |
|---|---|
Cache-Control: max-age=... | Freshness lifetime for shared/private caches |
public / private | CDN may store vs browser-only |
no-store / no-cache | Do not store / revalidate before use |
s-maxage | TTL specifically for shared caches (CDN) |
ETag / Last-Modified | Conditional revalidation |
Vary | Separate entries by header (e.g. Accept-Encoding) |
Fingerprint filenames so you can cache forever and deploy new bytes under a new URL. That beats emergency purges for routine releases.
5. Invalidation and purge
- Purge by URL: remove one object from edges (eventual across PoPs).
- Purge by tag/key: group related objects (product 42’s images).
- Soft purge / stale-while-revalidate: serve stale while refreshing.
Purge is not instant worldwide and can be expensive. Prefer versioned URLs; purge for mistakes and urgent takedowns.
6. Origin shield
A designated mid-tier cache collapses duplicate origin fetches, lowers origin load, and can improve cache hit ratio. Trade-off: extra hop on some misses.
7. Dynamic and security notes
- Signed URLs / cookies for private content (course videos, paid downloads).
- WAF and DDoS absorption often sit at the same edge.
- TLS terminates at edge; origin pull over HTTPS.
8. Interview placement
In almost every media-heavy design (YouTube, Drive, Instagram), draw CDN in front of object storage. State TTL strategy, versioning, and purge for bad assets. Mention origin shield if origin QPS is a concern.
9. Cache key design
The cache key is usually the full URL plus selected headers from
Vary. Query-string noise (utm_*) can fragment
the cache — strip tracking params at the edge when safe. Prefer
content-hashed paths for static assets so keys naturally change on deploy.
10. Measuring CDN health
- Edge hit ratio (bytes and requests).
- Origin bandwidth and 5xx from origin pulls.
- Time to first byte by geography.
- Purge latency and failure rate after incidents.
Quick revision
- CDN = edge PoPs caching content near users.
- Best for static and segment-based media; careful with personalized data.
- Cache-Control, ETag, Vary control freshness and variants.
- Fingerprint assets for long TTL; short TTL or revalidate for HTML.
- Purge for emergencies; versioning for routine deploys.
- Origin shield collapses miss stampedes onto origin.
- Signed URLs protect private objects at the edge.