Free — no signup required

CDN Cache Invalidation

2 min read

The Hardest Problem in Computer Science

Phil Karlton famously said: "There are only two hard things in Computer Science: cache invalidation and naming things." CDN cache invalidation is a perfect example of why.

When you update content on your origin server — a new product image, a corrected article, an updated JavaScript bundle — CDN POPs around the world still hold the old cached version. They won't check for updates until the TTL expires. If your TTL is set to 24 hours and you push a critical bug fix, users could see the broken version for up to a day.

Strategies for managing this:

1. Cache Purge / Invalidation API
Every major CDN (Cloudflare, Fastly, AWS CloudFront, Akamai) provides an API to explicitly invalidate cached objects before their TTL expires. You call the API with the URL(s) to purge, and the CDN propagates the invalidation to all POPs within seconds.

2. Cache-Busting via Versioned URLs
Instead of updating style.css in place, deploy it as style.v2.css or style.abc123.css (where abc123 is a hash of the file content). The old URL still serves the old file (which is fine — nothing links to it anymore). The new URL is a cache miss everywhere and fetches fresh content immediately. This is the most reliable approach for static assets and is standard practice in modern frontend build pipelines.

3. Short TTLs with Stale-While-Revalidate
Set a short TTL (e.g., 60 seconds) so content refreshes frequently, but use the stale-while-revalidate HTTP directive to allow POPs to serve slightly stale content while fetching a fresh copy in the background. This avoids cache-miss latency spikes while keeping content relatively fresh.

Cache-Control: max-age=60, stale-while-revalidate=300

The fundamental tension: Long TTLs improve cache hit rates and reduce origin load, but make it harder to push updates quickly. Short TTLs make updates propagate faster but increase origin traffic and reduce CDN effectiveness. The right balance depends on how frequently your content changes and how critical it is to serve the latest version immediately.

Key Point: Cache invalidation is an active operational concern, not a set-and-forget configuration. Versioned URLs are the most reliable strategy for static assets; purge APIs handle dynamic content updates.

This is one of 18 chapters

Every chapter is free — Kubernetes, Terraform, SRE, distributed systems, and more — with fast daily review built in.

Sign up free