After the cache expires, the old content is provided first, and then the update is performed in the background. What does this cache instruction solve?
The 'stale-while-revalidate' in Cache-Control resolves the issue of 'users should not wait for cache origin validation': According to the MDN documentation, it is a response directive that allows the reuse of old response content within the window period after expiration, while also revalidating with the origin server in the background. The example syntax is max-age=604800, stale-while-revalidate=86400——freshness for one week, with a one-day window to provide old content first before updating.It is not the same as the request instruction max-stale: the latter expresses that the client is willing to accept how old the cache is, and mainstream browsers do not support requests carrying max-stale.
How to define the freshness period and expiration period
max-age defines the freshness period of the response, within this period the cache can be hit directly without disturbing the source server. Once the freshness period is exceeded, it enters the expiration period, the default behavior is to 'check first and then return': the cache takes the identifier back to the source for confirmation, if the content has not changed it is continued to be used, if it has changed a new one is taken - this time the entire latency of the source visit is counted in the user's waiting time.stale-while-revalidate opens a window during expiration: requests within the window immediately receive old content, the cache component asynchronously completes validation in the background, and the next request receives the new version.The cost is that users may see expired content within the time window, the length of the window is the trade-off parameter between 'fast' and 'new'.
Where does the background update hide the delay?
What is hidden is the round-trip time for source validation. Under the normal strategy, users have to wait for a complete validation for the first request entering the expiration period; with this instruction, users receive a local old copy, and the validation is performed in the background. It is particularly suitable for content update rhythms measured in hours, and access peaks concentrated on the recently expired cache - for example, when article list pages fail in the morning rush hour, there will not be a batch of 'slow source requests'. It is not suitable for pages that require immediate consistency (such as payment, inventory), where the old content window is the error message window.
How does the old content tolerance support on the request side work?
max-stale is a request directive, the opposite direction: the client declares 'I can accept content expired for N seconds'. MDN explicitly states that mainstream browsers do not support sending requests with max-stale, so its actual effective scenario is between programmatic clients, proxies, and cache middleware, rather than the browser address bar. Do not rely on the browser to include this declaration when designing cache strategies.
and the division of labor with no-cache and must-revalidate
| directive | Position | semantics | Typical usage |
|---|---|---|---|
| no-cache | response | Can be stored and reused, but must be verified before each reuse | Pages with frequent changes but need to save traffic |
| must-revalidate | response | Must be verified after expiration before use, do not continue to provide old content if the source site is unreachable | Interfaces with high consistency requirements |
| stale-while-revalidate | response | Use old content first within the expiration window, and verify in the background | List pages, static resources |
The attitude of 'must-revalidate' is 'prefer not to return rather than return expired', which is exactly the opposite of 'stale-while-revalidate's 'give the old first', and the two are used separately according to the page tolerance.
Page documents and static resources should be separated.
The freshness period of the page document should be set short, with a minute-level expiration window to balance experience and freshness; for fingerprinted static resources, set a longer freshness period and add a short window for extreme cases. Caching is just a layer of speed improvement: AnQiCMS runs on Go language, with a significant increase in page loading speed compared to traditional PHP-based CMS, with an official capacity of about 5 million PV per machine——low runtime overhead, and the source verification itself is also inexpensive, so the caching parameters can be set more conservatively, without having to extend the old content window to protect the source station.
Common questions
Does this command affect search engine crawling?The search engine crawler may still hit the content in the old window, and the update lag may be up to one window period. Keeping the window within minutes to hours has a minimal impact on the exposure of new content.
If the browser does not support max-stale, are the side response instructions still effective?Valid. Both stale-while-revalidate and must-revalidate are response instructions, implemented and followed by the intermediate CDN, reverse proxy, and browser cache, and do not depend on the browser carrying the declaration when making requests.
Why doesn't the updated content in the old content window take effect?This is a design behavior: the old copy is given in the window first, and the new version is seen in the next request after the background verification is completed. To achieve immediate consistency, the window is set to zero and the pre-verification strategy is used.