Search code examples
nginx

nginx: what is nodelay in http request limit module?


I am learning nginx httprequestlimitmodule. i am not getting the concept of nodelay in httprequestmodule. i have tried below two configuration with nodelay and without nodelay. in both cases with nodelay and without nodelay i am hitting 10 request in 1 seconds and getting 503 temporary service unavailable error for 6 requests and 4 requests are successful. my question is if the result is same with nodelay and without nodelay then what is the use of nodelay option here.

limit_req_zone $binary_remote_addr zone=one:10m rate=2r/s;
limit_req zone=one burst=2 nodelay;

limit_req_zone $binary_remote_addr zone=one:10m rate=2r/s;
limit_req zone=one burst=2 ;

Solution

  • Let's take this config:

    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
    
    server {
        listen 127.0.0.1:81;
    
        location / {
            limit_req zone=one burst=5;
            echo 'OK';
        }
    
        location /nodelay {
            limit_req zone=one burst=5 nodelay;
            echo 'OK';
        }
    }
    

    and test it with nodelay

    $ siege -q -b -r 1 -c 10 http://127.0.0.1:81/nodelay
          done.
    
    Transactions:                  6 hits
    Availability:              60.00 %
    Elapsed time:               0.01 secs
    Data transferred:           0.00 MB
    Response time:              0.00 secs
    Transaction rate:         600.00 trans/sec
    Throughput:             0.09 MB/sec
    Concurrency:                0.00
    Successful transactions:           6
    Failed transactions:               4
    Longest transaction:            0.00
    Shortest transaction:           0.00
    

    And without nodelay:

    $ siege -q -b -r 1 -c 10 http://127.0.0.1:81/
          done.
    
    Transactions:                  6 hits
    Availability:              60.00 %
    Elapsed time:               5.00 secs
    Data transferred:           0.00 MB
    Response time:              2.50 secs
    Transaction rate:           1.20 trans/sec
    Throughput:             0.00 MB/sec
    Concurrency:                3.00
    Successful transactions:           6
    Failed transactions:               4
    Longest transaction:            5.00
    Shortest transaction:           0.00
    

    They both passed 6 request, but with nodelay Nginx process all the burst requests instantly and without this option Nginx makes excessive requests to wait so that overall rate would be no more than 1 request per second and last successful request took 5 seconds to complete.

    EDIT: rate=6r/s actually means one request in 1/6th of a second. So if you send 6 request simultaneously you'll get 5 of them with 503.

    There is a good answer with “bucket” explanation: https://serverfault.com/a/247302/211028