Module 12 / 20 · Phase C — Scale & Reliability · 40 min

CDNs, and
the global cache.

Latency is a function of distance. So what if your servers were everywhere at once — close to every user no matter where they are? That's the trick the entire commercial internet runs on. Time to look inside.

// What you'll know by the end

  • Why distance is still the enemy
  • How a CDN stops the round trip to origin
  • What gets cached at the edge and what doesn't
  • The four jobs a modern CDN actually does
§ 01 — Back to physics

A user in Sydney
can't be fast.

Remember Module 08? Light moves at about 200km/ms through fiber. London to Tokyo round-trip is ~210ms minimum, and that's the floor — engineering can shave the overhead above it, but the floor itself is set by physics. So when your servers live in Virginia and your user lives in Sydney, every single request pays that distance tax. Page loads feel slow. Apps stutter. Bounce rates rise. The internet has a geography problem.

// SAME 1MB FILE · DIFFERENT WORLDS
User in Virginia → origin in Virginia// same metro, lucky few
~12ms
User in London → origin in Virginia// transatlantic cable
~85ms
User in Tokyo → origin in Virginia// across the Pacific
~165ms
User in Sydney → origin in Virginia// halfway around the world
~220ms
User in São Paulo → origin in Virginia// down the Americas
~125ms

That's just one request. A typical web page loads 60-100 files (HTML, CSS, JS, images, fonts). A user in Sydney waiting on Virginia could spend seconds just on accumulated network latency. There's nothing wrong with the code. Nothing wrong with the server. The user is just too far from the data. The only fix is to move the data closer. Which, it turns out, is exactly what a CDN does.

§ 02 — What a CDN actually is

Copies everywhere,
by design.

A Content Delivery Network is a distributed network of caching servers — called edges or PoPs (Points of Presence) — spread across the planet. When your user in Sydney requests an image, the request doesn't go to Virginia. It goes to the Sydney edge. If the edge has the file cached, it returns it in 10ms. If not, it fetches from your origin once, caches it, and serves every Sydney user from local storage from then on.

// ONE ORIGIN, MANY EDGES

ORIGIN Virginia · 1 server source of truth London Edge cached copy Tokyo Edge cached copy São Paulo Edge cached copy Sydney Edge cached copy Frankfurt Edge cached copy Mumbai Edge cached copy edges pull from origin once · serve locally forever (until TTL)

That diagram shows six edges. A real CDN has hundreds. Cloudflare has 300+ data centers worldwide. Akamai has over 4,000 locations. The point isn't to have one giant cache — it's to have thousands of small caches, each within milliseconds of some population of users. Your origin server might still be a single box in Virginia, but to a user in Mumbai, the website effectively lives in Mumbai.

The origin is your warehouse. The edges are your local stores. Same product, much closer to the customer.

How users find the nearest edge is itself clever: it's done with the same Anycast trick we mentioned in M.11. The CDN announces the same IP address from hundreds of locations, and BGP routes each user to the closest one automatically. No client-side intelligence required. To your code, it just looks like a normal HTTP request — but the response comes back ten times faster than it should.

§ 03 — What's cacheable, what isn't

Not everything
can live at the edge.

CDNs are caches, so the cache rules from Module 07 still apply: you can only cache something safely if it's the same answer for everyone (or everyone in some segment). This works beautifully for static content — a CSS file looks the same to every user. It works terribly for per-user content — your bank balance shouldn't be cached at a public edge. The skill is knowing what lives in which category.

// CAN THIS GO ON A CDN?

Static assets// CSS, JS, images, fonts
Same bytes for every user. Change rarely. Often given long cache lifetimes (max-age=31536000, one year) by including a hash in the filename — app.a3f2b8.js. Easy win.
YES — ALWAYS
Public HTML pages// marketing, blog posts
Same for everyone, but you may update them. Cache with a short TTL (5-60 min) or use stale-while-revalidate so users see a cached copy while a fresh one is fetched.
YES — SHORT TTL
API responses// public data, rate-limited
Public lists (top products, leaderboards, currency rates) can be cached briefly. Often gated by query string — the CDN caches /api/products?page=1 as a different key than page 2.
SOMETIMES
Personalized HTML// "Welcome back, Alice"
Could be cached per user, but a CDN edge serves many users so it usually just bypasses the cache — set Cache-Control: private or no-store. The HTML hits origin; the static assets within it still benefit.
RARELY
User-specific data// account, profile, orders
Different for every user, often sensitive. Caching at a shared edge is a security hazard — you might serve Alice's data to Bob. Always set Cache-Control: no-store or private.
NEVER
Write operations// POST, PUT, DELETE
By definition not cacheable — each one changes state. CDNs forward these straight to the origin unchanged. Caching the response to a POST would be a correctness disaster.
NEVER

The control mechanism is the same set of HTTP headers we touched on in M.07: Cache-Control on responses tells the CDN what to do. public, max-age=3600 = "cacheable for anyone, fresh for an hour." private, no-store = "this belongs to one user, don't cache anywhere." Most production setups define cache rules per route or per file pattern at the CDN level — and getting them right is one of the easiest performance wins in any system.

§ 04 — Global edge cache · interactive lab

Now feel the
collapse.

Five users worldwide — Tokyo, London, Sydney, São Paulo, Mumbai — all request the same JS file from an origin in Virginia. Toggle between Without CDN and With CDN and hit Send batch. Watch what happens to the latency numbers. The first batch with CDN warms the edges (still kinda slow). Every batch after that — a different story.

CDN_EDGE.SIM // m.12 lab
MODE:
5 USERS WORLDWIDE · 1 ORIGIN · WITHOUT CDN ready · press Send batch to begin
// Aggregate stats
Avg latency
Origin hits
0
Edge hits
0
Batches sent
0
// Per-user latency · last batch
Tokyo
London
Sydney
São Paulo
Mumbai
// VERDICT
Press Send batch to begin

Without a CDN, every user pays the round-trip cost to Virginia. With a CDN, the first batch warms the edges (the edge must still fetch from origin once), but subsequent batches are answered locally. Watch the average latency collapse from ~160ms to under 15ms.'

§ 05 — More than just caching

Modern CDNs do
a lot more.

The caching story is the original CDN story, and it's still the headline feature. But the same global edge footprint turns out to be useful for several other jobs that have nothing to do with caching — and that's why CDNs have become a layer of infrastructure most production systems can't really do without.

// JOB 2 — security

DDoS protection

When an attacker tries to flood your origin with traffic, the CDN's hundreds of edges absorb the attack across their entire global capacity. A 1 Tbps flood that would kill your origin instantly becomes background noise distributed across the edge fleet. This is the second-most-common reason teams adopt a CDN.

// JOB 3 — TLS

TLS termination at the edge

The expensive TLS handshake (Module 03!) happens between user and the nearby edge, not user and your distant origin. Edges hold your SSL certificate and decrypt incoming traffic, forwarding plain HTTPS to your origin over a maintained connection. Cuts visible page-load times by 100-200ms.

// JOB 4 — optimization

Image & content optimization

Edges can resize images, convert formats (JPEG → WebP/AVIF), and minify JS/CSS on the fly. Same source file, different optimized versions per browser. Some edges even rewrite HTML to prefetch resources. Faster than doing it yourself — and zero deploy.

// JOB 5 — edge compute

Logic at the edge

Newer CDNs (Cloudflare Workers, Fastly Compute, Vercel Edge) run your code on every edge — A/B testing, auth checks, redirects, lightweight APIs. The user gets a response from 30ms away without your origin ever waking up. The line between CDN and compute platform is blurring fast.

The honest summary is that "CDN" has become a misleadingly narrow name. Modern providers like Cloudflare, Fastly, and CloudFront sell themselves as edge platforms — caching is just job one. By the time you've gotten to scale, the CDN is doing a dozen things: serving cached assets, terminating TLS, blocking bots, redirecting old URLs, A/B testing, optimizing images, rate-limiting hot endpoints. The platform earns its line item.

§ 06 — Eight words for the edge layer

Vocabulary,
for the global cache.

You'll hear these in every architecture conversation involving a frontend that anyone outside your office uses.

CDN
/siː diː ɛn/
Content Delivery Network. A global mesh of caching servers that stand in front of your origin, serving most user requests from edges near them. Cloudflare, Akamai, Fastly, CloudFront.
Edge / PoP
/ɛdʒ · pɒp/
A single CDN data center. "Point of Presence." Holds cached copies of your content. Cloudflare runs 300+ of these worldwide; Akamai over 4,000.
Origin
/ˈɒrɪdʒɪn/
Your actual server — the source of truth for content. Sits behind the CDN. Edges pull from origin on cache miss, then serve copies locally.
Cache-Control
/kæʃ kənˈtroʊl/
The HTTP response header that tells caches what to do: public, max-age=3600, no-store, private. The dial that controls everything about caching behavior.
Purge
/pɜːdʒ/
Telling the CDN to drop a specific cached entry (or all of them) before the TTL expires. Used after you deploy fresh content and need it visible immediately. Most CDNs purge globally in under a minute.
Origin Shield
/ˈɒrɪdʒɪn ʃiːld/
An optional intermediate cache layer between the edge fleet and your origin. Reduces origin load by making sure only one edge per region fetches from origin on a cache miss, not all of them.
Stale-While-Revalidate
/steɪl-waɪl/
A Cache-Control directive: serve the cached (possibly stale) response immediately, then refresh in the background. Users always get a fast response; content is eventually fresh.
Edge Compute
/ɛdʒ kəmˈpjuːt/
Running your own code at CDN edges, not just serving cached files. Cloudflare Workers, Fastly Compute. The bleeding edge of "no origin needed for this request."
§ 07 — Knowledge check

Five questions.
Find the edge.

Lock in the CDN intuition. Click an answer; the explanation drops in instantly.

QUESTION 1 OF 5
Loading question...
Score: 0 / 5
5 / 5

Distributed.

You see the global cache clearly now. Statelessness is next — and it's what makes all this distribution possible.

§ 08 — The recap

Three ideas to
carry forward.

CDNs are how the internet doesn't feel as slow as it actually is.

i

Distance is the enemy

Physics makes long-distance requests slow no matter how fast your server is. The only fix is to put a copy closer to the user.

ii

Edges are tiny caches, everywhere

A CDN spreads your static content across hundreds of points of presence. Each user hits the nearest one — typically <20ms away.

iii

CDNs do more than caching

TLS termination, DDoS protection, image optimization, edge compute. The global footprint is useful for far more than serving files.

↓ UP NEXT

M.13 — Statelessness
& sessions.

We've kept saying "stateless." Now's the moment to look closely. Where does the user's identity live? How do you scale horizontally without losing track of who's logged in? Time to look at sessions, tokens, and the discipline that unlocks every pattern we've built so far.

Continue to Module 13 →