Zeeshan Adil
Book a call
← All concepts

CONCEPT

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.

Also called: SWR, serve stale. By Zeeshan Adil··Distributed Systems·Performance

Chart of the share of reads served over 130 simulated seconds for one popular post: with a plain 30-second TTL 0% are served after the expiry at 10 seconds, with a soft and a hard TTL 100% are served until the 100-second mark where the hard TTL is reached.

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.

Code
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, 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.

RUN IT YOURSELF

Challenge · Distributed Systems ChallengesA popular post takes down the database
What is a cache stampede?What is request coalescing?What is a metastable failure?

SOURCES

Get the next deep dive by email

New deep dives on how production systems really behave, from Kubernetes networking to PostgreSQL internals. About one email a month.

You will get one email to confirm. Unsubscribe from any email with one click. Your address is used for this newsletter and nothing else.

Written by Zeeshan Adil

I run systems like this for a living. If your team is fighting the same problem, or wants this as a talk, write to me.