CDN (Content Delivery Network)

Serving content from a server near the user: edge locations, static vs dynamic content, and offloading the origin.

Beginner5 min read
Language
Theme
UserNew Yorknearest edgeCDN Edge (PoP)cached copymiss: fetch onceOrigin serverMumbaiCache-Control headers + versioned URLs (app.a3f9c.js) = no stale assetsBonus: origin offload, DDoS absorption, availability
Key idea at a glance

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

  1. User requests an image; DNS (Anycast or geo-DNS) routes them to the nearest edge.
  2. Cache hit: edge serves it immediately. Done.
  3. 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.

Key takeaways

Interview pointer

Any design serving images or video (Instagram, Netflix, a news site) is expected to mention a CDN. Add: 'asset URLs will be versioned so cache invalidation is never an issue.'