Meaning
Stale‑while‑revalidate (SWR) is a caching strategy where a cached response is served immediately even if it is stale, while a background request fetches fresh data to update the cache. It reduces latency for the user by avoiding waiting for network fetches, and it keeps data eventually consistent. The pattern is used when the application can tolerate slightly out‑of‑date information but wants to keep the cache up to date without blocking the request.
Primary Function
Caching strategy
Communicative Purpose
Ensures low‑latency responses while keeping cached data eventually fresh.
Pattern
read from cache → return cached value (even if stale) → trigger async refresh → update cache
Função primária
Caching strategy
Propósito comunicativo
Ensures low‑latency responses while keeping cached data eventually fresh.
Situações de gatilho
Web API client: frequent read‑only endpoint with occasional updates Mobile app: displaying user profile where slight staleness is acceptable Edge server: serving static assets that change infrequently
Contextos
Web applications, CDN edge servers, client‑side data‑fetching libraries (e.g., React SWR, Vue useSWR), API gateways
Padrão
read from cache → return cached value (even if stale) → trigger async refresh → update cache
Colocados típicos
- cache miss
- background fetch
- revalidation
- TTL
- freshness
- optimistic UI
Substituições comuns
- Using a simple time‑based expiration (no background refresh) – reduces complexity but increases latency Using a write‑through cache – ensures freshness but adds write overhead
Erros comuns
Assuming stale data is always acceptable, leading to UI showing outdated information → user confusion Not handling errors in the background refresh, causing the cache to remain stale indefinitely → data becomes increasingly out‑of‑date Updating the cache without proper synchronization in concurrent environments, causing race conditions → corrupted cache entries Setting the revalidation interval too short, causing excessive load on origin servers → performance degradation
Similar / contraste
Cache‑aside (pull) – only fetches on miss, no background refresh Write‑through – updates cache on every write, ensures freshness but higher write latency Stale‑if‑error – serves stale data only on fetch failure, not proactive
Interferências
Coming from Python: using `functools.lru_cache` without a background refresh → does not implement stale‑while‑revalidate semantics Coming from JavaScript: assuming `fetch` returns cached data automatically – browsers need Service Workers to implement SWR
Família do chunk
- cache‑control directives
- read‑through cache
- write‑through cache
- lazy loading
- optimistic UI
Nuance
Do not use SWR for data that must be strictly consistent, such as financial transactions Background revalidation adds extra network requests; on high‑traffic endpoints this can increase load If the cache store does not support atomic updates, race conditions may cause stale data to be overwritten incorrectly
Efeito pragmático
Provides fast perceived performance for end‑users while keeping data reasonably fresh, reducing server load compared to always‑fresh fetches.
Dica de memória
Think of a coffee shop that serves yesterday’s brew to a waiting customer while the barista prepares a fresh pot in the background.
Nota
SWR is popularized by the HTTP `Cache-Control` directive `stale-while-revalidate` and by libraries such as Vercel's SWR for React.
Upgrade path
Implement stale‑while‑revalidate with stale‑if‑error and cache‑stale‑while‑revalidate headers for full HTTP cache control.
Log in to save chunks.