Caching
SWR: multiple-choice review
Six questions that cut across the whole unit. Each mirrors a header you write or an incident you triage — not a directive to memorise, but a freshness-versus-latency trade to weigh under real traffic.
Confirm you can connect the freshness window, the SWR window, stale-if-error, single-flight, and the client revalidation model — and that you know the one place SWR must never go.
A request arrives 90 seconds after caching, with Cache-Control: max-age=60, stale-while-revalidate=300. What does the user get and why?
Why does max-age alone produce a per-TTL p99 sawtooth, and how does stale-while-revalidate flatten it?
A popular key expires under 10k req/s and the edge fires one background revalidation per stale-hit. What breaks and what is the fix?
What does stale-if-error add on top of stale-while-revalidate, and why is pairing them the senior default?
Two components call useSWR('/api/user') in the same render tick. What does the SWR client library do, and what governs it?
For which response is stale-while-revalidate the wrong tool, and why?
The through-line is one contract: return what you have now, fetch what is correct next, never block the human on the refresh. max-age sets freshness, the SWR window kills the synchronous-revalidation sawtooth, stale-if-error turns an outage into degraded-but-up, single-flight plus jitter stops the background-refresh herd, and the client libraries reproduce the same model with dedupingInterval and focus/reconnect revalidation. The one hard boundary: never apply SWR to auth, permissions, or anything where stale data is a security bug.