Invalidation and the CDN
Keep cached data from going stale, and push static content to an edge near the user.
Open this lesson in the learning hubKey points
- TTL is the invalidation you never have to remember. Choose the staleness you can live with and let entries expire.
- Eviction on write (
@CacheEvict, RedisDEL) is precise but fragile: every write path must remember to evict, forever. - Versioned keys dodge the problem. Read
product:42:v7, bump tov8on write, and let the old entry expire alone. - Stampede: a hot key expires and a thousand threads hit the database together. Fix it with per-key loading or jittered TTLs.
- A CDN is a cache you rent near the user: static assets, images, and cacheable
GETresponses served from an edge node instead of your origin. - Fingerprint asset names and cache them forever; keep HTML on
no-cacheso a deploy is visible immediately.
Example
# Fingerprinted asset: the name changes when the bytes change, so cache it forever.
curl -sI https://cdn.example.com/static/app.7f3a91c2.js | grep -Ei 'cache-control|age|x-cache'
# cache-control: public, max-age=31536000, immutable
# age: 82311 <- seconds this copy has sat at the edge
# x-cache: HIT <- never reached the origin
# HTML must never be stale: store it, but revalidate with the ETag every time.
curl -sI https://example.com/ | grep -i cache-control
# cache-control: no-cache
# Purge only as an escape hatch - it is slow and provider-specific.
curl -X POST https://api.cdn.example.com/v1/purge \
-H "Authorization: Bearer $CDN_TOKEN" \
-d '{"paths":["/index.html"]}'
Immutable names for assets, short TTLs for data, purge only when something is on fire.
This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the System Design course, and every lesson in it is listed on the System Design contents page.