← Back to all posts
CONCEPT

How CDNs Work
The Internet's Secret Superpower 🌐

Ever wondered how Netflix streams 4K video to 200 million users without buffering? Or why a website in Tokyo loads instantly for someone in New York? The answer is CDNs — the invisible infrastructure layer that powers over 60% of all internet traffic. Let's understand how they actually work! 🔥

📅 Dec 3, 2025 ⚡ 60%+ of Internet Traffic 🌍 330+ Global PoPs 📖 28 min read
Scroll to learn
01

What is a CDN & Why Does It Exist?

A Content Delivery Network (CDN) is a geographically distributed network of servers that work together to deliver content to users as fast as possible. Instead of every user fetching content from one central server (which could be thousands of miles away), CDNs place copies of that content on servers all around the world. 🌍

🔧 Simple Analogy

Imagine you're craving pizza. Without a CDN, it's like ordering from a single pizzeria in New York — even if you're in Los Angeles, your pizza has to travel 2,800 miles. With a CDN, it's like that pizzeria opening franchises in every major city. Now your pizza comes from a local shop just minutes away, using the same recipe (same content), but delivered way faster!

The Numbers Are Staggering 🤯

60%+ of Internet Traffic via CDNs
50-70% Latency Reduction
330+ Cloudflare PoP Locations
$20B+ CDN Market Size (2025)

What CDNs Actually Deliver

CDNs aren't just for images. They deliver almost everything you interact with online:

💡 Fun Fact

When you watch Netflix, the video isn't streaming from Netflix's servers in California. It's coming from a Netflix "Open Connect" appliance that's probably sitting inside your ISP's data center, just a few miles from your home. That's CDN magic!

02

The Internet Without CDNs (It's Painful)

To truly appreciate CDNs, let's imagine a world without them. Spoiler: it's not pretty. 😬

The Problem: Physics is Undefeated

Data travels through fiber optic cables at about 2/3 the speed of light. Sounds fast, right? But the Earth is big, and those milliseconds add up:

Round-Trip Time (RTT) Without CDN
Route Distance Min RTT (Light Speed) Real-World RTT
NYC → LA ~4,000 km ~27 ms 60-80 ms
NYC → London ~5,500 km ~37 ms 70-90 ms
NYC → Tokyo ~11,000 km ~73 ms 150-200 ms
NYC → Sydney ~16,000 km ~107 ms 200-300 ms

And that's just one round trip! A typical web page requires dozens of requests (HTML, CSS, JS, images, fonts, API calls). Without a CDN, each request has to travel across the world.

What Happens to Your Website

Loading a Page Without CDN (User in Sydney, Server in NYC)
Request 1
HTML
250ms RTT
Requests 2-5
CSS + JS
250ms × 4 = 1000ms
Requests 6-25
Images
250ms × 20 = 5000ms
Total
6+ seconds 😱
💸 The Business Impact

Amazon found that every 100ms of latency costs them 1% in sales. Google found that a 500ms delay reduces traffic by 20%. For a company doing $100M/year, that's $1M lost for every 100ms of delay!

Other Problems Without CDNs

Key Insight

CDNs exist because physics is undefeated. You can't make light travel faster, but you can move your content closer to users. That's the fundamental insight behind all CDN technology.

03

CDN Architecture: Edge, PoPs & Origin

Now let's look at how CDNs are actually built. Understanding this architecture is key to understanding how they work! 🏗️

The Three-Tier Architecture

🏠 Origin Server (Your Server)
This is your actual server where the "source of truth" lives. It could be an AWS EC2 instance, a Kubernetes cluster, or even a static site on S3. The CDN fetches content from here when it doesn't have a cached copy.
↕️ Origin Shield (Optional Middle Tier)
🛡️ Origin Shield / Mid-Tier Cache
A regional cache layer that sits between edge servers and origin. Multiple edge servers in a region share this cache, reducing load on the origin. Not all CDNs have this, but it's common in enterprise setups.
↕️ Distributed Globally
⚡ Edge Servers (PoPs)
This is where the magic happens! Edge servers are deployed in data centers around the world, as close to users as possible. They cache content and serve it directly to users. A major CDN has hundreds of these locations (called Points of Presence or PoPs).
↕️ Last Mile
👤 End Users
Users around the world connect to the nearest edge server. Instead of traveling 10,000 km to your origin, they travel maybe 50 km to the nearest PoP.

What's Inside an Edge Server?

Edge servers are specialized machines optimized for one thing: serving cached content incredibly fast. Here's what's typically inside:

💾 NVMe SSDs (fast storage)
🧠 256GB+ RAM (hot cache)
🌐 100Gbps+ Network
⚙️ Custom Software Stack

Points of Presence (PoPs) Explained

A PoP (Point of Presence) is a physical location where a CDN has deployed servers. Think of it as a "franchise location" for your content. 🏪

🔧 PoP Analogy

PoPs are like Starbucks locations. Starbucks doesn't ship your coffee from Seattle every time — they have stores in every city. Similarly, CDNs don't fetch your content from your origin server every time — they serve it from the nearest PoP.

Major CDN providers have PoPs strategically placed:

CDN Request Flow
Step 1
User Request
Step 2
DNS Routing
Step 3
Edge Server
Step 4a
Cache HIT ✅

If cache MISS, edge fetches from origin, caches it, then serves user

04

How Caching Actually Works

Caching is the heart of CDN technology. Let's understand exactly how it works — this is where it gets really interesting! 🎯

The Cache Key

When the CDN receives a request, it needs to decide: "Do I have this content already?" It does this by computing a cache key — a unique identifier for the content.

🔑 Typical Cache Key Components
// A cache key is typically built from:

cacheKey = hash(
    url,              // https://example.com/images/hero.jpg
    queryParams,       // ?v=1.2.3
    headers,           // Accept-Encoding, Accept-Language (if Vary)
    cookies            // Sometimes (careful with this!)
)

// Example cache key:
"example.com/images/hero.jpg?v=1.2.3|gzip|en-US"

Cache HIT vs MISS

Cache HIT (Fast Path) ⚡
0 ms
User requests /images/hero.jpg
Request arrives at nearest edge server
1 ms
Edge checks cache
Computes cache key, looks up in local storage
2 ms
✅ Cache HIT!
Content found, still valid (not expired)
5 ms
Response sent to user
Total time: ~5ms (vs 200ms without CDN!)
Cache MISS (Slow Path) 🐢
0 ms
User requests /images/new-hero.jpg
Request arrives at edge server
1 ms
❌ Cache MISS
Content not in cache (new or expired)
2-100 ms
Fetch from origin
Edge requests content from your origin server
100-150 ms
Cache the response
Edge stores content for future requests
150 ms
Response sent to user
First user pays the "cold cache" penalty

Cache-Control Headers

Your origin server tells the CDN how to cache content using HTTP headers. This is crucial — get it wrong, and you'll either serve stale content or have terrible cache hit rates. 😬

📋 HTTP Response Headers from Origin
// Tell CDN to cache for 1 year (immutable assets)
Cache-Control: public, max-age=31536000, immutable

// Tell CDN to cache for 1 hour
Cache-Control: public, max-age=3600

// Tell CDN to revalidate before serving
Cache-Control: public, max-age=0, must-revalidate

// Tell CDN NOT to cache (private user data)
Cache-Control: private, no-store

// Vary header: cache different versions based on header
Vary: Accept-Encoding, Accept-Language
⚠️ Common Mistake

Setting Cache-Control: public, max-age=31536000 on your HTML pages. Now users see stale content for a year! Only use long cache times for versioned assets (like app.v2.3.1.js).

Cache Tiers (Memory vs Disk)

Edge servers typically have multiple cache tiers:

Tier Storage Speed Capacity Used For
L1 (Hot) RAM ~0.1 ms 256 GB Most popular content
L2 (Warm) NVMe SSD ~1 ms 4-16 TB Frequently accessed
L3 (Cold) HDD / Remote ~10 ms Huge Long-tail content
Key Insight: Cache Hit Ratio

The cache hit ratio is the most important CDN metric. If 95% of requests are cache hits, only 5% go to your origin. This means your origin handles 20x less traffic! A good CDN should achieve 90-99% hit ratio for static assets.

05

Request Routing: Finding the Best Server

How does a user's request magically reach the "nearest" edge server? This is one of the most clever parts of CDN architecture! 🧭

Method 1: DNS-Based Routing

The most common approach. When you configure your domain to use a CDN, DNS queries return different IP addresses based on where the user is located.

DNS-Based Routing Flow
Step 1
User: cdn.example.com?
Step 2
CDN DNS Server
Step 3
Returns: 203.0.113.45

CDN DNS sees user is in Tokyo → Returns IP of Tokyo edge server

🌐 DNS Response Example
# User in New York queries cdn.example.com
$ dig cdn.example.com

;; ANSWER SECTION:
cdn.example.com.    60    IN    A    198.41.215.162  // NYC edge

# Same query from user in Tokyo
cdn.example.com.    60    IN    A    104.18.32.7     // Tokyo edge

The CDN's DNS servers use several signals to pick the best edge:

Method 2: Anycast Routing

A more elegant approach used by Cloudflare and others. Multiple servers around the world share the same IP address, and BGP routing automatically sends users to the nearest one!

🔧 Anycast Analogy

Imagine if every 911 call center in the country had the same phone number. When you dial 911, your call doesn't go to a random center — the phone network automatically routes it to the nearest one. That's Anycast!

Anycast: Same IP, Different Servers
NYC Edge
203.0.113.1
London Edge
203.0.113.1
Tokyo Edge
203.0.113.1

↑ All announce the SAME IP address! ↑

BGP routing automatically sends packets to the nearest server

DNS vs Anycast Comparison

Aspect DNS-Based Anycast
Setup Complexity Simpler Requires BGP expertise
Failover Speed Depends on DNS TTL (seconds-minutes) Near-instant (BGP reconvergence)
DDoS Resilience Attackers can target specific IPs Attack distributed across all PoPs
Accuracy Based on resolver location (can be wrong) Based on actual network path
✅ Best Practice

Modern CDNs often use both approaches together. Anycast for the initial connection, then DNS or cookies for session affinity if needed.

06

Cache Invalidation (The Hard Problem)

There's a famous quote in computer science: "There are only two hard things in Computer Science: cache invalidation and naming things." Let's see why cache invalidation is so tricky! 🤔

The Problem

You've cached an image at 300+ edge servers worldwide. Now you need to update it. How do you ensure all users see the new version immediately?

😱 Horror Story

A company cached their pricing page for 24 hours. They ran a flash sale with 50% off. But cached pages still showed full price! Customers who saw the sale ad clicked through to full-price pages. Chaos ensued.

Strategy 1: Cache Busting (Versioned URLs)

The most reliable approach: change the URL when the content changes.

✅ Cache Busting Example
<!-- Instead of this (hard to invalidate): -->
<link href="/styles/main.css">
<img src="/images/hero.jpg">

<!-- Do this (easy to invalidate): -->
<link href="/styles/main.v2.3.1.css">
<img src="/images/hero.abc123.jpg">

<!-- Or use query strings: -->
<link href="/styles/main.css?v=2.3.1">

When you deploy new content, you generate a new filename (using content hash or version number). Old cached content stays cached (harmlessly), new content gets cached fresh.

Strategy 2: Purge API

CDNs provide APIs to manually invalidate cached content:

🗑️ CDN Purge API Example
# Cloudflare - Purge specific files
curl -X POST "https://api.cloudflare.com/client/v4/zones/{zone}/purge_cache" \
  -H "Authorization: Bearer {token}" \
  -H "Content-Type: application/json" \
  --data '{"files":["https://example.com/images/hero.jpg"]}'

# Purge everything (nuclear option)
curl -X POST "https://api.cloudflare.com/client/v4/zones/{zone}/purge_cache" \
  --data '{"purge_everything":true}'
⚠️ Purge Limitations

Purging isn't instant! It can take 30 seconds to several minutes for a purge to propagate to all 300+ edge locations. During this window, some users see old content, others see new.

Strategy 3: Short TTLs + Stale-While-Revalidate

A clever middle ground: serve potentially stale content while fetching fresh content in the background.

⚡ Stale-While-Revalidate Header
// Content is "fresh" for 60 seconds
// After that, serve stale for up to 1 hour while revalidating in background
Cache-Control: public, max-age=60, stale-while-revalidate=3600

// How it works:
// t=0: Edge caches content
// t=30s: Request → Cache HIT (fresh)
// t=90s: Request → Cache HIT (stale, but served immediately)
//        Edge fetches fresh content from origin in background
// t=91s: Next request → Cache HIT (fresh again!)

Cache Invalidation Best Practices

The Golden Rules
  • Static assets (JS, CSS, images): Use content-hashed filenames + long max-age (1 year)
  • HTML pages: Short max-age (minutes) or no-cache + ETag validation
  • API responses: Depends on data freshness needs; often no-cache or short TTL
  • User-specific content: Cache-Control: private (never cache on CDN)
07

Beyond Caching: Security & DDoS Protection

Modern CDNs do way more than just cache content. They've evolved into comprehensive security platforms! 🛡️

DDoS Protection

CDNs are incredibly effective at absorbing DDoS attacks. Here's why:

100+ Tbps Cloudflare Network Capacity
300+ PoPs to Distribute Attack
Anycast Attack Traffic Scattered
🔧 DDoS Protection Analogy

Imagine someone trying to flood your house with water. Without a CDN, all that water hits your single front door. With a CDN, it's like having 300 front doors spread across the world — the flood is diluted to a trickle at each door.

Web Application Firewall (WAF)

CDNs can inspect and filter malicious requests before they reach your origin:

🚫 WAF Rule Example (Cloudflare)
// Block requests with SQL injection patterns
(http.request.uri.query contains "' OR '") or
(http.request.uri.query contains "1=1") or
(http.request.uri.query contains "UNION SELECT")

// Action: Block

SSL/TLS Termination

CDNs handle HTTPS encryption at the edge, which provides several benefits:

Edge Computing

The newest evolution: running your code directly on edge servers! 🚀

Platform Provider Runtime Use Cases
Workers Cloudflare V8 Isolates (JS) API routing, A/B testing, auth
Lambda@Edge AWS CloudFront Node.js, Python Request/response manipulation
Compute@Edge Fastly Wasm High-performance edge logic
⚡ Cloudflare Worker Example
// A/B test at the edge - no origin round-trip!
addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  // 50% of users see variant A, 50% see variant B
  const variant = Math.random() > 0.5 ? 'A' : 'B'
  
  const url = new URL(request.url)
  url.pathname = `/variant-${variant}` + url.pathname
  
  return fetch(url)
}
08

Major CDN Providers Compared

Let's look at the major players in the CDN space and what makes each unique. 🏆

The Big Players

CDN PoPs Best For Notable Customers
Cloudflare 330+ All-in-one (CDN + security + edge compute) Discord, Canva, Shopify
Akamai 4,200+ Enterprise, media streaming Apple, Microsoft, Adobe
AWS CloudFront 450+ AWS ecosystem integration Slack, Twitch, PBS
Fastly 90+ Real-time purging, edge compute GitHub, Stripe, New York Times
Google Cloud CDN 180+ GCP integration, ML workloads Spotify, Twitter
Bunny CDN 100+ Cost-effective, simple Indie developers, startups

Specialized CDNs

Pricing Models

💰 CDN Pricing Typically Based On:
  • Bandwidth: $/GB transferred (most common)
  • Requests: $/10,000 requests (for many small files)
  • Features: WAF, DDoS protection, edge compute often extra
  • Regions: Some charge more for Asia/South America delivery

Rough pricing comparison (as of 2025):

09

When CDNs Fail: Real-World Outages

CDNs are incredibly reliable, but when they fail, the impact is massive. Let's look at some notable outages and what we can learn from them. 💥

Notable CDN Outages

Nov 2025
Cloudflare Outage
A configuration file grew too large, crashing proxy servers. Took down ChatGPT, Discord, Spotify, and 20% of the internet for ~6 hours. Read our deep dive →
Oct 2025
AWS CloudFront / Global Outage
DNS issues in us-east-1 cascaded to CloudFront and affected Snapchat, Roblox, Disney+, and thousands of services for 15+ hours.
Jun 2021
Fastly Outage
A single customer's config change triggered a bug, taking down Reddit, Twitch, GitHub, The Guardian, and more for ~1 hour.
Jul 2020
Cloudflare Outage
A router rule deployment went wrong, causing 27 minutes of downtime for millions of sites.

Why CDN Outages Are So Impactful

The Concentration Problem

A handful of CDNs (Cloudflare, Akamai, Fastly, AWS) handle the majority of internet traffic. When one fails, it's not just one website — it's thousands. This concentration creates systemic risk. As one expert put it: "The internet was designed to be decentralized, but we've built chokepoints."

Multi-CDN Strategy

Large companies use multiple CDNs to avoid single points of failure:

🔀 Multi-CDN DNS Example
# Primary CDN (70% of traffic)
cdn.example.com.    60    IN    CNAME    example.cloudflare.com.  weight=70

# Secondary CDN (30% of traffic)
cdn.example.com.    60    IN    CNAME    example.fastly.net.      weight=30

# If Cloudflare goes down, traffic automatically shifts to Fastly
✅ Resilience Best Practices
  • Multi-CDN: Use at least 2 CDN providers for critical applications
  • Origin fallback: Configure direct-to-origin as a last resort
  • Monitor CDN health: Don't rely on CDN status pages alone
  • Graceful degradation: App should work (slowly) without CDN
10

Key Takeaways & Best Practices

Let's wrap up with the essential lessons about CDNs! 🎯

1. CDNs Exist Because Physics is Undefeated

You can't make light travel faster. CDNs solve latency by moving content closer to users, not by speeding up the network. Every millisecond matters for user experience and business metrics.

2. Cache-Control Headers Are Critical

Get your headers right! Use content-hashed URLs + long max-age for static assets. Use short TTLs + stale-while-revalidate for dynamic content. Never cache private user data on CDN.

3. Cache Hit Ratio is Your North Star Metric

Aim for 90%+ cache hit ratio on static assets. Every cache miss means a slow trip to your origin. Monitor your hit ratio and optimize cache keys accordingly.

4. CDNs Are More Than Caching

Modern CDNs provide DDoS protection, WAF, SSL termination, and edge computing. You're getting a lot of value beyond just faster content delivery.

5. Plan for CDN Failures

CDNs are reliable but not infallible. Consider multi-CDN strategies for critical applications. Test your fallback mechanisms. Don't let your app completely break if the CDN goes down.

6. Cache Invalidation is Hard

Prefer cache busting (versioned URLs) over purging. If you must purge, understand it's not instant. Use stale-while-revalidate for a better user experience during content updates.

7. Choose Based on Your Needs

Cloudflare for all-in-one simplicity. AWS CloudFront for AWS ecosystem. Fastly for real-time purging. Bunny CDN for cost-effectiveness. Akamai for enterprise scale.

Quick Reference: CDN Configuration Checklist

✅ Before Going Live
  • ☐ Configure appropriate Cache-Control headers for each content type
  • ☐ Set up content-hashed filenames for static assets
  • ☐ Enable compression (gzip/brotli)
  • ☐ Configure SSL/TLS (minimum TLS 1.2)
  • ☐ Set up WAF rules for common attack patterns
  • ☐ Test cache hit ratio with realistic traffic
  • ☐ Set up monitoring and alerting for CDN health
  • ☐ Document cache invalidation/purge procedures
  • ☐ Test fallback behavior if CDN fails
📖

References & Further Reading

Official Documentation

Technical Deep Dives

Incident Reports

Found this helpful? Share it! 🚀