When things break Recommended
Serve the last good page when the server fails
Let caches keep serving the last good copy of a public page for up to a day when the app fails or times out.
How to do it
Add stale-if-error (RFC 5861) to the Cache-Control of public pages. In a front server or CDN you control, turn on its own setting, such as proxy_cache_use_stale in nginx. Never do this for signed-in pages, which stay out of shared caches. Keep an alert on the errors, because visitors no longer report them.
Example
proxy_cache_path /var/cache/nginx/pages keys_zone=pages:10m max_size=1g inactive=1d;
location / {
proxy_cache pages;
proxy_cache_valid 200 5m;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_background_update on;
proxy_pass http://127.0.0.1:8080;
}
// Express: public pages only
res.set('Cache-Control', 'public, max-age=300, stale-while-revalidate=86400, stale-if-error=86400');
# Flask: public pages only
response.headers["Cache-Control"] = "public, max-age=300, stale-while-revalidate=86400, stale-if-error=86400"
How to check
Load a page through the cache, then stop the app. The same page still answers 200 from the cache, and a page never cached answers the 500 page.
For agents: if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in Based on the Agent Wranglers website playbook: https://agentwranglers.se/playbook/website/when-things-break/serve-stale-on-error.