The physics problem CDNs solve
Your server is in Mumbai, your user is in New York: every round trip costs ~250 ms no matter how fast your code is. You cannot beat the speed of light, so you move the content closer instead. A CDN caches your content on hundreds of edge locations (Points of Presence) worldwide and serves each user from the nearest one. Latency drops from ~250 ms to ~20-50 ms.
How a request flows
- User requests an image; DNS (Anycast or geo-DNS) routes them to the nearest edge.
- Cache hit: edge serves it immediately. Done.
- Cache miss: edge fetches from your origin, caches it per the TTL, serves it. Every later user in that region gets a hit.
The edge is a shared cache for a whole region. The first request in a region is slow; everything after is fast.
What belongs on a CDN (and what does not)
- Static assets: images, videos, JS/CSS bundles, fonts, PDFs. The obvious 90% case.
- Video streaming: segmented video (HLS/DASH) is the bulk of internet traffic; Netflix's Open Connect and YouTube's edge caches exist precisely because origin-serving video is impossible at that scale.
- Dynamic content, increasingly: API responses with short TTLs can be cached at the edge, and edge compute (Cloudflare Workers, Lambda@Edge) can run logic at the edge itself.
- Not: personalized, per-user, fast-changing data on a shared cache key. Serving user A's inbox to user B because both hit /inbox is a real, catastrophic bug class: cache keys must include what varies.
Controlling the cache: headers and versioned URLs
Cache-Control: max-age=86400: cache for a day.no-cache: revalidate every time.private: browser only, never shared caches.ETag/If-None-Match: edge revalidates cheaply ("changed? no? serve the copy").- Versioned URLs: when a JS bundle changes, its content hash becomes part of the URL (app.a3f9c.js), so you can cache assets forever (max-age=1 year) and never serve stale code. New deploy, new URLs, instant cutover. This trick eliminates the hardest CDN problem.
- Purge/invalidation APIs exist for emergencies, but are slow (seconds to minutes to propagate globally) and unreliable as a design pillar.
Beyond latency: the other three wins
- Origin offload: 90%+ of traffic terminates at the edge; your servers mostly handle the misses and dynamic APIs. Smaller fleet, smaller bill.
- DDoS absorption: attack traffic hits the provider's planet-scale edge network instead of your three servers.
- Availability: if the origin blips, cached content keeps the site alive (stale-if-error).
Worked example: image-heavy app, global users
Uploads go to object storage (S3). CloudFront in front, TTL 1 day for user images, versioned URLs for app assets (TTL 1 year). Origin shield enabled (one extra cache layer near the origin so 500 edges do not each miss independently). Result: ~95% of image traffic never touches the origin.
Common interview traps
- Any image/video design without a CDN.
- Personalized responses cached under a shared key.
- Relying on purge for correctness instead of TTLs and versioned URLs.
Interview Q&A
Q: Users in India see 2-second image loads; the bucket is in us-east-1. Fix?
A: Put a CDN in front; Indian edge PoPs serve cached images in tens of ms. Also verify image sizes (compression, modern formats, responsive sizes): a CDN does not fix 8 MB images.
Q: How do you update a cached asset instantly?
A: You do not "update" it: you publish it under a new versioned URL and reference the new URL. The old object simply ages out. That is why build pipelines fingerprint assets.
Wo physics problem jo CDN solve karta hai
Server Mumbai mein hai, user New York mein: har round trip ~250 ms lagega, code kitna bhi fast ho. Speed of light ko nahi hara sakte, to content ko user ke paas le jaate hain. CDN tumhara content duniya bhar ki saikdon edge locations (PoPs) par cache karta hai aur har user ko sabse nazdeek wali se serve karta hai. Latency ~250 ms se ghatak ~20-50 ms.
Request ka flow
- User image maangta hai; DNS (Anycast ya geo-DNS) usse sabse nazdeek edge par bhejta hai.
- Cache hit: edge turant serve karta hai. Khatam.
- Cache miss: edge tumhare origin se laata hai, TTL ke hisaab se cache karta hai, serve karta hai. Us region ke baad ke sab users ko hit milega.
Edge poore region ka shared cache hai. Region ki pehli request slow hoti hai; baaki sab fast.
CDN par kya jaata hai (aur kya nahi)
- Static assets: images, videos, JS/CSS bundles, fonts, PDFs. Obvious 90% case.
- Video streaming: segmented video (HLS/DASH) internet traffic ka sabse bada hissa hai; Netflix ka Open Connect aur YouTube ke edge caches isliye exist karte hain kyunki origin se video serve karna us scale par impossible hai.
- Dynamic content, badhta hua: short TTL wale API responses bhi edge par cache ho sakte hain, aur edge compute (Cloudflare Workers, Lambda@Edge) logic bhi edge par chala sakta hai.
- Nahi: personalized, per-user, fast-changing data shared cache key par. User A ka inbox user B ko dikh gaya kyunki dono ne /inbox hit kiya: ye asli, khatarnak bug class hai. Cache keys mein wo cheez honi chahiye jo vary karti hai.
Cache control: headers aur versioned URLs
Cache-Control: max-age=86400: ek din cache.no-cache: har baar revalidate.private: sirf browser, shared caches kabhi nahi.ETag/If-None-Match: edge saste mein revalidate karta hai ("change hua? nahi? copy serve karo").- Versioned URLs: JS bundle badla to uska content hash URL mein aa jaata hai (app.a3f9c.js), to assets ko forever cache karo (max-age=1 saal) aur stale code kabhi mat serve karo. Naya deploy, naye URLs, instant cutover. Ye trick CDN ki sabse mushkil problem khatam karti hai.
- Purge/invalidation APIs emergencies ke liye hain, par slow hain (globally propagate hone mein seconds se minutes) aur design pillar banane layak reliable nahi.
Latency ke aage: baaki teen wins
- Origin offload: 90%+ traffic edge par hi khatam; servers mostly misses aur dynamic APIs handle karte hain. Chhota fleet, chhota bill.
- DDoS absorption: attack traffic provider ke planet-scale edge network par lagta hai, tumhare teen servers par nahi.
- Availability: origin blip ho to cached content site ko zinda rakhta hai (stale-if-error).
Worked example: image-heavy app, global users
Uploads object storage (S3) mein. Aage CloudFront, user images ka TTL 1 din, app assets ke versioned URLs (TTL 1 saal). Origin shield on (origin ke paas ek extra cache layer taaki 500 edges alag alag miss na karein). Result: ~95% image traffic origin tak pahunchta hi nahi.
Common interview traps
- Image/video design bina CDN ke.
- Personalized responses ko shared key par cache karna.
- Correctness ke liye purge par depend karna, TTLs aur versioned URLs ki jagah.
Interview Q&A
Q: India ke users ko images 2 second mein load ho rahi hain; bucket us-east-1 mein hai. Fix?
A: Aage CDN lagao; Indian edge PoPs cached images tens of ms mein denge. Saath mein image sizes bhi check karo (compression, modern formats, responsive sizes): CDN 8 MB images fix nahi karta.
Q: Cached asset ko instantly update kaise karte ho?
A: "Update" nahi karte: naye versioned URL ke saath publish karte ho aur naya URL reference karte ho. Purana object apne aap age out ho jaata hai. Isliye build pipelines assets ko fingerprint karti hain.