CDN cache invalidation
medium 30 minHow to make a CDN stop serving an outdated copy: let it expire, purge it, mark it stale, give the new version a new name, or purge a whole group by tag.
See it run
The origin has a new version. Watch when each copy learns about it, and what the origin has to carry.
Operations
TTL expiry
A page with max-age=3600. Nobody sends a message.
Step 1 of 6
Cached
The copy of version 1 may be used for one hour.
Step 1: Cached
copy = cache[path]if now() < copy.expires: return copyanswer = origin(path, if_none_match=copy.etag)if answer.status == 304: copy.expires = now() + 3600else: cache[path] = Copy(answer, now() + 3600)- Delay
- Up to 1 hour
- Origin load
- Spread
- Needs
- a header
Nothing to operate, and it heals every other mistake.
In the real world
The same cdn cache invalidation doing real jobs. Pick one and step through it.
Purge and commit in the wrong order.
Step 1 of 4
Purge first
The edge is empty. The database still says 10.
Step 1: Purge first
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Purge before commit
criticalA reader in between refills the cache with the earlier value.
Wrongpurge(tag)db.commit()Rightdb.commit()purge(tag)Purge everything in each deploy
criticalEvery release becomes a load test for the origin.
Wrongpurge_everything()Rightpurge(paths=["/index.html"], soft=True) # files have hashed namesPurge by hand
majorSomebody forgets it, on a Friday.
Wrong# wiki: remember to click PurgeRightoutbox.add("purge", tag="product-123") # in the same transactionDeleting earlier files at release
majorOpen pages still point to them and break.
Wrongbucket.delete("app.v1.js")Rightbucket.expire("app.v1.js", after_days=30)
Interview tips
How to explain it
- Posters at bus shelters.
- Wait, tear, sticker, new board, red dots.
- The office is the origin.
Memory trick: STALE
- Soft purge, TTL.
- Always version, Labels.
- Emergency: hard purge.
Order
- Commit.
- Inner cache, shield, edge.
- Retry until it succeeds.
Check
- What do browsers keep?
- How many refills?
- What if the purge is lost?