MongoDB
Content Delivery Networks (CDN) Explained
Understand how CDNs work — edge servers, origin pull, cache headers, invalidation, and how to design CDN-first applications.
S
srikanthtelkalapally888@gmail.com
Content Delivery Networks Explained
A CDN distributes content to edge servers globally, delivering it from the location closest to the user.
Why CDN?
Without CDN:
User (Mumbai) → Server (US-East) = ~200ms
With CDN:
User (Mumbai) → CDN Edge (Mumbai) = ~5ms
How CDN Works
Cache Miss (First Request)
User → CDN Edge → Origin Server
↓
CDN caches response
Return to user
Cache Hit (Subsequent Requests)
User → CDN Edge → Return cached response
Cache Control Headers
Cache-Control: public, max-age=86400, stale-while-revalidate=3600
ETag: "abc123"
max-age: Cache for 24 hoursstale-while-revalidate: Serve stale while fetching freshETag: Content fingerprint for validation
Cache Invalidation
1. URL versioning: /app.v2.js (recommended)
2. API invalidation: CDN API to purge specific URLs
3. TTL: Wait for expiry
CDN for Dynamic Content
- Edge Side Includes (ESI): Cache fragments of pages
- Cloudflare Workers: Run logic at the edge
- Lambda@Edge: AWS edge functions
Push vs Pull CDN
Pull CDN: Fetches content from origin on demand (most common)
Push CDN: Content pre-uploaded to CDN nodes (large files, assets)
Major CDN Providers
| Provider | Strength |
|---|---|
| Cloudflare | Security + performance |
| AWS CloudFront | Deep AWS integration |
| Akamai | Enterprise, largest network |
| Fastly | Real-time purging, edge compute |
Conclusion
CDNs are essential for global performance. Design assets with long cache TTLs + URL versioning, and use edge compute for dynamic personalization.