Nginx fastcgi cache HIT vs STALE

cachenginx

I have strange problem with nginx microcache. When nginx serve STALE content, it takes too long.
My actual microacha part in config:

...
fastcgi_cache biznisto.sk;
fastcgi_cache_bypass $skip_cache;
fastcgi_cache_key "$scheme$request_method$host$request_uri$rt_session";
fastcgi_cache_valid 200 301 302 5m;
fastcgi_cache_use_stale updating error timeout invalid_header http_500;
fastcgi_cache_lock on;
fastcgi_cache_revalidate on;
fastcgi_cache_background_update on;
fastcgi_pass_header Set-Cookie;
fastcgi_pass_header Cookie;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
add_header X-Cache $upstream_cache_status;
...

Screens
STALE – wait 565ms
enter image description here
HIT – wait 59ms
enter image description here

Any suggestions?
Thanks

Best Answer

The fastcgi_cache_background_update directive allows updating an expired cache item while the stale cached response is being returned to the client.

If, however, the response is fully returned, but updating is not yet finished, it will delay subsequent actions, including processing of additional requests on the same connection, and/or the closing of the connection.

This behaviour ensures that:

  • a client cannot impose uncontrolled load on the server, assuming various limitations like limit_conn are in place
  • the overall operation is usually better and, in the worst case, is not worse than without background update.

https://trac.nginx.org/nginx/ticket/1329