Search code examples
cachingnginx

Nginx cache lock always causes 500ms delay


Nginx cache locked requests(http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_lock) always takes 500ms to respond

$ab -n 2 -c 2 http://192.168.12.103/test1234
access.log:
192.168.12.103 - - [12/Sep/2017:02:34:59 -0700] "GET /test1234 HTTP/1.0" 200 12 "-" "ApacheBench/2.3" "-" 127.0.0.1:9095 0.002 0.002 MISS
192.168.12.103 - - [12/Sep/2017:02:34:59 -0700] "GET /test1234 HTTP/1.0" 200 12 "-" "ApacheBench/2.3" "-" - - 0.502 HIT

I know that it buffers to a temp file and copies it to the cache. But 500ms looks large. Anybody knows why?
Any help would be appreciated.

Setup info:

  1. Test upstream server
  2. cache is located in tmpfs (mount -t tmpfs -o size=512m tmpfs /tmpfs)
  3. nginx version: 1.7.2.1

nginx config

worker_processes  1;
error_log  logs/error.log;
daemon off;
pid        /var/run/nginx.pid;
events {}
http {
    proxy_cache_path /tmpfs/local_cache keys_zone=local_cache:250m levels=1:2 inactive=8s max_size=1G;
    proxy_temp_path /tmpfs;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" '
                      '$upstream_addr $upstream_response_time $request_time $upstream_cache_status';

    access_log  logs/access.log  main;
    server {
        listen       80;
        server_name  localhost;

        location / {
                proxy_pass http://127.0.0.1:9095;
                proxy_cache local_cache;
                proxy_cache_valid 200 2s;
                proxy_cache_lock on;
        }

    }
}

Solution

  • This seems to be the default behaviour. The cache locked requests are locked by worst case 500ms or the time_out value.(source)

    If the upstream response times are stable, for cache_lock_timeout any value little above the response time, it will avoid the behaviour. In this particular case we can set about 5ms and the first request will return in 0-1 ms and the locked requests will return within 5-6ms.