Skip to content

11 — CDN Design

A Content Delivery Network (CDN) is a geographically distributed network of proxy servers and data centers that delivers content to users from the nearest edge location. CDNs are essential for global applications with users around the world.

Analogy: A CDN is like having local warehouses in every city instead of one central warehouse. When a customer orders a product, it ships from the nearest warehouse — arriving much faster than shipping from across the country.


Without a CDN:

  • Users far from your server experience high latency
  • Your origin server handles every request, limiting scale
  • A single server location has no redundancy
  • DDoS attacks hit your origin directly
  • Bandwidth costs are higher (no edge caching)

flowchart TB
Users["🌍 Global Users"] --> DNS["DNS<br/>Geo-routing"]
DNS --> Edge1["Edge: US East"]
DNS --> Edge2["Edge: Europe"]
DNS --> Edge3["Edge: Asia"]
DNS --> Edge4["Edge: Australia"]
Edge1 & Edge2 & Edge3 & Edge4 --> Origin["Origin Server<br/>Primary data source"]
subgraph Edge["Edge Server Cache"]
Static["Static: Images, CSS, JS<br/>Long TTL (1 year)"]
Dynamic["Dynamic: API, HTML<br/>Short TTL (60s)"]
end
style Users fill:#f59e0b,color:#fff
style DNS fill:#3b82f6,color:#fff
style Origin fill:#ef4444,color:#fff
style Edge fill:#7c3aed,color:#fff

ComponentDescription
Edge ServerServer at a CDN point-of-presence (PoP) — caches and serves content
Origin ServerThe primary source of truth for content
PoP (Point of Presence)A data center location where CDN servers are deployed
DNS RoutingRoutes users to the nearest PoP based on geolocation
Origin ShieldAn intermediate caching layer that protects origin from edge storms

flowchart TB
Content["Content Type"] --> Static["Static Content<br/>Images, CSS, JS, Video<br/>Same for all users"]
Content --> Dynamic["Dynamic Content<br/>API responses, HTML<br/>Personalized per user"]
Static --> StaticCDN["CDN Strategy<br/>Cache at edge for long TTL<br/>Version URLs for cache busting<br/>Immutable content = max-age: 1 year"]
Dynamic --> DynamicCDN["CDN Strategy<br/>Cache for short TTL (60s)<br/>Cache headers: no-cache for sensitive data<br/>Edge Workers for custom logic"]
style Content fill:#7c3aed,color:#fff
style Static fill:#3b82f6,color:#fff
style Dynamic fill:#f59e0b,color:#fff
style StaticCDN fill:#059669,color:#fff
style DynamicCDN fill:#ef4444,color:#fff

sequenceDiagram
participant User as User Browser
participant DNS as CDN DNS
participant Edge as Edge Server
participant Shield as Origin Shield
participant Origin as Origin Server
User->>DNS: www.example.com/image.jpg
DNS-->>User: Server IP (nearest PoP)
User->>Edge: GET /image.jpg
alt Cache Hit
Edge-->>User: 200 + image (from cache) ⚡
else Cache Miss
Edge->>Shield: Check shield cache
alt Shield Hit
Shield-->>Edge: image from shield cache
else Shield Miss
Shield->>Origin: GET /image.jpg
Origin-->>Shield: 200 + image
Shield->>Shield: Cache image
end
Edge->>Edge: Cache image (TTL: configurable)
Edge-->>User: 200 + image
end

StrategyCache HeaderUse Case
Versioned assetsCache-Control: public, max-age=31536000, immutableJS/CSS bundles with hash in filename
Non-versioned assetsCache-Control: public, max-age=86400Images without versioning
Dynamic HTMLCache-Control: public, max-age=60News pages, blog posts
User-specificCache-Control: private, max-age=0Account pages, dashboard
No cacheCache-Control: no-cache, no-storeAPIs with sensitive data
Stale while revalidateCache-Control: public, max-age=60, stale-while-revalidate=3600Serve stale, async refresh

PatternDescriptionExample
Static OffloadServe images, CSS, JS from CDNMost common pattern
Dynamic AccelerationOptimize routing for dynamic contentMulti-region API with CloudFront
Edge ComputingRun code at CDN edge (Workers, Lambda@Edge)A/B testing, header modification
DDoS ProtectionCDN absorbs DDoS traffic before originCloudflare, AWS Shield
Geo-blockingBlock/allow content by regionLicensing restrictions
Origin PullCDN pulls content from origin on missStandard CDN setup
Push CDNContent pushed to CDN proactivelyLarge media files

ProviderKey FeaturesGlobal PoPs
CloudFront (AWS)Deep AWS integration, Lambda@Edge450+
CloudflareDDoS protection, Workers edge compute330+
AkamaiEnterprise-grade, advanced routing4,100+
FastlyInstant Purge, VCL configuration100+
Google Cloud CDNGoogle network, GCP integration200+

DecisionProsCons
CDN for everythingFast globally, origin offloadStale content risk, cost
Short TTLFresh contentMore origin requests
Long TTL + versioned URLsMaximum cache hit rateBuild process complexity
Edge computeLogic at edge, less origin latencyVendor lock-in, debugging hard

StrategyDescription
Multiple CDN providersRedundancy, better global coverage
Origin ShieldSingle point of contact from CDN to origin (reduces origin load)
Pre-fetch/pre-warmPush popular content to CDN before traffic hits
Cache key optimizationInclude relevant headers only (not all cookies)
Tiered cacheEdge → Regional → Origin Shield

  1. How does a CDN reduce latency for global users?
  2. What’s the difference between static and dynamic content caching?
  3. How do you invalidate a CDN cache when content changes?
  4. What is origin shielding and why is it important?
  5. How would you design the caching strategy for a global video streaming platform?

SystemCDN Strategy
NetflixOpen Connect CDN — custom appliances in ISP data centers
YouTubeGoogle Global Cache — edge nodes in 1,000+ locations
AmazonCloudFront for e-commerce assets, dynamic content acceleration
FacebookCustom CDN for images, videos — pre-caching popular content

  • CDN = your content copied to servers worldwide — users get it from the nearest one
  • Static content (images, CSS, JS) = cache for a long time, use versioned URLs
  • Dynamic content (API, HTML) = cache for short time or use edge workers
  • CDN offloads your origin server — it serves most of the traffic, not your backend
  • Cache invalidation is hard — versioned URLs are the safest approach
  • DDoS protection is a major bonus — CDN absorbs attacks before they reach you
  • Use origin shield to prevent traffic storms when many edges miss cache simultaneously