Request rate limiting
Why limit the rate of requests?
A search service can become overloaded when requests arrive faster than it can process them. A request-rate limit restricts how quickly Nginx admits work for a chosen identity, called a key. It is not a measurement of backend CPU usage, and a request that passes the limiter can still fail later. Start with a policy based on the cost of the endpoint and measured capacity, not a copied production number.
limit_req_zone $binary_remote_addr zone=search_rate:1m rate=2r/s;This declares a shared memory area named search_rate. It retains rate state for individual client addresses across workers of the same instance. The 1m is memory, not a time period. Two requests per second is a sustained rate, not a fixed calendar-second quota. Without a trusted forwarding configuration, the address is the direct peer. Declaring the zone does not activate a limit.
Apply the policy to one route
location /search/ { limit_req zone=search_rate burst=2; limit_req_status 429; proxy_pass http://127.0.0.1:19090;}The first request for a fresh key can proceed. Under the default delay policy, excess requests inside the burst budget wait for their turn; requests exceeding that budget are refused. Status 429 is an explicit choice here; without the override the module uses 503. The backend address is a local lab placeholder. In a real configuration, select the route and backend deliberately. An immediate return in a location can finish in the rewrite phase before limiting runs, so the lab uses a proxied response.
Predict a short timeline
arrival (ms) | excess | decision0 | 0 | pass now0 | 1 | wait about 500 ms0 | 2 | wait about 1000 ms0 | 3 | reject500 | 2 | wait about 1000 msImagine four arrivals at the same instant, in the listed order. Every half-second drains one unit of excess. A rejected request does not add another admitted unit. At 500 ms the next arrival uses the newly released capacity, so it remains within burst 2. This arithmetic is an idealized teaching model; actual clocks, scheduling, and multiple zones affect observations. A burst is extra capacity, not a promise that every request inside a time window will succeed.
Decide whether excess traffic should wait
# Immediate admission within the burst budgetlimit_req zone=search_rate burst=2 nodelay;# OR allow one excess request immediately, then delay# limit_req zone=search_rate burst=2 delay=1;With nodelay, the initial request and the two excess requests can proceed without a limiter-imposed wait. The budget still fills, so a fourth immediate request is rejected. With delay=1 (introduced in 1.15.7), the first excess unit is admitted immediately and the next is delayed. Choose delay when smoothing the work is useful, or immediate bursts when the backend tolerates them. Waiting consumes resources too. Do not copy both alternatives into one route. Default burst is zero.
Common mistakes and practice
A generous burst does not increase the long-run rate, and Nginx does not automatically send a Retry-After header when you choose 429. Clients should use bounded backoff and respect an explicit retry contract where provided. Try a fresh key at 4r/s with burst 1 and three simultaneous arrivals, then another at 250 ms. Explain which requests proceed, wait, or fail before moving to connection limits. See the chapter lab for a finite local test.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Request rate limiting
Understand the request-rate budget, burst capacity and delay behavior, then predict the outcome of a short request sequence.
At 2r/s, how long does one unit of excess take to drain?
For a fresh key, burst=2 nodelay and four simultaneous arrivals, what is the idealized admission result?
What does declaring limit_req_zone alone accomplish?
What does delay=1 with burst=2 do for a fresh key?
Which response is configured by limit_req_status 429?