A CDN (content delivery network) is a worldwide fleet of cache servers placed close to users. Instead of every user in Sydney, Mumbai and Sao Paulo fetching an image from your single server in Virginia, they fetch it from a nearby CDN server that already holds a copy.
Distance is a physical limit: light in fibre covers roughly 200 km per millisecond, so a round trip between India and the US East Coast costs 200 ms or more before your server does any work. A CDN beats that by moving the data, not by making the network faster.
Detailed Notes
What is it?
A CDN is a network of servers, called edge servers or PoPs (points of presence), spread across the world in many cities. Each PoP caches content from your origin, the server (or storage bucket) that holds the real copy. A user's request is routed to a nearby PoP. If the PoP has the file, it answers immediately (a cache hit). If not, it fetches the file from the origin, keeps a copy, and returns it (a cache miss).
Remember the caching lesson: the CDN is simply a cache, but geographically distributed and operated by a provider such as Amazon CloudFront, Cloudflare or Akamai.
What belongs on a CDN:
Static content: images, videos, JavaScript, CSS, fonts, downloads. The same bytes for every user.
Cacheable API responses: a public product listing or a trending list that can be 30 seconds old.
Not suitable: personalized or private responses (a user's inbox) unless the CDN can be told to key them per user, and anything that must be perfectly fresh.
Why do we need it?
Three benefits, each with a number:
Latency. A user 12,000 km from the origin pays about 150 to 250 ms round trip per request. A PoP in their city is 5 to 20 ms away. A web page needing 20 sequential round trips saves several seconds.
Origin load. If 95% of image requests are served at the edge, the origin handles 5%. At 100,000 requests/s that is 5,000/s instead of 100,000/s.
Bandwidth cost and resilience. Serving a 2 MB image 100 million times is 200 TB. Pushing that out of the origin is expensive, and a traffic spike or DDoS attack (a flood of requests meant to overwhelm you) is absorbed by the edge instead of your servers.
Video is the extreme case: a streaming service moves petabytes per day, which is only feasible because popular titles are already sitting in edge servers near viewers.
How does it work?
Routing the user to a nearby PoP
The user types cdn.example.com. DNS (the system that maps names to IP addresses, from the networking lesson) returns the address of a PoP chosen for that user, based on their location and the PoP's health. Two techniques do this: GeoDNS / latency-based DNS (the DNS answer differs per region) and anycast (many PoPs share one IP address and the network routes each packet to the nearest one).
Key point. Hits end at the edge. Only misses travel toward the origin, and the shield merges them.
Users in different regions hit different PoPs. Misses funnel through one shield so the origin is asked once.
The user requests /img/cat.jpg; DNS sends them to the nearest PoP.
Hit: the PoP has a fresh copy and returns it. Done in about 10 ms.
Miss: the PoP asks the next tier up. Many CDNs have an origin shield, a middle cache layer that sits in front of the origin. All PoPs miss to the shield, so the origin sees one request per file instead of one per PoP. This matters: with 200 PoPs, a new file could otherwise cause 200 simultaneous origin requests.
The origin returns the file with headers saying how long it may be cached. The PoP stores it and responds.
Pull versus push
Pull CDN versus Push CDNComparison
Pull CDN
fetched on first request
1
User asks the PoP
file not there yet
2
PoP fetches from origin
stores a copy with TTL
3
Later users hit the cache
until the TTL expires
No upload step
Stores only requested files
First user per region is slow
Origin sees every cold miss
Default for websites and images
Push CDN
you upload ahead of time
1
Publish file to the CDN
during your release
2
CDN copies it to PoPs
before any user asks
3
Every request is a hit
origin is not involved
No cold-start penalty
Origin can be tiny
You manage storage and cleanup
Wasted copies if unpopular
Game patches, big launches, video files
Key point. Pull is the low-effort default. Push fits large, predictable releases.
The difference is who puts the file on the edge and when.
Pull CDN: the CDN fetches from the origin on the first miss. Simple, and storage is used only for content people actually request. The first user in each region pays the miss penalty. Best default.
Push CDN: you upload content to the CDN ahead of time, for example a new game patch before launch. There is no first-request penalty and the origin is not hit at all, but you manage what is stored and when it is removed. Best for large, predictable, rarely-changing files.
Controlling freshness
The origin tells the CDN how long to cache using HTTP headers:
max-age=86400 means "cache for 86,400 seconds (one day)".
public means shared caches such as a CDN may store it; private means only the user's browser may.
An ETag is a version tag. When a copy expires, the PoP can ask the origin "do you still have version v42?" and receive a tiny "304 Not Modified" instead of the whole file.
Invalidation (removing a file before its TTL ends) is slow and can cost money, since the command must reach hundreds of PoPs. The professional trick is versioned URLs: name files app.9f3a1c.js or /v42/logo.png. A new version has a new URL, so the old cached copy is never wrong and you can set a one-year TTL. Reserve explicit invalidation for emergencies, such as removing an illegal file.
Other things a CDN does
TLS termination: the CDN handles the HTTPS handshake near the user, saving several round trips of setup time.
Compression and image resizing at the edge.
Protection: rate limiting, web application firewall rules and DDoS absorption.
Signed URLs: a time-limited, tamper-proof link so only paying users can fetch a private video.
Interview tip. Do not say "put everything behind a CDN". Say which content (static, public, cacheable), what the TTL is, and how you update it (versioned URLs). Also remember the CDN is only useful when many users request the same object; unique, personalized responses get no hits.
Real-world example
A news site publishes a breaking story. Its images and CSS use versioned URLs with one-year TTLs. The HTML article page is cached at the edge for 30 seconds. When the editor fixes a typo, the change appears within 30 seconds worldwide without any invalidation call. If the site is hit by a 20x traffic spike, almost all of it lands on PoPs, not on the origin servers.
System design example
You design a photo-sharing app with 500 million image views per day and 200 KB per thumbnail.
text
500M views / 100,000 s = 5,000 requests/s average
5,000 x 3 peak = 15,000 requests/s peak
15,000 x 200 KB = 3 GB/s (24 Gbps) at peak
An origin serving 24 Gbps is expensive and far from half the planet. With a 95% edge hit rate the origin serves 5%: 750 requests/s and 150 MB/s. Design: images are stored in object storage (the blob storage lesson), served through a pull CDN with an origin shield, uploaded under content-hash names (/img/9f3a1c.jpg) so they never change and cache for a year. Page HTML and personalized data come from the application servers, not the CDN.
Interview discussion. "Static assets and thumbnails go through a pull CDN with a long TTL and content-hashed URLs, so I never need to invalidate. I'd add an origin shield so a cold file causes one origin fetch rather than one per region, and I'd watch the edge hit rate: with 95% the origin only sees 5% of the traffic."
Trade-off thinking. Long TTLs raise the hit rate and cut cost but make updates slow to appear; versioned URLs remove that conflict for static files. Push gives control and no cold-start penalty but needs a deployment step; pull is hands-off but the first request per region is slow. CDNs also cost money per GB, so cache only what is reused.
Key interview takeaway
A CDN is a geographically distributed cache for content many users share. Use it for static and public content, choose pull by default, set TTLs with Cache-Control, and avoid invalidation through versioned URLs. Quantify the win: lower latency from shorter distance, and the origin handling only the miss traffic.
Real World Example
A news site serves images and CSS with versioned URLs and one-year TTLs, and caches the HTML article for 30 seconds at the edge. A typo fix appears worldwide within 30 seconds with no invalidation, and a 20x traffic spike lands on PoPs rather than the origin.