Content Delivery Networks (CDN)

Basics

Visual Representation

Rendering diagram...

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:

  • First request (cache miss) — User in Delhi requests a photo. The CDN server in Delhi doesn't have it yet. It fetches from Instagram's origin server in the US, serves it to the user, and CACHES a copy locally.
  • Second request (cache hit) — Another Delhi user wants the SAME photo. CDN Delhi already has it. Serves instantly without going to the US. ⚡
  • Millions of requests — That viral meme? Cached on ALL CDN servers worldwide. Instagram's origin server handles ONE request. The CDN handles the other 10 million.
  • Cache expires (TTL) — After the configured time (say 24 hours), the cached copy is considered stale. Next request re-fetches from origin and refreshes the cache.
  • 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)

  • 🎯 "How would you serve images/videos globally?" → CDN is the answer
  • CDN shows up in: Design Instagram, Design Netflix, Design URL Shortener (redirects at edge)
  • Understanding pull vs push CDN models demonstrates depth
  • Interviewers love hearing about cache invalidation challenges with CDNs
  • Key Things to Remember

  • Pull CDN — CDN fetches from origin on first request (lazy loading). Simpler. Used for most websites. Cold start for first user in each region.
  • Push CDN — You pre-upload content to CDN. Used for predictable content (movie releases on Netflix). No cold start but requires more management.
  • TTL (Time To Live) — How long the CDN caches content. Short TTL = fresher but more origin hits. Long TTL = faster but might serve stale data.
  • Cache Invalidation — Need to update a cached image? You can: purge it from CDN, use versioned URLs (style.v2.css), or wait for TTL to expire.
  • Origin server — Your actual server where the original content lives. CDN always falls back to origin on cache miss.
  • Edge locations — The CDN servers spread globally. Cloudflare has 300+ edge locations. AWS CloudFront has 400+.
  • Reduced origin load — If 1M users view the same image, origin serves it ONCE to CDN. CDN serves 999,999 requests. Massive load reduction.
  • DDoS protection — CDNs absorb attack traffic at the edge before it reaches your origin. Their global network can handle massive traffic volumes.
  • HTTPS at edge — CDN terminates TLS. User connects to nearby CDN via HTTPS. CDN connects to origin (possibly via HTTP internally for speed).
  • Video streaming — Netflix's Open Connect places entire CDN appliances INSIDE ISP networks. Video literally comes from your ISP's building!
  • 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/5

    A user in Delhi loads an Instagram photo for the first time. Where does it come from?