Module 04 // PerformanceLesson 15 / 3414 min read
CDNs
A content delivery network keeps copies of your files on servers around the world, so users download them from somewhere nearby instead of from your one data center.
Think of it like this
Analogy
A popular bakery in one city starts shipping cakes nationwide, and deliveries take days. So it opens small shops in every city that stock the most popular cakes. Most customers pick one up from the shop around the corner. If the local shop doesn't have the cake you want, it orders one from the main bakery and keeps a few for the next customer.
The problem it solves
Physics sets a floor on latency. A round trip from Sydney to a server in Virginia takes about 200 ms before any work happens, and a page needs dozens of files. On top of that, every image and script costs your servers bandwidth and capacity. A traffic spike from a viral post can swamp your origin with requests for the same few files.
A real example
Example
A game update on launch day
A game studio ships a 20 GB update to 5 million players at the same moment. Their own servers could never send that much data.
The files go out through a CDN. The first player in each city who asks for a file causes the nearby edge to fetch it once; everyone after that gets the edge's copy. An origin shield, a middle cache layer, means hundreds of edges ask the shield, not the studio's servers.
Each file name includes a version, like patch-1.4.2-part07.bin, so files can be cached for a year without ever going stale. When one file turns out to be corrupt, the team purges just that path, and the edges fetch a fixed copy.
How it works
A CDN runs hundreds of edge locations around the world. DNS or anycast routing sends each user to a nearby edge. The edge serves cached files; on a miss it fetches from your origin (your servers or an object storage bucket), keeps a copy and returns it.
- Users in Europe to Edge: Frankfurt
- Users in Asia to Edge: Singapore
- Edge: Frankfurt to Origin: miss only
- Edge: Singapore to Origin: miss only
What decides what gets cached
Your origin controls caching with HTTP headers:
Cache-Control: public, max-age=31536000, immutable # app.3f9a1c.js: cache for a year
Cache-Control: public, max-age=60 # homepage HTML: one minute
Cache-Control: private, no-store # account page: never cache at the edgeThe CDN also builds a cache key, usually the URL. Including unnecessary query strings or cookies in the key splits the cache and kills the hit rate.
Getting new content out
- Versioned file names (best): build tools put a content hash in the name, so a new version is a new URL. Cache forever.
- Short TTLs for HTML and API responses that change.
- Purge specific paths when you must remove something immediately. Avoid purging everything, which floods the origin.
More than static files
Modern CDNs also:
- End TLS near the user, saving round trips even for uncached requests.
- Use fast private networks back to your origin.
- Cache API responses briefly (even one second absorbs huge spikes).
- Stream video in small segments (lesson 29).
- Run a web application firewall, block DDoS attacks and rate-limit at the edge (lesson 16).
- Run small functions at the edge for redirects, A/B tests or auth checks.
Origin shield
Without help, every edge that misses asks the origin. An origin shield is a middle cache layer: edges ask the shield, and only the shield asks the origin, so one popular new file costs one origin request instead of hundreds.
Trade-offs
What you gain
- Much lower latency for users far from your servers.
- Offloads most bandwidth and requests from your origin; absorbs spikes.
- Adds DDoS protection, TLS near users and edge security for free or cheaply.
What it costs you
- Stale content if TTLs or invalidation are wrong.
- Another layer to configure and debug (“is it the CDN or my server?”).
- Costs scale with traffic, and personalized content can't be cached.
When not to use it
- Your users are all near one data center and traffic is small; the gain is modest.
- Content is entirely private and per-user, and you don't need edge security; there's little to cache.
In the cloud
Content delivery networkEvery major cloud sells this as a managed service, so you configure it rather than build it.
AWS1
- Amazon CloudFront (opens vendor docs)
Global CDN that caches content at edge locations close to users.
Azure1
- Azure Front Door (opens vendor docs)
Microsoft's global CDN and Layer 7 entry point; replaces the retiring Azure CDN (classic).
Google Cloud2
- Cloud CDN (opens vendor docs)
Caches content from Google Cloud load balancers at Google's edge locations.
- Media CDN (opens vendor docs)
Google's CDN built for large video and download traffic.
Static files live in object storage; the CDN caches them at the edge and only asks the origin on a miss.
- Route 53
- CloudFront
- S3 bucket (origin)
Interview questions
Try answering out loud first, then open the model answer.
How does a CDN decide which edge serves a user?
Model answer
Usually through DNS-based routing or anycast. With DNS, the CDN's name servers return the IP of an edge close to the user's resolver, based on location and edge health. With anycast, many edges announce the same IP address and the internet's routing delivers packets to the nearest one. Either way the user connects to a nearby edge without knowing it.
How do you deploy a new front-end version without users seeing stale files?
Model answer
Build assets with content-hashed file names and cache them for a long time with immutable. Serve the HTML that references them with a short TTL or no-cache plus revalidation. Deploy the new assets first, then the new HTML, so the HTML never points to files that don't exist yet, and keep old assets around for a while for users mid-session.
A viral page sends 50× normal traffic. How does a CDN help, and what else do you need?
Model answer
If the page and its assets are cacheable, even for a few seconds, the CDN serves nearly all requests from the edge and the origin sees a tiny fraction. An origin shield and request collapsing (one fetch per object per edge) protect the origin further. For the dynamic parts, you still need caching, auto-scaling and rate limiting behind the CDN, and the page should degrade gracefully, for example showing cached counts instead of live ones.
Quiz
Pick an answer to see whether it’s right and why.
0 of 6 answered
Key terms
- CDN
- Content delivery network. Servers around the world that keep copies of files close to users.
- Edge location
- A CDN data center close to users that keeps cached copies of content.
- Origin
- The server or bucket that holds the original content a CDN copies from.
- Cache hit
- The requested data was found in the cache.
- TTL (time to live)
- How long a cached item stays valid before it expires and must be fetched again.
- Purge (invalidation)
- Telling a CDN to drop cached copies so the next request fetches a fresh version.
Progress is saved in this browser only.