Content Delivery Networks (CDN)
Basics
Visual Representation
What is it?
🏪 Think of it like Amazon warehouses:
Amazon doesn't ship everything from ONE giant warehouse in Delhi. They have small warehouses (fulfillment centers) in every city — Mumbai, Bangalore, Chennai. When you order, it ships from the NEAREST warehouse. Faster delivery.
A CDN (Content Delivery Network) is the same concept for data. Instead of serving your website from ONE server in the US, CDN copies your content (images, videos, CSS, JS) to HUNDREDS of servers worldwide. Users get content from the server nearest to them.
User in Mumbai → CDN server in Mumbai (5ms). Without CDN → origin server in Virginia, USA (150ms). That's 30x faster!
💡 Simple Summary: CDN = copies of your content placed on servers worldwide so users get data from nearby instead of far away.
How it works — Like you're watching it happen
When you load an Instagram photo:
❓ But wait — what about dynamic content (like your personalized feed)? Can CDNs cache that?
Traditional CDNs cache STATIC content (images, videos, CSS/JS files that don't change per user). Your personalized feed is dynamic (different for every user) and typically can't be cached at CDN. HOWEVER, modern "edge computing" CDNs (Cloudflare Workers, AWS Lambda@Edge) can run CODE at the edge — generating personalized responses close to the user. The line between CDN and compute is blurring.
Why should you care? (Interview perspective)
Key Things to Remember
Real Examples You Use Daily
📺 Netflix — Uses its own CDN (Open Connect). Entire servers are placed inside ISP data centers worldwide. When you stream a movie, it comes from a box literally inside Jio/Airtel's network. Virtually zero internet traversal.
📱 Instagram — Every photo/video you see on the feed is served from a CDN (Facebook/Meta CDN). The origin server stores the image once; CDN serves it to billions of viewers worldwide from local edge nodes.
🌐 Any website — When you visit any modern website, the CSS, JavaScript, fonts, and images almost always come from a CDN (Cloudflare, Akamai, AWS CloudFront). Open DevTools → Network tab → notice different domains for static assets.
🎮 Game updates — When a 50GB game update drops, game companies use CDNs. Without it, their ONE server would collapse under millions of simultaneous downloads. CDN distributes the load.
Common Mistakes in Interviews
❌ Thinking CDN only caches images — CDNs cache: HTML pages, CSS, JS, fonts, API responses (if cacheable), video segments, downloads. Some even run code (edge computing)!
❌ Not discussing cache invalidation — "We'll use a CDN" — but how do you update content? Discuss versioned URLs, cache purging, and TTL strategies.
❌ Forgetting cold starts — First user in a new region gets slow response (CDN fetches from origin). Discuss pre-warming/push CDN strategies for expected traffic (like a movie launch).
❌ Ignoring costs — CDN bandwidth isn't free. Serving petabytes of video per month costs real money. Mention cost considerations for high-bandwidth systems.
❌ Not connecting CDN to latency reduction — Don't just say "use a CDN." Say "CDN reduces latency from 150ms to 5ms by serving content from Mumbai instead of Virginia."
🎯 Interview One-Liner
"A CDN caches static content at edge locations worldwide to minimize latency — reducing origin load by serving requests locally — and for a media-heavy app like Instagram, I'd use a pull-based CDN with long TTLs for images and versioned URLs for cache invalidation."
Interview Q&A
Q: Pull CDN vs Push CDN — when to use each?
Pull CDN: content is fetched from origin on first request and cached. Best for: unpredictable traffic, large catalogs (e-commerce with millions of products). Push CDN: you pre-upload content to CDN before anyone requests it. Best for: predictable content (Netflix movie releases, planned event livestreams), large files where cold-start latency is unacceptable. Most companies use pull CDN for simplicity.
Q: How does a CDN know which edge server to direct a user to?
Two methods: (1) GeoDNS — the CDN's DNS resolves the same domain to different IPs based on the user's geographic location. User in India gets IP of Mumbai edge. (2) Anycast — same IP address is announced from multiple locations; internet routing naturally directs packets to the nearest one. Cloudflare uses Anycast; many CDNs use GeoDNS.
Q: What's cache invalidation in CDN and why is it hard?
You update a profile picture, but CDN still serves the OLD one (cached for 24 hours). Solutions: (1) Versioned URLs — change image URL when content changes (profile-pic-v2.jpg). Old URL still serves old version from cache; new URL fetches fresh. (2) Purge API — tell CDN to delete specific cached content. Takes time to propagate across 400+ edge nodes. (3) Short TTL — content expires quickly. More origin hits but fresher.
Q: How does Netflix achieve such fast streaming?
Netflix Open Connect places custom servers INSIDE ISP networks worldwide. When you stream, video bytes travel from a server physically in your ISP's building — not across the internet. They pre-load popular content (push CDN) during off-peak hours. New/unpopular content is pulled on demand. This is why Netflix accounts for ~15% of internet traffic without congesting backbone networks.
Q: CDN plus caching — how do they work together?
Multiple layers! Browser cache (seconds to hours) → CDN edge cache (hours to days) → Origin server cache (Redis/Memcached) → Database. A request only reaches the database if ALL caches miss. For a popular Instagram post: browser shows cached version for 5 minutes, CDN serves it for 24 hours after that, origin Redis serves it if CDN expires. Database is hit maybe once per million views.
Quick Quiz
1/5A user in Delhi loads an Instagram photo for the first time. Where does it come from?