Content Delivery Network (CDN)
BeginnerA CDN is a geographically distributed network of edge servers that cache and serve static content close to users, reducing latency and offloading origin servers.
Overview
A Content Delivery Network (CDN) places copies of your content — images, CSS, JS, videos — on edge servers distributed worldwide. When a user requests a resource, the CDN routes them to the nearest edge server (Point of Presence / PoP). If the edge has the content cached, it serves it directly (cache hit); otherwise, it fetches from the origin server, caches it, and serves it (cache miss). CDNs reduce latency (users are physically closer to edge servers), decrease origin load (most requests never reach your servers), improve availability (edge servers provide redundancy), and protect against DDoS (distributed absorption). Major CDNs include CloudFront, Cloudflare, Akamai, and Fastly. Modern CDNs also support dynamic content acceleration, edge compute (Cloudflare Workers, Lambda@Edge), and WebSocket proxying.
How a CDN Works
DNS resolves to the nearest CDN edge via anycast or geo-DNS. The edge checks its cache. On a hit, it serves the cached response. On a miss, it pulls from the origin, caches the response (respecting Cache-Control headers), and serves the user.
// CDN request flow
//
// User (Mumbai) → DNS → CDN Edge (Mumbai PoP)
// │
// ▼
// Cache HIT? ──yes──→ Serve from edge (< 20ms)
// │
// no
// │
// ▼
// Fetch from Origin (us-east-1) → Cache at edge → Serve user
//
// Subsequent requests from Mumbai region → served from edge cache
// Cache-Control headers control CDN behaviour
Cache-Control: public, max-age=86400, s-maxage=604800
// max-age=86400 → browser caches for 1 day
// s-maxage=604800 → CDN caches for 7 days
// public → CDN is allowed to cache this
// Invalidation
// POST /invalidation { paths: ["/images/*", "/css/main.css"] }Push vs Pull CDN
Pull CDNs fetch content from origin on demand — simpler, ideal for dynamic or frequently changing content. Push CDNs require you to upload content to edge servers proactively — better for large static files where you control the distribution schedule.
// Pull CDN (most common — CloudFront, Cloudflare)
// 1. User requests asset
// 2. Edge checks cache → miss → pulls from origin
// 3. Edge caches + serves
// Good for: websites, APIs, assets that change often
// Push CDN (e.g. S3 + CloudFront with origin groups)
// 1. You upload/deploy assets to CDN storage
// 2. CDN distributes to edges proactively
// 3. User requests → always served from edge
// Good for: large videos, software downloads, predictable content
// CloudFront distribution (Terraform)
resource "aws_cloudfront_distribution" "app" {
origin {
domain_name = aws_s3_bucket.static.bucket_regional_domain_name
origin_id = "s3-static"
}
default_cache_behavior {
target_origin_id = "s3-static"
viewer_protocol_policy = "redirect-to-https"
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
compress = true
default_ttl = 86400
}
price_class = "PriceClass_200" # edge locations in US, EU, Asia
}Edge Compute
Modern CDNs run code at the edge — Cloudflare Workers, Lambda@Edge, Fastly Compute@Edge. This enables personalisation, A/B testing, authentication, and API responses at the edge without round-tripping to origin.
// Cloudflare Worker — edge-side A/B testing
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const cookie = request.headers.get('Cookie') || ''
let variant = cookie.includes('ab=B') ? 'B' : 'A'
// New users: random assignment
if (!cookie.includes('ab=')) {
variant = Math.random() < 0.5 ? 'A' : 'B'
}
const url = variant === 'B'
? 'https://origin.example.com/landing-v2'
: 'https://origin.example.com/landing-v1'
const response = await fetch(url, request)
const newResponse = new Response(response.body, response)
newResponse.headers.set('Set-Cookie', `ab=${variant}; Path=/; Max-Age=86400`)
return newResponse
}Key Points to Remember
- 1CDNs cache content at edge servers close to users — reducing latency from hundreds of ms to single-digit ms.
- 2Pull CDNs fetch on demand (simpler); Push CDNs distribute proactively (better for large static assets).
- 3Cache-Control headers (max-age, s-maxage) control what CDNs cache and for how long.
- 4Edge compute (Workers, Lambda@Edge) enables running logic at the CDN layer without origin round-trips.
- 5CDNs also provide DDoS protection, TLS termination, and compression.
Interview Questions
Sign in to ask AriaHow does a CDN reduce latency for users?
What is the difference between push and pull CDN?
How would you invalidate a cached asset across all CDN edge servers?
When should you NOT use a CDN?
Design a CDN strategy for a video streaming platform serving 50 countries.
Ask Aria about Content Delivery Network (CDN)
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.