← Back to System Design, page 1

system-design-0010 · Easy fresher

What is a CDN and how does it make a website faster? What content should and should not go through it?

A CDN is a network of edge servers in many locations that cache content close to users. Requests go to a nearby edge; on a hit it answers directly, on a miss it fetches from the origin and keeps a copy. It cuts latency, reduces origin load and absorbs traffic spikes. Static and public content fits best; personal or rapidly changing data needs care.

Full answer

How it works

  1. A user in Nairobi requests https://cdn.example.com/app.js.
  2. DNS or anycast routing sends the request to a nearby edge server (a point of presence, or PoP).
  3. Hit: the edge has a valid copy and returns it in a few tens of milliseconds.
  4. Miss: the edge fetches from the origin server (say, in Mumbai), returns it, and stores it according to cache headers.

Physical distance matters because light in fibre still takes time. A round trip between continents commonly takes 100 to 300 ms, and a page load needs several round trips (DNS, TCP, TLS, request). Serving from a nearby edge removes most of that.

Controlling caching

The origin tells the CDN what to do with HTTP headers:

Cache-Control: public, max-age=31536000, immutable

for a file like app.3f9a2c.js whose name contains a content hash. A new build makes a new file name, so the old one can be cached for a year safely.

Cache-Control: public, max-age=60, stale-while-revalidate=30

for a course catalogue page that may be one minute old.

Cache-Control: private, no-store

for a student's personal dashboard.

To remove content early, CDNs offer purge (by URL or tag), but hashed file names are more reliable than purging.

What to put on a CDN

Good fit: images, video segments, JavaScript, CSS, fonts, downloadable PDFs, public HTML pages, public API responses that are the same for everyone.

Be careful with: anything personalised, logged-in pages, responses that depend on cookies. If you must, cache by a carefully chosen key (for example by language) and never cache responses that set or depend on a session.

Other benefits

CDNs also terminate TLS close to users, compress responses, absorb DDoS traffic, and keep serving cached content if the origin is briefly down.

By level

Fresher: Explain edge servers, hits and misses, and give examples of static content that should be cached. A fresher should also name one kind of content that must not be cached.

Mid: Explain Cache-Control directives, hashed file names, purge, cache keys and the danger of caching personalised responses. Expect a correct Cache-Control header for a hashed asset.

Senior: Discuss origin shielding, multi-CDN strategies, edge compute, cache key design for API responses, cost of egress, and how CDN behaviour affects incident response and rollbacks. The senior answer connects CDN choices to cost and incident handling.

Key points

  • Edge servers cache content near users; misses go to the origin
  • Distance and round trips dominate latency for far-away users
  • Cache-Control headers decide what is cached and for how long
  • Content-hashed file names make long caching safe
  • Never cache personalised responses publicly

Common mistakes

  • Caching pages that contain user-specific data
  • Relying on purge instead of versioned asset names
  • Forgetting that a CDN only helps if cache headers allow caching
  • Thinking a CDN is only for images

Follow-ups

  • How would you cache an API response that differs by language?
  • What is origin shielding?
  • How would you roll back a bad JavaScript file already cached at the edge?

Tips

  • Walk through hit and miss paths in order
  • Show one real Cache-Control header
  • Name one thing you would never put on a shared CDN cache

Evaluation rubric

  • Explains edge, hit, miss and origin clearly
  • Connects benefits to latency and origin load
  • Mentions cache control and versioning
  • Identifies unsafe content to cache