0
0
Nginxdevops~5 mins

Burst and nodelay options in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes many users try to access a website at the same time, causing slow responses or errors. The burst and nodelay options in nginx help control how many requests can wait and how fast they are processed to keep the site running smoothly.
When you want to limit how many users can send requests at once to avoid overloading your server.
When you want to allow a few extra requests to wait briefly during sudden traffic spikes.
When you want to decide if waiting requests should be delayed evenly or processed immediately.
When you want to protect your backend from too many requests arriving too quickly.
When you want to improve user experience by controlling request flow during busy times.
Config File - nginx.conf
nginx.conf
http {
    limit_req_zone $binary_remote_addr zone=mylimit:10m rate=5r/s;

    server {
        listen 80;

        location / {
            limit_req zone=mylimit burst=10 nodelay;
            proxy_pass http://localhost:8080;
        }
    }
}

limit_req_zone defines a shared memory zone named 'mylimit' to track request rates per user IP, allowing 5 requests per second.

limit_req inside the location applies the rate limit using the 'mylimit' zone, allows a burst of 10 extra requests to queue, and uses nodelay to process queued requests immediately without delay.

This setup helps control traffic spikes by letting some extra requests pass quickly without slowing down users.

Commands
Check the nginx configuration file for syntax errors before applying changes.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload nginx to apply the new configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Send a request to the nginx server to verify it responds correctly under the new rate limiting rules.
Terminal
curl -s -w "%{http_code}\n" http://localhost/
Expected OutputExpected
200
Key Concept

If you remember nothing else from this pattern, remember: burst allows extra requests to queue temporarily, and nodelay makes those queued requests process immediately without waiting.

Common Mistakes
Setting burst too low or too high without testing traffic patterns.
Too low burst blocks users too quickly; too high burst defeats rate limiting purpose.
Choose a burst value that matches expected traffic spikes and test under load.
Using nodelay without understanding it causes queued requests to process immediately.
This can cause sudden bursts of traffic hitting backend all at once, possibly overloading it.
Use nodelay only if your backend can handle sudden bursts; otherwise omit it to delay queued requests.
Not testing nginx config syntax before reload.
Syntax errors cause nginx to fail reloading, leading to downtime.
Always run 'nginx -t' before reloading to ensure config is valid.
Summary
Define a limit_req_zone to track request rates per user IP.
Use limit_req with burst to allow extra queued requests during spikes.
Add nodelay to process queued requests immediately if desired.
Test nginx config syntax with 'nginx -t' before reloading.
Reload nginx to apply changes without downtime.