Server & Hosting Performance

Set stale-while-revalidate windows without hiding outdated content

A faster cached response is useful only when its age and audience are still acceptable.

A document card beside a clock and a circular refresh arrow connected to a server outline.

Use stale-while-revalidate when a response can remain useful for a short, agreed period after its normal freshness lifetime expires. Choose that allowance from the consequences of showing older content, then test the fresh, stale and expired states separately. Adding the directive to every response is not a substitute for deciding which information may be reused.

Start with a content-age budget

Begin with one public resource whose owner can explain how quickly a correction must become visible. A directory description may tolerate a brief delay, while a checkout decision or account balance needs a different design. These examples describe decisions to make, not universal cache settings for those page types.

Ask what happens if somebody acts on the previous version. Record the maximum acceptable age, the people responsible for changing the content and the route used to invalidate an urgent correction. If nobody can answer those questions, keep the current conservative policy while you investigate rather than introducing a new stale-serving allowance.

Distinguish content age from time since a visitor opened a tab. A cached response already has a history when the browser receives it. A page that looks newly loaded can contain an older representation, so the useful question is when that representation was generated or successfully validated, not when a particular visitor arrived.

Start with the browser caching guide if max-age, validators and cache layers are unfamiliar. This workflow concentrates on the extra interval after freshness expires. It does not replace the underlying storage rules or turn a response that should remain private into public material.

Separate freshness from permission to serve stale content

max-age establishes a freshness lifetime. stale-while-revalidate adds a limited period during which a supporting cache may reuse an already stale response while checking it in the background. The second value is additional time, not a replacement for the first and not a promise that all requests will avoid waiting.

Consider a deliberately short test policy with max-age=60, stale-while-revalidate=30. In the simplified example below, the response starts at age zero and has no competing directives or intervening validation. The numbers illustrate the decision boundaries; they are not recommended production values.

Response age in the example State Expected test question
20 seconds Fresh Is the stored representation reused under the applicable policy?
75 seconds Stale, inside the extra allowance Can the cache serve the old representation while it revalidates?
100 seconds without successful revalidation Outside that allowance Does normal validation or retrieval happen instead of relying on this extension?

The combined possible age in this example is 90 seconds before this particular stale allowance ends. A successful validation changes the later calculation, so follow the actual response headers instead of treating the table as a permanent wall-clock schedule. Other configured cache policies may also affect the outcome and must be inspected independently.

The HTTP stale-content extension definition explains the request-driven background validation model. A quiet resource may receive no request during its stale window, leaving the next visitor to wait for normal validation. This is why a periodic refresh job and stale-while-revalidate are not interchangeable arrangements.

Keep privacy and cache identity ahead of speed

Write down the complete path from the application through any reverse proxy or CDN to the browser. Identify which components are shared across visitors and which belong to one user. A browser-only test cannot establish that an intermediary stores and serves the same response correctly.

Inspect the response for account information, location-dependent content, negotiated language and other variations. Shared storage is particularly sensitive because a mistake can deliver one visitor's representation to another. Do not add public merely to make a cache hit appear in a diagnostic panel; first establish that the response is suitable for that audience.

private restricts storage by shared caches, whereas no-store prohibits storage under the HTTP caching rules. no-cache allows storage but requires validation before reuse. These directives solve different problems, and adding an attractive stale interval does not erase their requirements or justify a contradictory combination.

Review the cache key and any Vary behavior alongside the lifetime. A response served quickly under the wrong language, encoding or account state is still wrong. Use separate test accounts where appropriate and avoid putting private values into screenshots, shared logs or the evidence attached to a performance ticket.

Observe revalidation through the real delivery path

Create a small staging resource with an obvious version marker and a controllable update process. Capture the response body, Cache-Control, Age, validators such as ETag, and any documented cache-status diagnostics your infrastructure exposes. Mark the cache layer each observation describes instead of treating every hit as an origin bypass.

Fetch the resource normally to establish a baseline, then repeat the request while it is fresh. Wait until the chosen stale interval and request it again after changing the origin version. Check both the immediately returned representation and a subsequent request, because a background update does not rewrite a response that has already reached the visitor.

Also repeat the test without changing the representation. Conditional validation can confirm that the stored copy remains usable, often with a 304 Not Modified response between the cache and origin. A visitor may still receive the representation from cache; do not expect every internal validation exchange to appear as the browser's final response status.

Keep ordinary navigation, forced reload and a test with caching disabled as separate scenarios. They can send different request directives or bypass the behavior being investigated. Save the exact method and environment with each result so a colleague can reproduce it rather than relying on a screenshot of one fast request.

Test silence, updates and failures separately

Leave the staging resource untouched until both the fresh lifetime and the additional stale interval have elapsed. Request it only then. This low-traffic case is essential because a busy demonstration can repeatedly trigger successful background checks and conceal the wait that an infrequently visited page will experience.

Next, make an urgent content change and exercise the documented invalidation procedure. Check the affected URLs and variants at the relevant layers. A CDN purge does not, by itself, demonstrate that an already stored browser response disappeared, and an application deployment does not prove that every intermediary discarded its old object.

Test an origin failure separately from a normal content update. stale-if-error is a different extension with its own allowance; stale-while-revalidate is not an unlimited promise to serve old content through an outage. Record the actual behavior after the allowed interval, including errors, rather than assuming that an old successful response must remain available.

Check what happens when the cache does not support the extension. An unknown directive can be ignored, so the remaining policy still needs to make sense. Treat compatibility and intermediary configuration as part of the rollout evidence, not as something established by the presence of the header alone.

Maintain a simple evidence sheet for each scenario: the resource, requested variant, time, observed age, returned version and whether the origin was contacted. Include a failed expectation as clearly as a successful one. This keeps a later configuration change from being justified by a test that exercised a different cache or a different representation.

Roll out one policy with an owner and a rollback

Choose a small, clearly defined group of public resources for the first release. Document the fresh lifetime, extra stale interval, variation rules, refresh evidence and correction procedure together. Keep account pages and transactional decisions outside that experiment unless their requirements have been assessed separately and explicitly.

Compare response timing and origin request behavior under matching conditions, but also track whether visitors receive acceptable versions. A higher cache-hit ratio is not a sufficient outcome when important changes become harder to see. Record observed results without inventing a percentage improvement or projecting a lab result onto every visitor.

Define who can shorten the allowance, disable it and invalidate affected objects if a correctness problem appears. Revisit the policy when the page gains personalized content or its update frequency changes. The useful result is a bounded, tested exchange between responsiveness and freshness, with a recovery path that the team can actually execute.

Related guides