How does a CDN work and what does it improve?
A CDN keeps copies of your content on servers near users around the world. A user is routed to the closest edge, which serves cached content quickly and only contacts your origin server on a miss.
Explain it simply
If every customer of a famous sweet shop in Kolkata had to travel there, delivery would be slow. Opening branches in every city solves it: each branch stocks the popular sweets and only calls the main kitchen for rare orders. A CDN (content delivery network) is those branches for your website.
Flow:
- User requests an image; DNS or anycast sends them to the nearest edge server.
- Edge has the file: it returns it immediately (cache hit).
- Edge lacks it: it fetches from your origin, stores a copy per the cache headers and returns it.
Benefits:
- Lower latency because the distance is shorter.
- Less load on the origin since most requests never reach it.
- Better resilience to traffic spikes and some DDoS attacks.
- TLS termination and compression at the edge.
It works best for static files: images, JS, CSS, video. Dynamic, personalised responses are harder to cache.
Example
Image served to a user in Chennai, origin in Virginia.
First user: edge Chennai -> miss -> fetch from origin (about 250 ms)
Next users: edge Chennai -> hit -> served in about 20 ms
Cache-Control: public, max-age=86400 (edge keeps it for 1 day)What to say in the interview
"A CDN replicates cacheable content to edge servers near users, serving hits locally and fetching from the origin on misses. It cuts latency and origin load and absorbs spikes. I control freshness with Cache-Control headers and versioned filenames."