Stale responses and background updates
An expired copy is not necessarily unusable
Suppose a public museum guide has a 30-second freshness lifetime and the origin becomes unavailable. A cached guide can be better than an error if its age is acceptable to readers. A cached account balance or revoked permission is different. Never enable broad stale serving just to raise the hit rate. Decide by route, document the maximum tolerated age, and measure how old the data actually is.
proxy_cache_use_stale error timeout http_502 http_503 http_504;proxy_cache_valid 200 30s;This fragment allows reuse of an expired eligible entry under selected origin failures. It does not create an entry where none exists. The directive is not a maximum staleness timer; inactive is not such a timer either. For a strict age requirement use a verified bounded policy and monitor actual age. Do not apply this fragment to authentication, permissions, transactions or private account pages.
Refresh without making every reader wait
When public material can be slightly outdated, one request can initiate a refresh while other readers continue using the old document. An updating policy can also let a request receive the stale response while its own refresh runs separately. The next fresh response should reflect the origin update. A background refresh is request-triggered; it is not a periodic job that visits every key.
proxy_cache_use_stale updating;proxy_cache_background_update on;proxy_cache_revalidate on;Cache-Control: public, max-age=30, stale-while-revalidate=10, stale-if-error=60The header extensions express finite allowances beyond freshness; verify the selected cache supports and honors them. Explicit Nginx stale conditions take precedence over these header allowances, so do not combine the two and assume the header caps the directive. A stale-while-revalidate window does not guarantee an update completes inside it. Track failed refreshes, origin errors and document version as well as HIT and STALE counts.
location = /account { proxy_cache off; proxy_pass http://127.0.0.1:8091;}Practice
Classify a public guide, an inventory reservation and an account profile by acceptable age. Write an outage test with a warm key and a cold key; the cold one must not invent a successful response. Next: trace a second cache tier.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Stale responses and background updates
Choose freshness and availability policies for public content, without leaking private or safety-critical data.
Which item is a reasonable stale-on-error candidate?
Can stale-on-error produce a stored copy for a cold key?
What is required for background update in this configuration?
Does inactive impose a strict maximum stale age?
Which policy wins when explicit stale conditions overlap origin allowances?