Course contentsShow
← Cloud and Infrastructure
Lesson 2679 of 2,98958. Caching StrategiesFree lesson

Stale Content Policies

Use stale-if-error and stale-while-revalidate to serve cached content during origin failures or updates.

Stale Content Policies

What You'll Learn

You'll learn how to use stale-if-error and stale-while-revalidate directives to serve cached content during origin failures or while refreshing data.

The Core Idea

Sometimes serving slightly outdated content is better than serving nothing at all. Stale content policies let CDNs and caches continue delivering cached responses even after they've technically expired—either when your origin server fails or while the cache fetches a fresh copy in the background.

Think of it like a newspaper stand: if today's paper hasn't arrived yet, you'd rather sell yesterday's paper than close the stand. Customers get something useful while you wait for the fresh delivery.

Two Key Directives

stale-while-revalidate

Allows serving stale content while asynchronously fetching a fresh version:

Cache-Control: max-age=3600, stale-while-revalidate=86400

After 1 hour, the cache serves the stale version immediately to users, then fetches fresh content in the background. The next request gets the updated version.

stale-if-error

Permits serving stale content when the origin returns an error:

Cache-Control: max-age=3600, stale-if-error=86400

If your origin is down or returns a 500 error within the 24-hour window, the cache serves the last-known-good version instead of propagating the error.

Combined Strategy

Cache-Control: max-age=600, stale-while-revalidate=3600, stale-if-error=86400

This provides resilience: fresh content within 10 minutes, background updates for an hour, and error fallback for a day.

Key Takeaway: Stale content policies use stale-while-revalidate and stale-if-error to improve availability by serving cached content during origin updates or failures, gracefully degrading service rather than showing errors.