Chapter 05 / 24 · System Design

The edge: CDNs and static delivery

The fastest request is one that never crosses an ocean; the edge is how you arrange that at scale.

Foundations proved the expensive thing about networks is distance, and no vendor sells shorter light-paths. The CDN is the industry’s answer: if the bytes cannot travel faster, station copies of them near everyone. This chapter is how that actually works — and the header-level mechanics that decide whether it works for you.

The machinery

A CDN is hundreds of points of presence — cache clusters parked at internet exchange points — plus routing that lands each user on a nearby one, usually via anycast: every PoP announces the same IP, and internet routing itself delivers packets to the closest. A request hits the edge; on a miss it walks up a cache hierarchy and ultimately to your origin. The round-trip arithmetic is the sale: 150 ms cross-continent becomes 15 ms to the edge — and for HTTPS, the TLS handshakes happen at the edge too, so even uncacheable API traffic gets faster by terminating nearby and riding the CDN’s warm, fat pipes to origin.

The headers do the work

The CDN obeys your origin’s HTTP headers, so caching quality is a code review, not a vendor setting. Cache-Control carries the policy: max-age (how long anyone may cache), s-maxage (CDN-specific override), public/private (may shared caches hold it), no-store (never). ETag enables cheap revalidation — “I have version abc123, still current?” → 304 Not Modified, headers only, no body. And the modern winner for HTML: stale-while-revalidate — serve the stale copy instantly, refresh behind the scenes; users get edge latency, origin gets background traffic, and staleness is bounded and chosen.

Invalidation, and the strategy that skips it

Purging a URL from hundreds of PoPs is slow, eventually-consistent, and — recall the reads chapter — one of the two hard problems. The professional pattern opts out: versioned URLs. Fingerprint assets by content (app.9f2c1a.js), cache them for a year as immutable, and “invalidate” by shipping HTML that references the new name. The mutable surface shrinks to HTML itself — short TTL plus stale-while-revalidate — and purges become an emergency tool, not a workflow. For private content, signed URLs (expiring, tamper-proof query signatures) let the CDN serve authorised bytes without holding your auth logic; the blob-storage chapter leans on exactly this.

Where it bites

  • One user’s private response cached as public and served to another — the classic CDN incident; audit cacheability per route.
  • A hot asset expiring everywhere at once — the miss storm hits origin; jitter TTLs, use origin shielding (one designated PoP collapses the herd).
  • Purge-based deploys: version instead, and stop fighting propagation.
  • Treating the CDN as static-only — edge TLS termination for APIs is often the cheapest 30–80 ms you will ever save.