System Design Fundamentals

Lesson 8 of 12 · 16 min

x
67%

Content Delivery Networks

A CDN is a globally distributed network of edge servers that cache content close to users. Instead of every request crossing the world to your origin server, a user in Tokyo hits a CDN node in Tokyo and gets a cached response in milliseconds. This reduces latency for static assets and offloads traffic from your origin. Modern CDNs do more than cache files. Edge compute (Cloudflare Workers, Lambda@Edge, Vercel Edge Functions) runs code at CDN nodes — A/B testing, authentication, personalisation — without a round trip to origin. Dynamic content can be cached at the edge too using short TTLs and stale-while-revalidate: serve the cached version instantly, refresh it in the background.

Before
No CDN — every user hits origin
User (Tokyo) → Origin Server (US East) → response
Round-trip latency: ~150ms
All traffic hits origin — static and dynamic
Image assets re-fetched on every cold load
After
CDN — assets and short-lived API responses cached at edge
User (Tokyo) → CDN Edge (Tokyo) → ~8ms cached response

// Immutable assets — cache forever at edge
Cache-Control: public, max-age=31536000, immutable

// API responses — cache briefly, serve stale while refreshing
Cache-Control: s-maxage=60, stale-while-revalidate=30

Key Takeaway

Put a CDN in front of everything — cache immutable assets forever, cache dynamic responses briefly with stale-while-revalidate.

PreviousNext Lesson