limit_req_zone and limit_req in Nginx - Time & Space Complexity
When nginx limits request rates, it must track and check each request against stored data.
We want to understand how the time to process requests grows as more requests come in.
Analyze the time complexity of the following nginx configuration snippet.
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
location / {
limit_req zone=one burst=5;
proxy_pass http://backend;
}
}
This code limits requests per IP address to 1 request per second with some burst allowance.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking and updating the request count for each IP in the shared memory zone.
- How many times: Once per incoming request.
Each request triggers a lookup and update in a fixed-size memory zone keyed by IP.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 lookups and updates |
| 100 | 100 lookups and updates |
| 1000 | 1000 lookups and updates |
Pattern observation: The work grows linearly with the number of requests.
Time Complexity: O(n)
This means the time to process requests grows directly with the number of requests.
[X] Wrong: "The request limiting happens instantly no matter how many requests come in."
[OK] Correct: Each request requires a lookup and update in memory, so more requests mean more work.
Understanding how nginx handles request limiting helps you reason about performance under load, a useful skill in real-world server management.
What if we increased the burst size? How would the time complexity change?
Practice
limit_req_zone directive in nginx?Solution
Step 1: Understand the role of
This directive sets the shared memory zone and key used to track requests for limiting.limit_req_zoneStep 2: Differentiate from
limit_reqlimit_reqapplies the rules, butlimit_req_zonedefines them.Final Answer:
To define the key and rate limit parameters for request limiting -> Option CQuick Check:
limit_req_zonedefines limits [OK]
- Confusing
limit_req_zonewithlimit_req - Thinking it applies limits directly
- Assuming it logs requests
mylimit with 10MB memory and 5 requests per second per IP?Solution
Step 1: Check the correct order of parameters
The syntax islimit_req_zone key zone=name:size rate=rate;so key first, then zone with name and size, then rate.Step 2: Validate values
$binary_remote_addris preferred key, zone name ismylimit, size is 10m, rate is 5r/s (requests per second).Final Answer:
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=5r/s; -> Option AQuick Check:
Correct syntax = limit_req_zone $binary_remote_addr zone=mylimit:10m rate=5r/s; [OK]
- Swapping zone and key order
- Using wrong memory size units
- Confusing rate units (r/s vs r/m)
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
location /api/ {
limit_req zone=one burst=2;
}
}What happens if a client sends 4 requests instantly to
/api/?Solution
Step 1: Understand rate and burst
Rate is 1 request per second, burst=2 allows 2 extra requests to queue.Step 2: Analyze 4 instant requests
First request passes immediately, next 2 are delayed (burst), 4th exceeds burst and is rejected.Final Answer:
1 request processed immediately, 2 delayed, 1 rejected -> Option BQuick Check:
burst=2 allows 2 queued requests [OK]
- Assuming all requests pass immediately
- Ignoring burst parameter
- Thinking burst rejects requests
limit_req_zone $binary_remote_addr zone=rlimit:10m rate=10r/s;
server {
location / {
limit_req zone=rlimit burst=5;
}
}But nginx logs show no rate limiting is applied. What is the likely error?
Solution
Step 1: Check key correctness
Using an incorrect key inlimit_req_zone(e.g.,$remote_addrinstead of the preferred$binary_remote_addr) prevents proper rate limiting from being applied.Step 2: Confirm directive placement
limit_req_zonemust be in http context,limit_reqin server/location; placement here is correct.Final Answer:
Missinglimit_reqdirective insideserverblock -> Option DQuick Check:
limit_reqdirective is required to apply limits [OK]
- Placing limit_req_zone inside server block
- Assuming nodelay is required
- Not matching key with client IP
Solution
Step 1: Define correct rate and burst
Rate is 10r/s, burst 20 means allow 20 extra requests queued without delay.Step 2: Use
nodelayto avoid delaying burst requestsnodelaymakes burst requests processed immediately, matching requirement.Step 3: Validate key and zone size
$binary_remote_addris preferred key, 10m memory is sufficient.Final Answer:
limit_req_zone $binary_remote_addr zone=fastlimit:10m rate=10r/s; limit_req zone=fastlimit burst=20 nodelay; -> Option AQuick Check:
burst=20 with nodelay = immediate burst allowed [OK]
- Swapping burst and rate values
- Omitting nodelay causing delays
- Using wrong key variable
Ready to prove it?
Pass the test and this pattern moves unknown
