HTTP caching from the browser to the CDN to React Query
Follow one request outward through every cache it can hit: the browser's HTTP cache (Cache-Control, ETag, Last-Modified, revalidation), the CDN edge, the client query cache (React Query and SWR), and the server's own cache. Then tackle the hard part, invalidation. You'll meet strong and weak validators, stale-while-revalidate, cache stampedes, request coalescing, negative caching, and cache keys. Each explanation links to the RFC or docs behind the answer.
Questions
- Not answered. Who coined the "two hard things in Computer Science" quip?
- Not answered. What does
Cache-Control: no-cacheactually permit? - Not answered. When does
no-storebelong on a response? - Not answered. What does
Cache-Control: privatetell caches? - Not answered. Why is the CDN serving one user's data to everyone, and what's the fix?
- Not answered. How long does each cache treat this response as fresh?
- Not answered. Which HTTP status code lets the cache reuse its copy without receiving the body again?
- Not answered. Which rule does a correct
304 Not Modifiedresponse follow? - Not answered. When a cache revalidates a stored GET response, which headers carry which validator?
- Not answered. What does the
W/prefix on an ETag mean? - Not answered. Why does a range request need a strong ETag, not a weak one?
- Not answered. Why can
Last-Modifiedalone (with noETag) serve an outdated body even though revalidation "succeeded"? - Not answered. Which directive lets a cache return a stale response immediately while it revalidates in the background?
- Not answered. What does the user get 700 seconds after the response was stored?
- Not answered. Which directive lets a cache keep serving a stale copy when revalidation fails (the origin errors or can't be reached)?
- Not answered. What does adding
must-revalidatetomax-age=60change in practice? - Not answered. What does
immutableadd beyond a year-longmax-age? - Not answered. For how many hours will a cache treat this response as fresh?
- Not answered. What does
Vary: Accept-Encodingtell caches to do? - Not answered. What will probably happen if you add
Vary: User-Agentto shared, cacheable HTML? - Not answered. What does
Age: 550tell you? - Not answered. Which responses should a shared CDN cache refuse to store by default?
- Not answered. What is the "stampede" that follows a hot key's expiry?
- Not answered. Which techniques actually prevent a cache stampede on a hot key?
- Not answered. What is this
404-caching technique called, and what is its main risk? - Not answered. With request coalescing on, how many requests reach the origin?
- Not answered. What's the difference between
staleTimeandgcTimein React Query? - Not answered. Why does React Query refetch data it just rendered?
- Not answered. What happens when two components call
useSWR('/api/user')within about 2 seconds? - Not answered. Why does fingerprinting make cache invalidation almost a non-problem?