CDN and Edge Caching
easyA CDN is a globally distributed cache that serves content from the PoP nearest each user. Originally for static assets, modern CDNs also do TLS termination, edge compute, bot management, and DDoS scrubbing.
Key Concepts
Cache-Control: max-age=N for browser; s-maxage=N overrides for shared caches. immutable says content never changes. stale-while-revalidate lets the CDN serve stale while refreshing in background. private blocks shared caching. The cache key is URL by default; including too many headers/cookies explodes key space and tanks hit rate.app.a1b2c3.js instead of app.js. The build pipeline emits hashed filenames; the URL changes when content changes. Old URLs can have infinite TTL — they'll never serve wrong content because they are the old content. Cache-bust is automatic.Approach
- Pick a CDN — Cloudflare, Fastly, Akamai, AWS CloudFront, GCP Cloud CDN. Compare PoP coverage in your user geographies.
- Identify content tiers: immutable (long TTL, versioned URL), semi-dynamic (short TTL, stale-while-revalidate), dynamic (no cache, edge compute).
- Use versioned URLs for immutable assets. Build pipeline generates hashed filenames.
- Set Cache-Control + s-maxage + stale-while-revalidate appropriately.
- Enable origin shield to consolidate cache-miss traffic.
- Use edge compute for personalization, geo routing, simple auth, A/B test bucketing.
- Terminate TLS at edge; enable HTTP/2 and HTTP/3.
- Configure cache key carefully — include only headers that meaningfully vary content.
- Monitor cache hit ratio, origin offload, p50/p99 latency per region.
- Configuration as code (Terraform, Pulumi, vendor SDK).
Components
- Edge PoPs with cache + TLS terminator + HTTP/2/3 parsers.
- Cache hierarchy: edge → regional shield → origin shield → origin.
- DNS-based routing (GeoDNS) or anycast (most modern CDNs).
- Purge API for explicit invalidation; tag-based purges if vendor supports.
- Edge compute runtime (Workers, Lambda@Edge, Compute@Edge).
- WAF rules and bot management.
- Real-time analytics — per-PoP hit ratio, origin offload, cache miss reasons.
Cache invalidation strategies
Versioned URLs (best): change the URL when content changes. Old URLs keep working with long TTL. Build pipeline does the rename.
Tag-based purge: tag content with logical groups; purge by tag (e.g., purge all 'product-123' content). Fast, scoped.
Path-based purge: purge specific paths or prefixes.
Soft purge: mark stale, serve stale-while-revalidate. Smooth invalidation under load.
Full purge: nuclear option. Slow, expensive, only for emergencies.
Avoid relying on TTLs alone for freshness-critical content; combine with one of the above.
Trade-offs
Long TTL = high hit rate, stale risk. Short TTL = freshness, origin pressure.
Versioned URLs solve both: immutable + new URL on change. Strongly preferred.
Edge compute reduces latency but limits runtime (CPU budget, memory cap, no native libs).
Push CDN warms popular assets but requires pre-knowledge of hot content. Pull CDN is more common and self-tuning.
Multiple cache keys (per-cookie, per-header) tank hit rate. Normalize aggressively.
Cache hierarchy adds RTT but absorbs cache misses; net win at scale.
Real-world patterns
- Netflix Open Connect: in-house CDN with caches embedded inside ISPs.
- Cloudflare: anycast edge with Workers (V8-isolated edge compute).
- Fastly: VCL-programmable edge with Compute@Edge (Wasm).
- Spotify: aggressive use of versioned URLs + multi-tier cache.
- Twitch: hybrid CDN + P2P (peer-assisted delivery) for very high concurrency events.
- Shopify: Cloudflare in front of Rails origin with cache tags per shop/product.