Field Notes

Microcaching a slow backend with nginx

A one second cache sounds pointless. Under load it turns a thousand identical requests per second into one.

proxy_cache_path /var/cache/nginx keys_zone=micro:10m max_size=200m;
location / {
  proxy_cache micro;
  proxy_cache_valid 200 1s;
  proxy_cache_lock on;
  proxy_cache_use_stale updating error timeout;
}

The two lines that matter

proxy_cache_lock lets a single request populate a missing entry while the others wait. use_stale updating serves the old copy during refresh, so nobody waits at all.

Do not cache

Anything with a session cookie. Bypass on the cookie and on non-GET methods, and add the cache status to a response header while testing.