What is a CDN?
A Content Delivery Network (CDN) is a distributed network of servers strategically placed around the world to deliver content to users from the geographically closest location, minimizing latency and improving performance.
Think of it like having warehouse stores in every city instead of one central warehouse across the country.
The Problem: Distance = Latency
User in Tokyo requests image from New York server: Physical distance: ~11,000 km Speed of light in fiber: ~200,000 km/s Minimum latency: 55ms (one way) Round trip: 110ms+ Add processing time: 150-300ms total latency
With CDN:
User in Tokyo ā Tokyo edge server (same city) Distance: ~50 km Latency: ~5-10ms
How CDN Works
graph TB
User[š¤ User in Tokyo] -->|1. Request| Edge[CDN Edge Server<br/>Tokyo]
Edge -->|2. Cache Hit?| Check{Has Content?}
Check -->|Yes| Return[3. Serve from cache<br/>ā” 10ms]
Check -->|No| Origin[4. Fetch from Origin<br/>New York Server]
Origin -->|5. Cache & Return| Edge
Edge -->|6. Serve to user| User
Detailed Flow
- User request: User in Tokyo requests
example.com/logo.png - DNS resolution: DNS returns CDN edge server IP (Tokyo)
- Edge check: Tokyo edge server checks local cache
- Cache hit: If found, serve immediately (10ms)
- Cache miss: If not found:
- Fetch from origin server (New York)
- Cache locally
- Serve to user
- Future requests served from cache
Push vs Pull CDNs
Push CDN
You manually upload content to CDN servers.
# Example: Upload to CDN aws s3 sync ./dist s3://my-cdn-bucket --acl public-read # CloudFront invalidation aws cloudfront create-invalidation \ --distribution-id E1234 \ --paths "/*"
When to use:
- Small sites with infrequent updates
- Static content that rarely changes
- Want full control over what's cached
Pros:
- Predictable: content only available when you push it
- No surprise origin bandwidth costs
- Control cache timing precisely
Cons:
- Manual workflow
- Deployment complexity
- Stale content if you forget to update
Examples: Netlify, Vercel deploy-time CDN
Pull CDN (Origin Pull)
CDN automatically fetches content from origin on first request.
# Origin server configuration
server {
listen 80;
server_name origin.example.com;
location / {
root /var/www;
# CDN will pull from here
}
}
When to use:
- Dynamic sites with frequent updates
- Large content libraries
- Don't want to manage uploads
Pros:
- Zero maintenance
- Automatic updates
- Scales easily
Cons:
- First request is slow (cache miss)
- Can overwhelm origin on cache purge
- Harder to predict costs
Examples: Cloudflare, AWS CloudFront (default)
Caching Strategies
1. Cache-Control Headers
# Origin server response HTTP/1.1 200 OK Content-Type: image/png Cache-Control: public, max-age=31536000, immutable
Common patterns:
// Static assets (never change) "Cache-Control": "public, max-age=31536000, immutable" // 1 year cache, never revalidate // Dynamic content "Cache-Control": "public, max-age=3600, must-revalidate" // 1 hour cache, revalidate before serving stale // Never cache "Cache-Control": "no-store, no-cache, must-revalidate"
2. Cache Busting (Versioning)
<!-- Without versioning --> <link rel="stylesheet" href="/style.css"> <!-- CDN caches for 1 year, updates invisible to users --> <!-- With versioning --> <link rel="stylesheet" href="/style.abc123.css"> <!-- New deploy = new filename = new cache entry -->
Webpack/Vite auto-versioning:
// vite.config.js
export default {
build: {
rollupOptions: {
output: {
assetFileNames: 'assets/[name].[hash].[ext]'
}
}
}
}
3. Purge/Invalidation
# Cloudflare API
curl -X POST "https://api.cloudflare.com/client/v4/zones/{zone_id}/purge_cache" \
-H "Authorization: Bearer {token}" \
-d '{"files":["https://example.com/style.css"]}'
# AWS CloudFront
aws cloudfront create-invalidation \
--distribution-id E123456789 \
--paths "/index.html" "/style.css"
Cost consideration: CloudFront charges per invalidation (first 1,000/month free)
CDN Architecture
Edge Servers (Points of Presence - PoPs)
Global CDN Network: - North America: 50+ PoPs - Europe: 40+ PoPs - Asia: 35+ PoPs - South America: 10+ PoPs - Africa: 5+ PoPs - Oceania: 5+ PoPs Total: 150+ locations worldwide
Multi-Tier Caching
graph LR
User[User] --> Edge[Edge Server<br/>Tier 1]
Edge -->|miss| Regional[Regional Server<br/>Tier 2]
Regional -->|miss| Origin[Origin Server]
Benefits:
- Tier 1 (Edge): Ultra-low latency, small cache
- Tier 2 (Regional): Larger cache, reduces origin load
- Origin: Only hit if both tiers miss
Real-World CDN Providers
Cloudflare
// Cloudflare Workers (Edge computing)
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
// Run code at CDN edge
const cache = caches.default
let response = await cache.match(request)
if (!response) {
response = await fetch(request)
// Cache for 1 hour
response = new Response(response.body, response)
response.headers.set('Cache-Control', 'max-age=3600')
event.waitUntil(cache.put(request, response.clone()))
}
return response
}
Features:
- Free tier (unlimited bandwidth!)
- DDoS protection
- Web Application Firewall (WAF)
- Edge computing (Workers)
- 200+ PoPs
AWS CloudFront
// CloudFront distribution config
const distribution = new aws.cloudfront.Distribution({
origins: [{
domainName: "origin.example.com",
originId: "myOrigin"
}],
defaultCacheBehavior: {
targetOriginId: "myOrigin",
viewerProtocolPolicy: "redirect-to-https",
allowedMethods: ["GET", "HEAD"],
cachedMethods: ["GET", "HEAD"],
forwardedValues: {
queryString: false,
cookies: { forward: "none" }
},
minTTL: 0,
defaultTTL: 86400, // 24 hours
maxTTL: 31536000 // 1 year
},
priceClass: "PriceClass_100" // US, Europe
})
Features:
- Integration with AWS services
- Lambda@Edge (serverless edge functions)
- Real-time logs
- Geo-restriction
Fastly
# Fastly VCL (Varnish Configuration Language)
sub vcl_recv {
# Custom logic at edge
if (req.url ~ "^/api/") {
return(pass); # Don't cache API requests
}
}
sub vcl_fetch {
if (beresp.status == 200) {
set beresp.ttl = 1h;
}
}
Features:
- Instant purge (40ms worldwide)
- Real-time analytics
- Edge computing
- Used by: Stripe, GitHub, Shopify
Performance Benefits
Latency Reduction
Without CDN: User (Sydney) ā Server (New York) Distance: 16,000 km Latency: 200-300ms With CDN: User (Sydney) ā Edge (Sydney) Distance: 50 km Latency: 5-15ms Improvement: 95% latency reduction
Bandwidth Cost Reduction
Without CDN: 1M requests Ć 1MB file = 1TB origin bandwidth Cost: ~$90/TB = $90 With CDN (95% cache hit rate): 50,000 origin requests Ć 1MB = 50GB origin bandwidth Cost: ~$4.50 Savings: $85.50 (95% reduction)
Origin Server Protection
Traffic spike: 10,000 req/sec Without CDN: 10,000 req/sec ā Origin server Result: Server crash With CDN (90% cache hit): 1,000 req/sec ā Origin server 9,000 req/sec ā Served from cache Result: Server handles load easily
Security Benefits
DDoS Protection
DDoS Attack: 1,000,000 req/sec CDN behavior: - Absorb attack at edge (distributed across 200+ PoPs) - Rate limiting - Geographic filtering - Only 5,000 legitimate req/sec ā Origin Origin: Protected, serves normal traffic
SSL/TLS Termination
Client āā CDN āā Origin HTTPS (encrypted) āā HTTPS (edge) āā HTTP (internal) Benefits: - Free SSL certificates (Let's Encrypt) - Latest TLS protocols at edge - Origin doesn't need SSL overhead
CDN Use Cases
1. Static Website Hosting
# Netlify (CDN + hosting) build: publish: dist command: npm run build # Automatically deploys to global CDN
2. Video Streaming
// HLS video streaming via CDN <video> <source src="https://cdn.example.com/video/stream.m3u8" type="application/x-mpegURL"> </video> // CDN serves video chunks https://cdn.example.com/video/chunk-001.ts https://cdn.example.com/video/chunk-002.ts
Netflix's CDN: Open Connect (custom CDN in ISP datacenters)
3. Software Distribution
# NPM packages via CDN <script src="https://cdn.jsdelivr.net/npm/react@18/umd/react.production.min.js"></script> # Benefits: # - Browser cache across sites # - No npm install needed # - Instant availability
4. API Acceleration
// Cache API responses at edge
app.get('/api/products', (req, res) => {
res.set('Cache-Control', 'public, max-age=300') // 5 min
res.json(products)
})
// CDN caches response for 5 minutes
// Reduces database queries by 99%
Interview Tips š”
When discussing CDNs in system design interviews:
- Explain the problem: "Users in Asia experience 300ms latency fetching from US servers..."
- Propose CDN: "We'd use a CDN like Cloudflare to cache static assets globally..."
- Discuss cache strategy: "Images cached for 1 year with versioned filenames, HTML cached for 5 minutes..."
- Mention invalidation: "On deploy, we'd invalidate HTML and CSS via API..."
- Security benefits: "CDN provides DDoS protection and WAF as bonus..."
- Cost optimization: "95% cache hit rate reduces origin bandwidth by$85/month..."
Related Concepts
- Caching Strategies ā Cache invalidation and patterns
- Load Balancing ā Distribute traffic across origins
- Reverse Proxy ā CDNs are specialized reverse proxies
- HTTP Caching ā Cache-Control headers
- Edge Computing ā Running code at CDN edge
About ScaleWiki
ScaleWiki is an interactive educational platform dedicated to demystifying distributed systems, software architecture, and system design. Our mission is to provide high-quality, technically accurate resources for software engineers preparing for interviews or solving complex scaling challenges in production.
Read more about our Editorial Guidelines & Authorship.
Educational Disclaimer: The architectural patterns and system designs discussed in this article are based on common industry practices, technical whitepapers, and public engineering blogs. Actual implementations in enterprise environments may vary significantly based on specific product requirements, legacy constraints, and evolving technologies.
Related Articles
Graph Databases
Complete guide to graph databases treating relationships as first-class citizens, covering property graphs, Cypher query language, graph algorithms, and production implementations in Neo4j, Amazon Neptune powering social networks, fraud detection, and recommendation systems.
DNS Architecture
The phonebook of the internet. How Domain Name System works, the hierarchy of Route 53, and recursive vs iterative resolution strategies.
HTTP Evolution (H1 to H3)
From text-based HTTP/1.1 to binary HTTP/2 and UDP-based HTTP/3 (QUIC). Why we needed upgrades and how they solve Head-of-Line Blocking.