CDN & CACHE INVALIDATION

You purged the origin, and the edge is still serving yesterday

L215 MINCACHING
PREDICT

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.

▶ SIMULATION
EDGE E1
cache empty
last load: —
EDGE E2
cache empty
last load: —
EDGE E3
cache empty
last load: —
ORIGIN
serving: v1
status: UP
requests: 0
clockt = 0 s · /app.js max-age=3600
in flight—
stale loads / errors / loads0 / 0 / 0
last event—
✓ every page load that starts after the deploy runs the new version✓ no page load gets an error
SCHEDULE
01[OPS]config: /app.js, max-age=3600, nobody purges
02[E1]E1: page load @ 0 s
03[E3]E3: page load @ 0 s
04[E2]E2: page load @ 590 s — miss, fetch sent to origin (slow)
05[ORIGIN]deploy v2 @ 600 s
06[E2]E2: v1 response arrives @ 605 s, stored
07[E1]E1: page load @ 700 s
08[E2]E2: page load @ 700 s
09[E3]E3: page load @ 700 s
10[ORIGIN]origin goes down @ 800 s
11[E1]E1: page load @ 900 s
12[E2]E2: page load @ 900 s
13[E3]E3: page load @ 900 s
14[OPS]report
Start of no purge scenario. 14 steps.
# 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

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.

BREAK IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

never change the bytes behind a cached URL; change the URL. Then the only thing that must be fresh is the small document that names the URLs, and old copies are harmless instead of wrong. A purge is a delete and races like one. Keep the last good copy around (stale-if-error) so the origin can fail without users noticing.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: CAPSTONE: CACHE A FEED WITH READ-YOUR-WRITES →