e4407d9guide1 min read
A mental model for edge caching
Three questions to answer for every response: who may cache it, for how long, and what makes it different from the next one.
Most caching bugs come from leaving one of three decisions to a default. Make each one explicitly and the headers follow.
Who may cache it
private keeps a response in the browser only; public lets shared caches, like a CDN, store it. Anything personalized should be private or not cached at all.
For how long
Separate browser lifetime from shared-cache lifetime. s-maxage applies only to shared caches, so you can purge the edge while browsers keep a short copy.
Cache-Control: public, max-age=60, s-maxage=86400, stale-while-revalidate=300What varies
If a response depends on a header, say so with Vary. Keep the list short — every value multiplies the number of cached copies.