Suppose your website's servers are in the United States and a student in Chennai opens your home page. Every image, stylesheet and script has to cross the ocean and come back. Even with perfect servers, the round trip takes roughly 200 ms or more, and a page needs several round trips. The page feels slow, and there is nothing your code can do about it: the problem is the speed of light over a long distance.
The fix is simple in idea: keep copies of your content on servers spread across the world, and serve each user from the copy closest to them. A network of such servers is a Content Delivery Network (CDN). In the previous chapter, Back-of-the-Envelope Estimation, our photo app needed about 28 Gbps of outgoing bandwidth even after using thumbnails. In this chapter we see how a CDN takes almost all of that load off our own servers.
A Content Delivery Network (CDN) is a geographically distributed group of servers (edge servers) that cache content close to users, so that requests are served from a nearby location instead of the faraway origin server.
Without CDN With CDN
User (Chennai) ──── 220 ms RTT ────→ Origin (USA) User (Chennai) ── 10 ms ──→ Edge (Chennai)
User (London) ──── 80 ms RTT ────→ Origin (USA) │ only on a miss
User (Tokyo) ──── 150 ms RTT ────→ Origin (USA) ↓
Origin (USA)
Why We Need CDNs
A CDN solves three separate problems at once.
Latency. As we saw in Latency, Throughput & Availability, propagation delay depends on distance, and no amount of bandwidth reduces it. Moving the content closer is the only fix.
Origin load. If 95% of requests are answered by edge servers, your origin handles only 5% of the traffic. Fewer servers, smaller bills, less risk of overload during a traffic spike.
Bandwidth cost and availability. CDNs connect directly to many ISPs and have huge total capacity. They can keep serving cached content even when the origin is briefly down, and they can absorb attack traffic.
Key Terms
Term | Meaning |
|---|---|
Origin server | Your own server (or storage bucket) that holds the original, authoritative content. |
Edge server | A CDN server near users that stores cached copies and answers requests. |
PoP (Point of Presence) | A CDN location (usually in a data centre or internet exchange) containing many edge servers. |
Cache hit | The edge already has a fresh copy and answers directly. |
Cache miss | The edge does not have the content (or its copy is stale), so it fetches from the origin first. |
TTL (Time To Live) | How long a cached copy is considered fresh. |
Hit ratio | Hits ÷ total requests. Also called offload when measured from the origin's point of view. |
How a CDN Request Works
Let's follow a request for https://cdn.example.com/img/logo.png.
1. Browser asks DNS: "What is the IP of cdn.example.com?"
2. DNS (run by the CDN) returns the IP of a nearby edge (or an anycast IP).
3. Browser opens a TCP + TLS connection to that edge and sends GET /img/logo.png
4. Edge looks up its cache:
HIT → return the stored copy immediately (fast path)
MISS → fetch from origin, store a copy, then return (slow path, once)
5. The next user near the same edge gets a HIT.
Browser Edge (Chennai PoP) Origin
│ GET /logo.png │ │
│──────────────────────→│ not in cache (MISS) │
│ │──── GET /logo.png ────────────→│
│ │←─── 200 OK, Cache-Control: ────│
│ │ max-age=86400 │
│←── 200 OK ────────────│ store copy for 1 day │
│ │ │
│ GET /logo.png │ │
│──────────────────────→│ in cache, fresh (HIT) │
│←── 200 OK, Age: 35 ───│ origin not contacted │
The Age header in the second response tells the client how many seconds the copy has been sitting in the cache.
How Users Reach the Nearest Edge
There are two main techniques, and many CDNs combine them.
1. DNS-Based Routing
Your domain (for example cdn.example.com) is a CNAME pointing to the CDN's domain. The CDN's DNS servers look at where the query came from (the resolver's IP, or the client subnet if the resolver passes it along) and answer with the IP of a nearby, healthy PoP. Different users get different IP addresses for the same name.
2. Anycast Routing
With anycast, the same IP address is announced from many PoPs using BGP, the routing protocol of the internet. Routers send each packet along the best route they know, which usually ends at a nearby PoP. Every user uses the same IP, but different users reach different physical servers.
Feature | DNS-based | Anycast |
|---|---|---|
IP returned | Different per region | Same everywhere |
Who chooses the PoP | CDN's DNS server | Internet routing (BGP) |
Failover speed | Limited by DNS TTL and resolver caching | Fast: withdraw the route and traffic moves |
Fine control (load, cost) | High | Lower: "nearest" means nearest in BGP terms, not always geographically |
DDoS absorption | Good | Excellent: attack traffic is spread across all PoPs |
Pull CDN vs Push CDN
How does content get onto the edge in the first place?
Pull CDN: the edge fetches content from the origin on the first request (a miss) and caches it. You do nothing except point the CDN at your origin. This is the default for almost all websites.
Push CDN: you upload content to the CDN ahead of time. The CDN storage becomes the source. Useful for large files you know will be requested, such as a game patch or a new film release.
Feature | Pull CDN | Push CDN |
|---|---|---|
Content arrives at edge | Lazily, on first request | Eagerly, when you upload |
First request | Slow (miss) | Fast |
Effort | Very low | You manage uploads and deletes |
Storage used | Only popular content | Everything you push |
Best for | Websites, APIs, user images with unpredictable popularity | Large, known, infrequently changing files |
Controlling Caching with HTTP Headers
A CDN is an HTTP shared cache, so it follows the HTTP caching rules (RFC 9111, which replaced RFC 7234 in 2022). The origin tells caches what to do using the Cache-Control response header.
Directive | Meaning |
|---|---|
| The response is fresh for N seconds (for all caches, including the browser). |
| Like max-age, but only for shared caches such as CDNs and proxies. Overrides max-age there. |
| Any cache may store it, even in cases where it normally would not. |
| Only the user's browser may store it; a CDN must not. |
| May be stored, but must be revalidated with the origin before every reuse. |
| Must not be stored anywhere. Use for sensitive data. |
| Once stale, must not be served without successful revalidation. |
| The content will never change while fresh, so the browser need not revalidate even on reload. |
| After expiry, the cache may serve the stale copy for up to N more seconds while it refreshes in the background. |
| If the origin returns an error or is unreachable, the stale copy may be served for up to N seconds. |
The most misunderstood pair:
no-cachedoes NOT mean "do not cache". It means "cache, but check with the origin before using". The directive that forbids storing isno-store.
Typical header choices:
# Fingerprinted static file (name changes when content changes)
Cache-Control: public, max-age=31536000, immutable
# Product page: CDN keeps it 5 min, browser 1 min, serve stale briefly while refreshing
Cache-Control: public, max-age=60, s-maxage=300, stale-while-revalidate=30
# Logged-in user's dashboard: browser only
Cache-Control: private, max-age=0, must-revalidate
# Bank statement API response
Cache-Control: no-store
Revalidation: Cheap Freshness Checks
When a cached copy becomes stale, the cache does not always need to download it again. If the origin gave an ETag (a version identifier) or Last-Modified date, the cache sends a conditional request:
Edge → Origin: GET /style.css
If-None-Match: "v42"
Origin → Edge: 304 Not Modified (no body: the cached copy is still good)
or: 200 OK + new body + ETag: "v43"
A 304 response is tiny, so revalidation saves bandwidth even though it still costs a round trip to the origin.
Many CDNs also support CDN-specific headers such as CDN-Cache-Control (standardised in RFC 9213), which lets you give the CDN different instructions from the browser without affecting other caches.
The Cache Key
The cache key is what the edge uses to decide whether two requests are "the same". By default it is roughly the host plus the full URL path and query string.
https://cdn.example.com/img/logo.png → key A
https://cdn.example.com/img/logo.png?utm=insta → key B (a different entry!)
Tracking parameters such as utm_source create thousands of copies of the same file and destroy the hit ratio. Good CDN setups ignore or sort irrelevant query parameters in the cache key.
The Vary response header adds request headers to the key. Vary: Accept-Encoding is normal (gzip and Brotli versions are stored separately). Vary: User-Agent or Vary: Cookie is usually a disaster, because almost every user has a different value, so nearly every request becomes a miss.
Invalidation: Changing Cached Content
Once content is cached at hundreds of PoPs, how do you update it? Three approaches:
Wait for TTL expiry. Simple, but users may see old content until it expires.
Purge (invalidate). Call the CDN's API to remove a URL, a prefix or a tag from all edges. Useful for urgent fixes, but it takes some time to reach every PoP and is often rate limited.
Versioned URLs (cache busting). Put a content hash or version in the file name:
app.3f9a1c.js. A new release produces a new name, so there is nothing to invalidate. The HTML is updated to reference the new name, and the old file simply stops being requested.
The best practice for static assets is: fingerprinted file names + a one-year TTL + immutable, and a short TTL for the HTML page that references them.
index.html Cache-Control: max-age=60 (short, so new releases show quickly)
└─ app.3f9a1c.js Cache-Control: max-age=31536000, immutable
└─ logo.8b21de.png Cache-Control: max-age=31536000, immutable
Tiered Caching and Origin Shield
A CDN may have hundreds of PoPs. If each PoP misses independently, a newly popular file causes hundreds of separate requests to your origin. To avoid that, CDNs add a middle layer: edges that miss ask a regional or "shield" cache first, and only the shield talks to the origin.
Edge Chennai ─┐
Edge Mumbai ─┼──→ Regional / shield cache (Mumbai) ──→ Origin
Edge Delhi ─┘ (one request per object)
Edge Tokyo ─┐
Edge Seoul ─┴──→ Regional / shield cache (Tokyo) ──→ Origin
This raises the overall hit ratio and protects the origin. Providers name it differently (for example "tiered cache", "regional edge cache" or "origin shield"), but the idea is the same.
What Should Go on a CDN?
Content | CDN-friendly? | Why |
|---|---|---|
Images, CSS, JavaScript, fonts | Yes, ideal | Same for every user, change rarely |
Video segments (HLS/DASH chunks) | Yes, ideal | Huge bandwidth, identical for all viewers |
Software downloads, game patches | Yes | Large and popular |
Public HTML pages, public API responses | Yes, with short TTLs | Same for everyone for a short time |
Personalised pages, cart, account data | Do not cache | Different per user; caching leaks data between users |
Write requests (POST, PUT) | Pass through | Not cacheable, but the CDN can still speed up the connection |
Even uncacheable traffic benefits from a CDN. The user's TCP and TLS handshakes finish at a nearby edge, and the edge forwards the request over a warm, long-lived connection to the origin. This is often called dynamic site acceleration. Many CDNs also run small programs at the edge (edge computing), for example for redirects, A/B tests or authentication checks.
Security Benefits
DDoS protection: attack traffic is spread across a very large network instead of hitting your few servers.
Web Application Firewall (WAF): filters malicious requests (for example SQL injection patterns) at the edge.
TLS termination: certificates are managed at the edge, close to users.
Hiding the origin: if the origin accepts traffic only from the CDN, attackers cannot reach it directly.
A Tiny Edge Cache Simulation
The program below models one edge server in front of an origin. The origin says each object is fresh for 60 seconds (like max-age=60). Watch how hits, misses and expiry happen.
class Origin:
def __init__(self):
self.hits = 0
def fetch(self, path):
self.hits += 1
# (body, max_age in seconds) as the origin's Cache-Control would say
return f"content of {path}", 60
class EdgeServer:
def __init__(self, name, origin):
self.name = name
self.origin = origin
self.store = {} # path -> (body, stored_at, max_age)
def get(self, path, now):
entry = self.store.get(path)
if entry:
body, stored_at, max_age = entry
age = now - stored_at
if age < max_age:
return f"HIT age={age:>3}s"
status = "EXPIRED"
else:
status = "MISS "
body, max_age = self.origin.fetch(path)
self.store[path] = (body, now, max_age)
return f"{status} fetched from origin"
origin = Origin()
mumbai = EdgeServer("Mumbai", origin)
requests = [(0, "/logo.png"), (5, "/logo.png"), (20, "/app.js"),
(30, "/logo.png"), (59, "/logo.png"), (61, "/logo.png"),
(70, "/app.js"), (75, "/logo.png")]
for t, path in requests:
print(f"t={t:>3}s GET {path:<10} -> {mumbai.get(path, t)}")
total = len(requests)
print(f"\nRequests: {total}, origin fetches: {origin.hits}, "
f"offload: {100 * (total - origin.hits) / total:.1f}%")
Output:
t= 0s GET /logo.png -> MISS fetched from origin
t= 5s GET /logo.png -> HIT age= 5s
t= 20s GET /app.js -> MISS fetched from origin
t= 30s GET /logo.png -> HIT age= 30s
t= 59s GET /logo.png -> HIT age= 59s
t= 61s GET /logo.png -> EXPIRED fetched from origin
t= 70s GET /app.js -> HIT age= 50s
t= 75s GET /logo.png -> HIT age= 14s
Requests: 8, origin fetches: 3, offload: 62.5%
Notice the trade-off: a longer TTL would have turned the expiry at t = 61 into a hit (higher offload), but users would also see updates later. A real CDN would revalidate with an ETag on expiry instead of downloading the whole file again.
Worked Example: How Much Does a CDN Help?
From Back-of-the-Envelope Estimation, our photo app serves about 27.8 Gbps of thumbnails on average. With a CDN, the origin only serves the misses:
Origin egress = total egress × (1 − hit ratio)
Hit ratio 80% → 27.8 × 0.20 = 5.56 Gbps
Hit ratio 90% → 27.8 × 0.10 = 2.78 Gbps
Hit ratio 95% → 27.8 × 0.05 = 1.39 Gbps
Hit ratio 99% → 27.8 × 0.01 = 0.28 Gbps
Going from 90% to 99% hit ratio cuts origin traffic by another 10 times. Small improvements in hit ratio matter a lot near the top.
Latency: a user in Chennai, an origin in the US (≈ 220 ms RTT), an edge in Chennai (≈ 10 ms RTT). Say loading an object takes about 3 round trips (TCP handshake, TLS 1.3 handshake, then the request).
Direct to origin : 3 × 220 ms = 660 ms
Via edge, HIT : 3 × 10 ms = 30 ms
Via edge, MISS : 30 ms + 220 ms (edge reuses a warm connection to origin) = 250 ms
Average with 95% hit ratio = 0.95 × 30 + 0.05 × 250 = 28.5 + 12.5 = 41 ms
Even the misses are faster than going direct, because the slow handshakes happen over the short user-to-edge link.
Advantages and Disadvantages
Advantages | Disadvantages |
|---|---|
Much lower latency for distant users | Extra cost (usually billed per GB and per request) |
Large reduction in origin load and bandwidth | Stale content until TTL expiry or purge |
Better availability (can serve stale on origin failure) | Misconfiguration can leak private data between users |
DDoS protection, WAF, TLS at the edge | Harder debugging: is the bug in the origin or in a cached copy? |
Handles sudden traffic spikes | Dependence on a third-party provider (an outage there affects you) |
Common Mistakes
Thinking
no-cachemeans "never cache". It means "revalidate before use". Useno-storeto forbid storage.Caching personalised responses. A page with "Hello, Priya" cached publicly will greet everyone as Priya. Mark such responses
privateorno-store.Long TTLs on non-versioned files. If
style.csshas a one-year TTL and you change it in place, many users will not see the change. Use fingerprinted names.Polluted cache keys. Tracking query parameters or
Vary: Cookiesplit one object into thousands of entries and kill the hit ratio.Assuming a CDN is only for static files. It also speeds up dynamic traffic through edge TLS termination and warm origin connections.
Leaving the origin open. Attackers who find the origin IP can bypass the CDN's protection.
Interview Questions
1. What is a CDN and what problems does it solve?
A CDN is a globally distributed network of edge servers that cache content close to users. It reduces latency (shorter distance), reduces origin load and bandwidth (most requests are served at the edge), improves availability, and adds security features such as DDoS protection and a WAF.
2. Explain pull CDN vs push CDN.
In a pull CDN, edges fetch content from the origin on the first request and cache it for its TTL; it needs almost no effort and stores only what is popular. In a push CDN, you upload content to the CDN in advance; first requests are fast but you manage uploads and storage. Pull is the default for websites; push suits large, known files.
3. You deployed a CSS fix but users still see the old styling. Why, and how do you prevent it?
The old file is still cached at the CDN and in browsers until its TTL expires. You can purge it, but the robust fix is to use fingerprinted file names (for example style.a1b2c3.css) with a long TTL and immutable, and a short TTL on the HTML that references them. Each release then uses new URLs.
4. What is the difference between no-cache, no-store and private?
no-store: never store the response. no-cache: may store, but must revalidate with the origin before each reuse. private: only the end user's browser may store it; shared caches like CDNs must not.
5. How does anycast help a CDN?
The same IP address is announced from many PoPs via BGP, so each user's packets are routed to a nearby PoP automatically. Failover is fast (withdraw the route) and attack traffic is spread across many locations. The trade-off is less precise control, since BGP's "nearest" is not always the geographically or least-loaded nearest.
6. What is an origin shield and why use it?
It is an intermediate cache layer between edges and the origin. When many edges miss on the same object, only the shield fetches it from the origin. This collapses duplicate requests, increases the overall hit ratio and protects the origin during spikes.
Summary
A CDN caches content on edge servers in many PoPs near users; the origin is contacted only on a miss.
Users reach a nearby edge through DNS-based routing or anycast.
Pull CDNs fetch lazily on first request; push CDNs are loaded in advance.
Caching is controlled with
Cache-Control(RFC 9111):max-age,s-maxage,private,no-cache,no-store, plusstale-while-revalidateandstale-if-error(RFC 5861).Update content with TTL expiry, purges, or best of all versioned URLs.
Tiered caching / origin shields reduce origin load further. Small gains in hit ratio near 100% give large savings.
Never cache personalised data publicly; design the cache key carefully.
You cannot make light faster, but you can make the distance shorter. That is all a CDN does, and it is enough.