Part 3 · The Building Blocks

How CDNs Make the Web Fast

Your servers are in Mumbai and a user in São Paulo waits three seconds. How CDNs copy content to edges near every user, how HTTP headers control what gets cached, and how to update files that live on hundreds of servers.

Lesson 148 min readbeginner#cdn#performance

On this page

  1. The Problem
  2. The Core Idea
  3. How It Works
  4. Trade-offs
  5. In the Real World
  6. Interview Questions
  7. Key Takeaways
  8. Further Reading

The Problem

Your servers are in Mumbai. A user in Mumbai loads your homepage in 300 ms. A user in São Paulo takes 3 seconds. Each image, script and stylesheet crosses the planet, and round trips there take over 300 ms.

Then your product goes viral. Traffic jumps 50×, mostly people downloading the same homepage images, and your servers melt.

A CDN (Content Delivery Network) solves both problems. It brings content close to users, and it takes most of the load off your servers.

In one sentence: a CDN is a group of servers spread around the world that keep copies of your files, so each user downloads them from a server nearby. Latency is the time a request takes to get an answer. Origin is your own server, where the original files live.

The Core Idea

Think about how a popular biscuit brand reaches shops. The factory doesn't ship one packet at a time from its single factory to every corner shop in the country. Instead, it sends big batches to regional warehouses, and each shop gets stock from the nearest warehouse. It's faster for customers and far less work for the factory.

A CDN is a worldwide network of warehouses for your content:

  • Origin = your servers (the factory).
  • Edge servers / PoPs (Points of Presence) = the CDN's servers in hundreds of cities (the warehouses).
  • Users get content from the nearest edge. The edge only goes back to your origin when it doesn't have a fresh copy.

How It Works

A request through a CDN

step 1 / 8
User A (São Paulo)
User B (São Paulo)
Edge (São Paulo)
Origin (Mumbai)
  1. GET /logo.png
  2. miss → fetch
    ~300 ms across the planet
  3. logo.png + Cache-Control
  4. ↻ store a copy
  5. logo.png
  6. Cache is now warm for the whole region
  7. GET /logo.png
  8. hit!
    ~10–20 ms, origin never touched
A pull CDN. The first request in a region pays the trip to the origin; everyone after it is served locally. · sequence · step through
  1. The user's request is directed to the nearest edge, using anycast or DNS (post 11).
  2. The edge checks its cache. A cache is a place that keeps copies of files so they can be served again quickly.
    • Cache hit: the edge has the file, so it returns it immediately.
    • Cache miss: the edge does not have the file, so it fetches it from the origin, return it to the user, and keep a copy for next time.
  3. Later users near that edge get the fast cached copy.

The key metric is the cache hit ratio: the share of requests answered from the cache. If it's 95%, only 5% of requests ever reach your servers.

Pull CDN vs push CDN

Pull CDN
  • +No upload step — point it at your origin
  • +Stores only what's actually requested
  • −First user per region gets a slow miss
Use whenWebsites, millions of rarely-viewed images
Push CDN
  • +No first-request miss
  • +Full control over what is stored where
  • −You manage uploads and storage
Use whenGame patches, app installers, video libraries
Two ways content reaches the edge. · compare
  • Pull CDN (most common). You point the CDN at your origin. Edges fetch content on demand at the first request, then cache it.
    • ✅ Easy: no upload step.
    • ❌ The first user in each region gets a slower miss.
  • Push CDN. You upload content to the CDN ahead of time.
    • ✅ No first-request miss, and you have full control over what's stored.
    • ❌ You must manage uploads and storage. It's good for large, planned content like software downloads or a video library.

For a site with millions of rarely-viewed images, pull is usually better, because you only store what's actually requested. For a big game update everyone downloads at launch, push (pre-warming) makes sense.

What to put on a CDN

  • Static assets: JavaScript, CSS, fonts, icons.
  • Images and video: product photos, thumbnails, video segments.
  • Downloads: apps, installers, PDFs.
  • Whole static pages: blogs, documentation, marketing sites.
  • Some API responses: public, rarely-changing data like a product catalogue or exchange rates, cached for a short time.

Don't cache: personal pages (your cart, your account), anything with private data, or responses that must always be up to the second, unless you're very careful with cache rules.

How the CDN knows what to cache: HTTP headers

An HTTP header is a line of extra information sent with a request or response. CDNs follow the standard HTTP caching headers from your origin. max-age is the number of seconds a copy may be reused. immutable means the file will never change.

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

means "anyone may cache this for a year; it will never change". It's perfect for versioned files like app.3f2a9c.js.

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

means "cache for 60 seconds. After that, you may serve the old copy for up to 5 more minutes while fetching a fresh one in the background". The word "stale" means old. This is great for pages that change occasionally.

HTTP
Cache-Control: private, no-store

means "do not store this at all, and only the user's own browser may use it". A shared cache is a cache used by many people, such as a CDN. Use this for account pages and anything personal.

Other headers you'll use:

  • s-maxage: like max-age, but only for shared caches like CDNs.
  • ETag / Last-Modified: let the CDN ask the origin "has this changed?" and get a small 304 Not Modified back. An ETag is a label (like a fingerprint) for one version of a file. Last-Modified is the time the file last changed.
  • Vary: "cache different copies depending on this header", for example Vary: Accept-Encoding for gzip vs Brotli versions. Be careful: Vary: Cookie can destroy your hit ratio.

The cache key is what the CDN uses to decide whether two requests are "the same". By default it's the URL. A query parameter is the part of a URL after the ?. If your URLs include random tracking parameters (?utm_source=...), you may want the CDN to ignore them, or every link becomes a separate cache entry.

Invalidation: updating cached content

app.3f2a9c.js
hashed file names
new content = new URL; cache forever
PURGE
delete everywhere
seconds, but an extra step
max-age=60
short TTLs
still absorbs huge spikes
tag:product-991
surrogate keys
purge a whole group in one call
Four ways to change something that is cached on hundreds of servers. · numbers

"There are only two hard things in computer science: cache invalidation and naming things." This saying is famous for a reason. Once a file is cached on hundreds of edges, how do you update it?

  1. Versioned file names (best for static assets). Build tools like Vite, webpack and Next.js add a content hash: app.3f2a9c.js. A new deploy produces app.8b1d4e.js, a new URL, so there's nothing to invalidate. Cache the files forever, and keep the HTML itself on a short cache time.
  2. Purge. Tell the CDN "delete /images/banner.jpg everywhere". Most CDNs purge within seconds, but it's an extra step, and frequent purges can hit limits.
  3. Short TTLs. For content that changes regularly, just cache it for 30–60 seconds. Even a short cache absorbs huge traffic spikes.
  4. Tags / surrogate keys. Some CDNs let you tag responses ("product-991") and purge everything with that tag in one call.

Origin shielding

With 300 edges, a cache miss could mean 300 separate requests to your origin for the same file. Origin shield (tiered caching) adds a middle layer. Edges ask a regional shield cache first, and only the shield talks to your origin. This protects your servers from "miss storms".

  1. Edges
    hundreds of cities
  2. Shield
    a few regions
  3. Origin
    you — sees a trickle
Origin shield. Hundreds of edges ask a few regional shields; only the shields ask you. · flow

Video delivery

Streaming services cut videos into small segments (a few seconds each) at several quality levels, using formats like HLS or DASH (two standard ways to stream video over normal HTTP). Each segment is just a small static file, which is perfect for CDNs. The player picks the quality level that matches your connection, switching up or down as your network changes. That's why a video goes blurry for a moment and then sharpens.

CDNs do more than caching

Modern CDNs also provide:

  • DDoS protection: a DDoS attack sends huge fake traffic to knock a site offline. A CDN absorbs this traffic across its global network.
  • Web Application Firewall (WAF): a filter that blocks common attacks such as SQL injection.
  • TLS at the edge: TLS is the encryption used by HTTPS. The handshake (the first exchange that sets up encryption) happens nearby, so HTTPS starts faster.
  • Image optimisation: automatically resizing images and converting them to modern formats (WebP/AVIF).
  • Edge compute: running small pieces of code at the edge for redirects, A/B tests (showing two versions to compare them), auth checks or personalisation (for example, Cloudflare Workers or Lambda@Edge).

Trade-offs

  • Stale content. A cached copy may be old until it expires or is purged. Choose TTLs per content type.
  • Cost. CDNs charge for bandwidth and requests, but this is usually cheaper than serving the same traffic from your own servers.
  • Debugging is harder. "Why is this user seeing the old version?" might be a CDN, browser or proxy cache.
  • A new dependency. If the CDN has an outage, your site can go down with it, even though your servers are fine. Some large companies use two CDNs.
  • Personalised content is hard to cache. Split pages into a cacheable shell plus a small personalised API call.

When not to bother: internal tools used from one office, or apps where almost everything is personal and real-time. Even then, a CDN for static files is usually a cheap win.

In the Real World

Netflix Open Connect. Netflix built its own CDN and places Open Connect appliances, servers filled with popular shows, directly inside internet providers' networks around the world. Popular content is pushed to these boxes during quiet hours, so when you press play in the evening, the video often comes from a server in your own internet provider's network.

The Fastly outage (June 2021). A single customer's valid configuration change triggered a hidden bug in Fastly's software. For under an hour (95% of the network was back after 49 minutes), many major websites (news sites, e-commerce sites and government websites) showed errors worldwide. The origins were fine; the layer in front of them failed. It's a clear lesson that a CDN is part of your critical path.

Cloudflare, Akamai and Amazon CloudFront serve a large share of all web traffic. Cloudflare in particular offers free plans that let even small blogs get global caching, HTTPS and DDoS protection.

Modern frontend hosting. Platforms like Vercel and Netlify put your built site on a global CDN automatically. Hashed file names from the build step, plus short caching for HTML, is exactly the invalidation strategy described above.

Live cricket and big events. Streaming platforms push video segments to CDN edges across the country (and use many CDN partners) so that millions of people can watch the same moment without every request hitting a central server.

Interview Questions

Q1How does a CDN reduce latency and origin load?

It serves content from edge servers close to users, so each request travels a few kilometres instead of across continents, and the TLS handshake happens nearby. The origin only sees cache misses: with a 95% hit ratio, it handles 5% of the traffic.

Q2How do you deploy new JavaScript without users getting stale files?

Fingerprint asset file names with a content hash (app.3f2a9c.js) and cache them forever with immutable. Each deploy produces new URLs, so nothing needs invalidating. Keep the HTML that references them on a short TTL.

Q3What's the difference between max-age and s-maxage?

max-age applies to every cache, including browsers. s-maxage applies only to shared caches like CDNs and proxies, so you can let the CDN cache for an hour while browsers revalidate every minute.

Q4Why can Vary: Cookie destroy your hit ratio?

The CDN stores a separate copy per distinct value of the Vary header. Almost every user has different cookies, so almost every request becomes its own cache entry and misses. Vary on small, bounded headers like Accept-Encoding instead.

Q5Can you cache personalised pages on a CDN?

Not the personal parts. Split the page into a public, cacheable shell and a small personalised API call made from the client, or personalise at the edge with edge compute. Mark private responses Cache-Control: private, no-store.

Q6What is an origin shield?

A middle tier of regional caches between edges and the origin. On a miss, edges ask the shield, and only the shield asks the origin, collapsing hundreds of parallel misses for the same file into one.

Key Takeaways

  • A CDN caches content on edge servers near users. It lowers latency and offloads most traffic from your origin.
  • Pull CDNs fetch on the first request; push CDNs are loaded in advance.
  • HTTP headers (Cache-Control, ETag, Vary) control caching. Watch the cache key and the hit ratio.
  • Use hashed file names for static assets (cache them forever) and short TTLs or purges for content that changes.
  • CDNs also provide DDoS protection, WAF, TLS at the edge and edge compute, but they're a critical dependency too.

Further Reading

  • MDN Web Docs guide "HTTP caching"
  • RFC 9111 (HTTP Caching)
  • The System Design Primer on GitHub (section "Content delivery network")
  • Cloudflare Learning Center article "What is a CDN?"
  • Netflix's Open Connect overview and the Netflix Tech Blog

Published by Jainik · Last updated · How these lessons are made

Keep learning

Part of the study guide

Related series