limit-req
Limits the request rate with a leaky bucket.
limit-reqPriority 1001What it does
limit-req holds the requests that reach a route to a steady rate. It counts them under a key that tells callers apart, for example the client address remote_addr. Requests per second is that rate, and Burst is how many requests may arrive above it before one is refused.
A request inside the burst waits until the rate allows it, unless No delay is on. A request above the burst is answered by the gateway itself with Rejected code, 503 unless you set another, as How G8 handles a request describes. With a rate of 10, a burst of 20 and the code set to 429 as in the example, 40 requests sent at once are answered partly 200 and partly 429.
When to use it
- A backend that copes with a steady flow but not with spikes: requests above the rate wait their turn instead of reaching it together.
- A rate for each client address, for example 10 requests a second, where a client that sends a short burst is slowed down rather than refused.
- A rate for each tenant rather than each address, with Key type set to
var_combinationand a key like$remote_addr-$http_x_tenant. - A rate that several gateway nodes enforce together, through one Redis server or a Redis cluster.
Not for a quota counted per time window, for example 100 requests a minute — see limit-count (Limits the number of requests in a time window, per key) or capping how many requests are open at once — see limit-conn (Limits concurrent connections per key).
Set it up
Allow 10 requests a second for each client address and let 20 more wait their turn
Under Basics set Requests per second to 10, set Burst to 20 and set Key to remote_addr. Under Behavior set Rejected code to 429.
{
"rate": 10,
"burst": 20,
"key": "remote_addr",
"rejected_code": 429
}
More scenarios
Things to know
- Rejected code — Set to
429, the code that tells a client it sent too many requests: with a rate of 10 and a burst of 20, 40 requests sent at once are answered partly200and partly429. - Rejected code — Unset, a request above the burst is answered
503, the answer of a service that is unavailable. - No delay — Off, a request inside the burst waits until the rate allows it, and on it is answered at once, with only requests above the burst refused.
- Requests per second — Requests per second and Burst take a number. A $variable like
$http_x_rateis refused when you save. - Requests per second — A rate of
0is refused when you save. - Key — A variable the request does not carry, or a name with a dollar sign under Variable, is saved without a word, and every request is then counted by client address. So is a combination in which no $name resolves.
- Policy — Policy is
local,redisorredis-cluster.redis-sentinelis refused when you save. - Allow degradation — When the limit cannot be counted, because the store is unreachable or a value resolves to nothing, off answers 500 and on lets every request through unlimited.
- AdviceBehind a load balancer, add real-ip to the same route, so that each client is held to the rate by its own address and not by the load balancer's.
Settings
The fields as the console shows them, section by section. A field under “When … is …” is there only while that value is set.
Basics
ratenumberRequiredThe steady number of requests per second let through, where a fraction such as 0.5 is allowed.
burstnumberRequiredHow many requests may arrive above the rate before one is refused.
keystringRequiredA request variable without its dollar sign, such as remote_addr, or text with $names under Variable combination.
remote_addrkey_typestringdefault varVariable reads one request variable by name, and Variable combination expands every $name in the text into one key.
Behavior
policystringdefault localWhen Policy is redis
redis_hoststringRequiredredis_portintegerdefault 6379redis_databaseintegerdefault 0redis_usernamestringredis_passwordstringStored on the engine and shown only to roles that manage this object.
redis_sslbooleandefault falseredis_ssl_verifybooleandefault falseredis_server_namestringThe server name sent in the TLS handshake and, with verification on, checked against the certificate; unset, the host is sent, and an address never is.
redis_timeoutintegerdefault 1000redis_keepalive_poolintegerdefault 100redis_keepalive_timeoutintegerdefault 10000When Policy is redis-cluster· 8 fields
redis_cluster_namestringRequiredredis_cluster_nodesarray of stringsRequiredredis_cluster_sslbooleandefault falseredis_cluster_ssl_verifybooleandefault falseredis_passwordstringStored on the engine and shown only to roles that manage this object.
redis_timeoutintegerdefault 1000redis_keepalive_poolintegerdefault 100redis_keepalive_timeoutintegerdefault 10000nodelaybooleandefault falserejected_codeintegerdefault 503rejected_msgstringallow_degradationbooleandefault false