HTTP semantics, caching and delivery · core
HTTP caching in depth: freshness, validation and Vary
A cached response is reused without asking the server while it is fresh (max-age, s-maxage), then revalidated with ETag/If-None-Match or Last-Modified/If-Modified-Since so the server can answer 304, and directives like no-cache, no-store, private, immutable and stale-while-revalidate control who may store it and how.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Freshness: max-age=N (seconds since the response was generated; the Age header counts time spent in caches). s-maxage overrides max-age for shared caches (CDNs).
- Validation: when stale, the browser sends If-None-Match: <ETag> or If-Modified-Since; 304 Not Modified has no body and refreshes the stored copy.
- no-cache = store but revalidate before every use. no-store = don't store at all. private = only the browser may store it. public = shared caches may store it even with Authorization.
- immutable = never revalidate while fresh, even on reload (for hashed files). stale-while-revalidate=N = serve stale for N seconds while revalidating in the background. stale-if-error=N = serve stale if the origin returns 5xx.
- No Cache-Control header -> heuristic caching (browsers guess, often 10% of the time since Last-Modified). Always set it.
- Vary: lists request headers that change the response (Accept-Encoding, Accept-Language, Origin for CORS). Vary: Cookie or User-Agent destroys CDN hit rates.
- Layers: memory/disk HTTP cache, service worker Cache API (separate, your code decides), CDN, reverse proxy. bfcache restores pages without any of these.
The red pen
Common traps: mistake → fix
Mistake: "no-cache means don't cache"
Fix: it means revalidate before reuse; no-store means don't store.
Mistake: "private, no-cache, no-store, max-age=0, must-revalidate is safest"
Fix: conflicting directives; no-store alone says it.
Mistake: "A 304 still downloads the file"
Fix: a 304 has no body; the cached copy is reused.
Mistake: "max-age counts from when the browser received it"
Fix: it counts from when the origin generated it; Age is subtracted.
Easy to hard
Questions interviewers ask, easy to hard
- Explain Cache-Control, ETag and Last-Modified.
- How would you cache an SPA's index.html vs its hashed JS bundles?
- no-cache vs no-store vs max-age=0, must-revalidate?
- What does stale-while-revalidate do for users and for the origin?
- Two users saw each other's dashboard data through the CDN. Which headers would you check?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.