# What is stale-while-revalidate?

> Stale-while-revalidate is a caching strategy that serves a cached response after its freshness time has passed, while a refresh runs in the background. Readers get an instant answer and the next reader gets a fresh one. It stops at a hard limit, after which the old copy is no longer served.

- Author: Zeeshan Adil (https://www.internalswithzee.com/about)
- Published: 2026-11-12
- Source: https://www.internalswithzee.com/concepts/stale-while-revalidate

Also called: SWR, serve stale.

## The problem it solves

A plain TTL has one cliff: until it expires the copy is fine, and the instant it expires the copy is gone. The next reader pays for the recompute, and if many readers arrive together they all pay at once. Most data is not that urgent. A comment list that is 40 seconds old is usually as good as one that is 1 second old.

## How it works

Keep two limits instead of one. The soft TTL says a refresh is due. The hard TTL says the copy is no longer acceptable.

```
age under soft TTL        -> serve it
age between soft and hard -> serve it now, refresh in the background
age over hard TTL         -> do not serve it; wait for a refresh or fail
```

RFC 5861 defines this for HTTP as the `stale-while-revalidate` and `stale-if-error` Cache-Control directives. The same idea works in an application cache: store when the data was read, and decide freshness from its age.

## What it does not guarantee

It buys time, it does not fix the cause. Each stale read still triggers a refresh, so on its own it can start thousands of refreshes behind the readers. Pair it with request coalescing so only one refresh per key runs. Two more traps:

- **Never re-stamp a copy.** If a copy moved between caches is stamped as just read, its age resets and old data is served for another full window.
- **Watch the age, not the hit rate.** During a database outage the hit rate stays at 100% until the hard TTL while the data gets older every second.

## Measured

These numbers come from my [cache stampede challenge](/challenges/distributed-systems/a-popular-post-takes-down-the-database), a simulation of 20,000 reads a second for one post, with a 30 s soft TTL and a 120 s hard TTL. With a plain 30 s TTL, 0.0% of reads were served after the expiry. With the soft and hard TTL, 100.0% were served from 10 to 100 seconds, and the oldest data served was 120 s.

It was not a fix on its own: every stale read started its own refresh, the database ran at 100% CPU, and no refresh finished. At 100 s the outage began anyway. In a second run with PostgreSQL down, a copy re-stamped as just read reached a real age of 200 s, against 120 s when timestamps were kept.

## Related

- Challenge to run: https://www.internalswithzee.com/challenges/distributed-systems/a-popular-post-takes-down-the-database
- Related concept: https://www.internalswithzee.com/concepts/cache-stampede
- Related concept: https://www.internalswithzee.com/concepts/request-coalescing
- Related concept: https://www.internalswithzee.com/concepts/metastable-failure

## Sources

- [RFC 5861: HTTP Cache-Control Extensions for Stale Content](https://www.rfc-editor.org/rfc/rfc5861)
- [AWS Builders' Library: Caching challenges and strategies](https://aws.amazon.com/builders-library/caching-challenges-and-strategies/)

