CDN & CACHE INVALIDATION
You purged the origin, and the edge is still serving yesterday
Edge E2 sends a request for /app.js at 590 s. The origin answers with v1 but the response is slow. You deploy v2 at 600 s and purge every edge at 601 s. The v1 response reaches E2 at 605 s with max-age 3600. What does E2 serve at 700 s?
A CDN edge caches what the origin sends for as long as max-age allows. A deploy changes the origin, not the edges. A purge tells the edges, but a purge is a delete, so it races like one: a fetch that left before the deploy can come back after the purge and refill the old bytes.
Versioned URLs avoid the race. Each build ships /app.<hash>.js, cached forever, and only the HTML that names it must be fresh. Making the HTML always revalidate makes you depend on the origin, so add stale-if-error to serve the last copy when the origin fails.
# origin
GET /app.js Cache-Control: max-age=3600
# deploy script
rsync build/ origin:/srv/
cdn purge --all # races fetches already in flight;
# edges you forgot keep v1 for an hour# origin
GET / Cache-Control: max-age=0, stale-if-error=86400
GET /app.3f9a1c.js Cache-Control: max-age=31536000, immutable, stale-if-error=86400
# deploy: upload new hashed assets first, then switch the HTML.
# Old URLs keep their old bytes, so a late fill of /app.<old>.js is harmless.
# No purge needed; during an origin outage the edge serves its last copy. All four scenarios have the same deploy, the same slow fetch at E2 and the same origin outage at 800 s. Watch the EDGE boxes. NO PURGE keeps v1 everywhere. PURGE RACE keeps v1 on E2 only. VERSIONED URLS is never stale but passes the outage through. VERSIONED + STALE-IF-ERROR keeps both invariants green.
Versioned URLs and stale-if-error survive the deploy and the outage. Inject one operational fault so that some page load gets a 502.
GOAL: a page load gets a 502
Implement the edge read. Serve a fresh copy without calling the origin. When the copy is stale, ask the origin. If the origin fails, serve the stale copy only while its age is under maxAge + opts.staleIfError; otherwise return 502. Serving a stale copy must not extend its life.
Any CDN (CloudFront, Fastly, Cloudflare, Akamai) in front of a web app. Build tools emit hashed filenames for this reason. stale-if-error and stale-while-revalidate are HTTP Cache-Control extensions (RFC 5861). Symptoms: one region on the old version after a deploy, or a full outage when the origin blips even though the CDN had everything cached.
“How do you make sure users get the new version after a deploy?” Say content-hashed asset URLs with long max-age and immutable, short-lived or revalidated HTML, deploy new assets before switching the HTML, and purge only the few mutable URLs. Mention the purge-vs-in-flight-fetch race, and stale-if-error for origin outages.