cache2-stale-while-revalidate interaction with worker process restarts
0 reputation · 02 Oct 2023, 09:23 UTC
0 reputation · 02 Oct 2023, 09:23 UTC
uwsgi cache2 provides per-process key-value storage with a documented 'expires' parameter for time-bound entries; absent an explicit setting, items persist until the hosting worker process restarts. Version 2.0.10 added the 'cache2-stale-while-revalidate' flag to control stale entry serving during background revalidation, but how this flag interacts with worker process restarts across a deployment remains unspecified.
Because each uwsgi worker process maintains its own independent cache2 instance, stale entry behavior is not shared or synchronized between workers unless an external store such as Redis is deployed. The undocumented interaction between cache2-stale-while-revalidate and per-process restart cycles raises questions about whether stale serving resets, persists, or becomes inconsistent depending on which worker handles a given request.
When a uWSGI worker restarts, the cache2-stale-while-revalidate window resets to zero because the entire cache2 instance is destroyed and recreated empty. This causes inconsistent stale-serving behavior across workers: a non-restarted worker may still serve a stale entry during background revalidation, while a restarted worker sees a complete cache miss and triggers a fresh fetch.
The stale-while-revalidate window is calculated from the entry's insertion timestamp. On restart, the entry itself vanishes, so the window cannot be preserved. Requests routed to different workers post-restart will see different cache states: one worker might serve stale content while another fetches fresh data, creating user-visible inconsistency.
cache2-stale-while-revalidate=0 to ensure consistent miss/fetch behavior across workers.expires value (shorter than typical worker restart intervals) so entries expire before restarts occur, ensuring all workers see the same state.uWSGI 1.9.x lacks the stale-while-revalidate flag entirely. Entries are either fresh (within expires) or expired (triggering immediate fetch). The 2.0.10+ flag introduces the ability to serve stale content during async revalidation, making restart-related inconsistency possible where it wasn't before.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.