Content Delivery Networks (CDN)
A CDN is a globally distributed network of servers that delivers content to users from the nearest location, reducing latency and offloading origin servers.
Benefits
Loading diagram...
| Benefit | Impact |
|---|---|
| Lower latency | Content served from nearby edge |
| Reduced bandwidth | Origin serves each asset once per edge |
| High availability | Multiple edges provide redundancy |
| DDoS protection | Distributed network absorbs attacks |
| Scalability | Handle traffic spikes without scaling origin |
How CDN Works
Loading diagram...
Push vs Pull CDN
Pull CDN (Lazy Loading)
Content is fetched from origin on first request.
Loading diagram...
| Advantages | Disadvantages |
|---|---|
| No upfront upload required | First request is slow (cache miss) |
| Automatic cache management | Origin must be available |
| Only popular content cached | TTL management complexity |
Use cases: Dynamic content, large content libraries, unpredictable traffic
Push CDN
Content is uploaded to CDN before users request it.
Loading diagram...
| Advantages | Disadvantages |
|---|---|
| No cache misses | Storage costs for all content |
| Origin can be offline | Upload latency on deploy |
| Predictable performance | Manual cache management |
Use cases: Static sites, video streaming, known content libraries
Comparison
| Aspect | Pull CDN | Push CDN |
|---|---|---|
| Setup | Configure origin URL | Upload content |
| First request | Slow (cache miss) | Fast (pre-cached) |
| Storage cost | Pay for cached only | Pay for all content |
| Origin dependency | Required for misses | Not required |
| Use cases | Dynamic, large catalogs | Static, predictable |
CDN Caching Strategies
Cache Headers
| Header | Purpose | Example |
|---|---|---|
Cache-Control | Caching directives | max-age=31536000, public |
Expires | Expiration date | Thu, 01 Dec 2024 16:00:00 GMT |
ETag | Content version | "33a64df5" |
Last-Modified | Last change time | Wed, 21 Oct 2023 07:28:00 GMT |
Cache-Control Directives
| Directive | Meaning |
|---|---|
public | Cacheable by CDN and browsers |
private | Only browser can cache (user-specific) |
max-age=N | Cache for N seconds |
s-maxage=N | CDN cache time (overrides max-age) |
no-cache | Must revalidate before using |
no-store | Never cache |
immutable | Content never changes |
Recommended TTLs
| Content Type | TTL | Cache-Control |
|---|---|---|
| Versioned assets (app.abc123.js) | 1 year | max-age=31536000, immutable |
| Images, fonts | 1 year | max-age=31536000 |
| CSS/JS (unversioned) | 1 week | max-age=604800 |
| HTML pages | 0 or short | no-cache or max-age=60 |
| API responses | Varies | private, max-age=60 |
| User-specific data | Never | private, no-store |
Cache Invalidation
Strategies
Loading diagram...
| Method | Speed | Complexity | Cost |
|---|---|---|---|
| TTL expiration | Slow | None | Free |
| URL purge | Fast | Low | Per-request |
| Wildcard purge | Fast | Low | Higher |
| Versioned URLs | Instant | Medium | None |
| Cache tags | Fast | Medium | Varies |
Versioned URLs (Recommended)
Loading diagram...
Implementation options:
- Hash content:
app.a3b2c1.js - Query parameter:
app.js?v=1.2.3 - Build timestamp:
app.js?t=1699876543
CDN Architecture Patterns
Multi-CDN
Loading diagram...
Benefits:
- Avoid vendor lock-in
- Better coverage
- Failover capability
- Cost optimization
Origin Shield
Loading diagram...
Benefits:
- Reduces origin requests
- Single point for cache warming
- Consistent cache across edges
Edge Computing
Modern CDNs support code execution at the edge.
Loading diagram...
| Provider | Edge Compute |
|---|---|
| Cloudflare | Workers |
| AWS CloudFront | Lambda@Edge |
| Fastly | Compute@Edge |
| Vercel | Edge Functions |
Use cases:
- Authentication/authorization
- A/B testing
- Personalization
- Image optimization
- Bot detection
CDN Providers Comparison
| Provider | Strengths | Use Cases |
|---|---|---|
| CloudFront | AWS integration, Lambda@Edge | AWS-based applications |
| Cloudflare | DDoS protection, Workers | Security-focused |
| Akamai | Enterprise, largest network | Large enterprises |
| Fastly | Real-time purge, VCL | Dynamic content |
| Bunny CDN | Cost-effective | Budget-conscious |
Performance Metrics
| Metric | Description | Target |
|---|---|---|
| Cache hit ratio | % requests served from cache | > 90% |
| TTFB | Time to first byte | < 100ms |
| Origin offload | % traffic not hitting origin | > 95% |
| Bandwidth saved | Data served from cache | Varies |
Cache Hit Ratio Optimization
Loading diagram...