TTL — origin tells the edge how long to trust the copy
max-age applies to every cache; s-maxage overrides it for shared caches like the CDN; TTL is the countdown that flips a cached cell from fresh to stale.
The user is now reaching a nearby POP — but how long does that POP hold the cached copy before checking origin again? The origin tells it via Cache-Control, and the dial that lets the CDN hold for a day while the browser holds for a minute is s-maxage.
Scene 04
TTL — origin tells the edge how long to trust the copy
- Watch
- Try it
- Predict
- Capture
A response just arrived at the POP with Cache-Control: public, max-age=60, s-maxage=3600. The EDGE cell shows 1:00:00 ticking, the BROWSER cell shows 1:00 ticking — both green. Watch the clock advance: the browser will turn gray well before the edge does.
Highlighted lines are the ones running in the diagram right now.
def parseCacheControl(header, role):d = parseDirectives(header) # k=v pairsif 'no-store' in d:return Lifetime(ttl=0, store=False)max_age = int(d.get('max-age', 0))s_maxage = d.get('s-maxage') # may be absentif role == 'shared' and s_maxage is not None:return Lifetime(ttl=int(s_maxage))return Lifetime(ttl=max_age)
def isFresh(cell, now):if cell.lifetime.ttl == 0:return False # born stale (e.g. max-age=0)age = now - cell.created_atreturn age < cell.lifetime.ttl
def serve(request, role):cell = store.lookup(request.url)if cell is None:return forwardToOrigin(request) # MISSif isFresh(cell, now()):return cell.response # HIT# stale: must revalidate before servingreturn revalidate(cell, request)
Where this sits in Build a CDN
Scene 04 of 13, in the Freshness act — TTL, revalidation, stale-while-revalidate.. max-age applies to every cache, including the browser; s-maxage overrides it for shared caches like the CDN — and TTL is what flips a cell from fresh to stale.
Up next. When the edge's copy goes stale it does not re-download the whole body — it asks origin a cheap question first.