Caching & CDN· 15 min

Content Delivery Network

Edge locations cache content close to users

CachingFundamental15 min read

A geographically distributed network of servers that caches static content close to users.

Content Delivery Network (CDN) — diagram
🖱️ Drag nodes · Scroll to zoom · Drag background to pan
requestrequestrequestMISSMISSMISSUserMumbaiUserLondonUserNYCEdge NodeMumbai · 5msEdge NodeLondon · 8msEdge NodeNYC · 12msOrigin ServerUS Data Center

High-level design

User in India → CDN edge (Mumbai) → [cache hit: return; miss: fetch from origin in US]. Edge servers cache images, videos, CSS/JS. Reduces latency for global users and offloads traffic from the origin.

Low-level design

Pull model: edge fetches from origin on first miss, caches with TTL. Push model: origin pushes content to all edges. Cache invalidation: API purge, versioned URLs. Origin shield: a middle cache layer to protect origin from edge misses.

Trade-offs

  • →CDN is fast for static but useless for dynamic per-user content
  • →Long TTLs improve hit rate but serve stale content on deploy
  • →More edge locations = lower latency but higher cost

Used in production by

Cloudflare, AWS CloudFront, Akamai, Fastly

Questions to ask your interviewer

  • →What % of traffic is static vs dynamic?
  • →How quickly must deploys propagate?
  • →What's the origin's max QPS?

System design deep dive

Content Delivery Network (CDN): deep dive

A CDN serves your content from servers a few milliseconds away from each user instead of from one distant origin, which makes pages faster and absorbs traffic spikes your origin could never handle. Interviews probe how caching keys, TTLs, and invalidation work at the edge.

How a CDN request works, step by step

  1. A user in Bengaluru requests https://cdn.example.com/img/hero-a81f.webp.
  2. DNS for the CDN hostname (via anycast or geo-aware resolution) returns the address of a nearby point of presence (PoP), such as Chennai or Mumbai.
  3. The edge server computes a cache key, usually the host plus path plus any query parameters and headers configured as significant.
  4. On a hit, the edge returns the object directly, often in 10 to 30 ms instead of the 200+ ms a round trip to a US origin would take.
  5. On a miss, the edge asks a regional mid-tier or origin shield. Only if the shield also misses does a request reach the origin.
  6. The origin replies with Cache-Control: public, max-age=31536000, immutable; the edge and shield store the object and serve it to every later user in that region.

Beyond latency, the CDN is a bandwidth shield. If a 2 MB video thumbnail sprite is requested 50 million times a day with a 98% hit rate, the origin serves about 2 TB instead of 100 TB.

What to put on a CDN

  • Static assets: JS and CSS bundles, fonts, images. Use fingerprinted filenames so they can be cached for a year.
  • Media: video segments for HLS or DASH streaming, podcast files, downloads.
  • Cacheable API responses: public, non-personalized data such as a product listing, with short TTLs like 30 to 60 seconds.
  • Whole pages for logged-out users, with stale-while-revalidate to hide origin latency.
  • Not per-user responses (a cart, a feed) unless you are deliberately using edge compute; caching those risks serving one user's data to another.

Cache-control and invalidation strategies

TechniqueHow it worksBest for
Versioned URLsFile name includes a content hash; new deploy means new URLJS, CSS, images: instant, safe updates
Short TTLmax-age=60 so content refreshes every minuteSemi-dynamic JSON, homepages
stale-while-revalidateServe stale copy instantly while fetching a fresh oneHiding origin latency on popular pages
Purge APIExplicitly evict a path or tag from all PoPsTakedowns, urgent corrections
Surrogate keys / tagsTag objects, purge every object with a tag at onceInvalidating all pages that show product 42

When a CDN helps (and when it does not)

A CDN gives the biggest win when your users are geographically spread out and a large share of bytes is identical for everyone. It also helps security: many CDNs absorb DDoS floods and provide a web application firewall at the edge. It helps little for a single-region internal tool, for write-heavy APIs, or for highly personalized responses, though even then a CDN can still terminate TLS close to the user and keep a warm connection back to origin, saving a few round trips.

Common mistakes in interviews

  • Saying put it on a CDN without explaining how a deploy invalidates old content.
  • Including cookies or session headers in the cache key, which drops the hit rate to near zero.
  • Forgetting the origin shield and letting hundreds of PoPs each miss independently after a purge, hammering the origin.
  • Caching authenticated responses with public, leaking private data across users.
  • Not mentioning that a CDN is also a cost decision: egress from a CDN is often cheaper than egress from cloud origin servers.

Interview questions

›What is the difference between a push CDN and a pull CDN?

With a pull CDN, edges fetch content from the origin the first time it is requested and cache it, so setup is easy and only requested content is stored. With a push CDN, you upload content ahead of time, which suits large files that are known in advance, such as a game patch, but requires managing uploads and storage.

›How do you deploy a new frontend without users seeing a broken mix of old and new files?

Fingerprint every asset (app.3f9c1.js) and cache it for a year, while serving the HTML that references them with a short TTL or no-cache. A new deploy changes the HTML to point at new URLs, so old and new assets never collide.

›What is an origin shield and why use one?

It is a designated mid-tier cache between the edge PoPs and the origin. All edge misses go to the shield first, so the origin sees roughly one request per object per TTL instead of one per PoP. It dramatically reduces origin load after purges or for long-tail content.

›How does a CDN help with video streaming?

Videos are split into small segments of a few seconds, each at multiple bitrates, and every segment is a cacheable file. Popular titles get very high hit rates at the edge, and the player switches bitrates based on bandwidth while still fetching cached segments.

›Can a CDN serve dynamic content?

Partially. It can cache short-lived public responses, use edge functions to run logic near the user, and accelerate uncached requests through optimized routes and persistent connections to the origin. Truly per-user data is still produced by the origin.

More System Design Concepts

Compare this lesson with the other 13 concepts in the course. Expand any card for its design, trade-offs and diagram.

Category:
Difficulty:
Showing 13 of 13