recall

← recall

stale-while-revalidate pattern

Serve the cached value immediately, then refresh it in the background. Users always get a fast response; the cache stays fresh without any user paying the latency. HTTP standard; also the SWR / TanStack Query pattern in the frontend.

Serve the cached value immediately, then refresh it in the background. Users always get a fast response; the cache stays fresh without any user paying the latency. HTTP standard; also the SWR / TanStack Query pattern in the frontend.

symptoms

  • slow first-time loads after cache expiry
  • cache stampedes on TTL boundaries

causes

  • hard TTL eviction with sync revalidation

fixes

  • Cache-Control: stale-while-revalidate=N
  • SWR libraries on the client
  • single-flight on the revalidation

you might say

  • SWR it
  • stale-while-revalidate
  • serve stale, refresh async

related

aliases: SWR

topics: caching

references: