Documentation menu

Docs › Caching

Stale content

Serving slightly stale content is usually better than making a visitor wait, and far better than showing an error. Cache.bz supports both standard stale directives.

stale-while-revalidate

When an object's TTL expires, the edge normally has to fetch a fresh copy before answering. With stale-while-revalidate, it answers immediately with the expired copy and refreshes it in the background. Only one background request is sent per object per location, however many visitors arrive.

TOML
[[rules]]
match = "/api/products/*"
ttl   = "30s"
stale_while_revalidate = "5m"

In this example a product response is fresh for 30 seconds. For the five minutes after that, visitors get the cached copy instantly while the edge revalidates. After five and a half minutes without a successful refresh, the next request waits for the origin.

stale-if-error

stale-if-error keeps serving the last good copy when your origin is unreachable, times out, or returns a 500, 502, 503 or 504. It turns an origin outage into a period of slightly old content.

TOML
[[rules]]
match = "/*"
stale_if_error = "24h"

Using origin headers

Both directives can also come from your origin in Cache-Control, which is handy when the application knows best:

HTTP
Cache-Control: public, max-age=60, stale-while-revalidate=600, stale-if-error=86400

Values in cachebz.toml take precedence over origin headers when both are present.

Seeing it in responses

A stale response is marked in the cache-status header, so you can see exactly what happened:

HTTP
cache-status: cachebz; hit; ttl=-12; detail=stale-revalidating
cache-status: cachebz; hit; ttl=-340; detail=stale-origin-error
Recommended defaults

For most HTML pages, a short TTL (60s) with stale-while-revalidate of 10 minutes and stale-if-error of 24 hours gives near-instant updates, a high hit ratio and resilience to origin outages.

Was this page helpful? Tell us what was missing at support@cache.bz. Every message is read by the team that writes these docs.