Scenario: You own a pizza restaurant in New York. Customers order from:
Method A: Centralized Kitchen
Method B: Distributed Kitchens
Question: Which method serves all customers faster?
Definition: A CDN is a network of servers distributed globally that store copies of your content closer to users.
Without CDN (Origin Server Only):
User in Tokyo requests cat video from US server:

Round trip:
With CDN (Edge Servers):
User in Tokyo requests cat video:
Step 1: Request goes to nearest CDN edge server

If cached:
If NOT cached (first request): Tokyo 🇯🇵 ─── 🇯🇵 Tokyo CDN ─────────── 🇺🇸 Origin (10ms) (150ms)

Visual map:

How it works:
✅ Perfect for CDN:
❌ Not for CDN:
Example: E-commerce site
CDN (Cached): ✅ Product images ✅ Logo, icons ✅ CSS stylesheets ✅ JavaScript files ✅ Product videos
Origin Server (Dynamic): ❌ User account info ❌ Shopping cart ❌ Checkout process ❌ Inventory counts ❌ Personalized recommendations
Performance improvement:
Without CDN: New York → California Server: 70ms latency Tokyo → California Server: 150ms latency London → California Server: 130ms latency
With CDN: New York → New York Edge: 5ms latency (14x faster!) Tokyo → Tokyo Edge: 10ms latency (15x faster!) London → London Edge: 8ms latency (16x faster!)
Bandwidth savings:
Without CDN: 1 million requests × 2 MB image = 2 TB from origin Cost: $200/TB = $400
With CDN (90% cache hit): 100k requests × 2 MB = 200 GB from origin 900k served from cache (free!) Cost: $200/TB × 0.2 TB = $40
Savings: $360 (90% cheaper!) 💰
Decide which should be served from CDN vs Origin:
| Content | CDN or Origin? | Why? |
|---|---|---|
| Company logo (logo.png) | ? | ? |
| User's profile picture | ? | ? |
| jQuery library (3rd party) | ? | ? |
| Shopping cart total | ? | ? |
| Product catalog page | ? | ? |
| Live chat messages | ? | ? |
Answers:
Company Logo: CDN ✅
User Profile Picture: CDN ✅
jQuery Library: CDN ✅✅✅
Shopping Cart Total: Origin ❌
Product Catalog: Hybrid 🤔
Live Chat Messages: Origin ❌
Cloudflare CDN Setup:
AWS CloudFront: (CDN provider by Amazon)
// Configure CloudFront distribution
// URLs automatically routed through CloudFront//
https://d1234.cloudfront.net/images/product.jpg |
Control how CDN caches your content:
Cache-Control directives:
public - Anyone can cache
private - Only browser can cache (not CDN)
no-cache - Must revalidate before using
no-store - Never cache
max-age=3600 - Cache for 1 hour
immutable - Never changes, cache forever
must-revalidate - Check with origin if expired
Problem: You update logo.png on origin, but CDN still serves old cached version!
Solution 1: Wait for TTL to expire
❌ Slow! Could take hours/days Not acceptable for urgent updates
Solution 2: Cache Busting (Versioning)
✅ Best practice! CDN sees new URL, fetches new file
Solution 3: Manual Purge/Invalidation
Cloudflare 🌐
Cloudflare as a free tier available
It has availability across 200+ locations worldwide
DDoS protection included
It is considered great for small-medium sites
Amazon CloudFront ☁️
Cloud front is amazon’s Pay-as-you-go cdn service
Integrates with AWS
400+ edge locations (high availability)
Enterprise-grade
Fastly ⚡
Real-time purging
Edge computing (VCL)
Premium performance
Used by: GitHub, Shopify
Akamai 🏢
Largest CDN (4,000+ locations)
Enterprise focus
Most expensive
Used by: Apple, Facebook