Limit Req
The limit-req plugin uses a leaky bucket to enforce a sustained request rate per key. It can reject excess requests immediately or delay them within a configured burst allowance.
Local vs Redis Rate Limiting
The limit-req plugin supports two modes of rate limiting:
- Local rate limiting: Limits are enforced independently on each gateway instance. Each instance maintains its own counters, so the effective limit is roughly (limit × number of instances) when traffic is spread across instances. This is the default when no
policyis set or whenpolicyislocal. - Redis-based rate limiting: Limits are shared across all gateway instances through Redis. All instances share the same quota, so the configured limit applies to all gateway instances.
Understand Burst and Request Spacing
rate drains accumulated excess continuously; burst is the excess the limiter will tolerate. They do not define a fixed rate + burst quota for each wall-clock second.
For a fresh key with rate: 2 and burst: 3, assume requests reach the limiter at exactly the times below:
| Arrival pattern | Result |
|---|---|
| Five requests at the same instant | The first request starts the bucket. The next three occupy the burst allowance; the fifth is rejected. |
| Five requests spaced 0.5 seconds apart | Excess drains at two requests per second, so all five can pass. |
| Another request 0.5 seconds after the simultaneous batch | One unit of excess has drained, so one more request can be admitted. |
With nodelay: false, the three admitted excess requests in the simultaneous batch wait approximately 0.5, 1, and 1.5 seconds. With nodelay: true, they proceed immediately, but they still occupy the allowance. This setting does not increase the sustained rate or clear the bucket.
Use Implement API Throttling to configure the bucket and compare simultaneous requests with requests spaced at the configured rate. Wait for the bucket to drain between trials, and record timestamps and latency because client processes only approximate simultaneous arrival.
Examples
The examples below demonstrate how you can configure limit-req in different scenarios.
Apply Rate Limiting by Remote Address
The following example demonstrates the rate limiting of HTTP requests by a single variable, remote_addr.
Create a route with limit-req plugin that allows for 1 QPS per remote address:
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '
{
"id": "limit-req-route",
"uri": "/get",
"plugins": {
"limit-req": {
"rate": 1,
"burst": 0,
"key": "remote_addr",
"key_type": "var",
"rejected_code": 429,
"policy": "local",
"nodelay": true
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
services:
- name: httpbin
labels:
docs-example: limit-req
routes:
- uris:
- /get
name: limit-req-route
plugins:
limit-req:
rate: 1
burst: 0
key: remote_addr
key_type: var
rejected_code: 429
policy: local
nodelay: true
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
Preview the changes and confirm that they only affect resources for this guide:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=limit-req
Synchronize the configuration to the gateway:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=limit-req
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: limit-req-plugin-config
spec:
plugins:
- name: limit-req
config:
rate: 1
burst: 0
key: remote_addr
key_type: var
rejected_code: 429
policy: local
nodelay: true
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: limit-req-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /get
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: limit-req-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: limit-req-route
spec:
ingressClassName: apisix
http:
- name: limit-req-route
match:
paths:
- /get
methods:
- GET
upstreams:
- name: httpbin-external-domain
plugins:
- name: limit-req
config:
rate: 1
burst: 0
key: remote_addr
key_type: var
rejected_code: 429
policy: local
nodelay: true
Apply the configuration:
kubectl apply -f limit-req-ic.yaml
❶ rate: limit the QPS to 1.
❷ key: set to remote_addr to apply rate limiting quota by remote address and consumer.
❸ key_type: set to var to interpret the key as a variable.
Send a request to verify:
curl -i "http://127.0.0.1:9080/get"
You should see an HTTP/1.1 200 OK response.
The request has consumed all the quota allowed for the time window. If you send the request again within the same second, you should receive an HTTP/1.1 429 Too Many Requests response, indicating the request surpasses the quota threshold.
Implement API Throttling
The following example demonstrates how to configure burst to allow overrun of the rate limiting threshold by the configured value and achieve request throttling. You will also see a comparison against when throttling is not implemented.
Create a route with limit-req plugin that allows for 1 QPS per remote address, with a burst of 1:
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "limit-req-route",
"uri": "/get",
"plugins": {
"limit-req": {
"rate": 1,
"burst": 1,
"key": "remote_addr",
"rejected_code": 429,
"policy": "local"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
services:
- name: httpbin
labels:
docs-example: limit-req
routes:
- uris:
- /get
name: limit-req-route
plugins:
limit-req:
rate: 1
burst: 1
key: remote_addr
rejected_code: 429
policy: local
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
Preview the changes and confirm that they only affect resources for this guide:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=limit-req
Synchronize the configuration to the gateway:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=limit-req
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: limit-req-plugin-config
spec:
plugins:
- name: limit-req
config:
rate: 1
burst: 1
key: remote_addr
rejected_code: 429
policy: local
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: limit-req-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /get
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: limit-req-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: limit-req-route
spec:
ingressClassName: apisix
http:
- name: limit-req-route
match:
paths:
- /get
methods:
- GET
upstreams:
- name: httpbin-external-domain
plugins:
- name: limit-req
config:
rate: 1
burst: 1
key: remote_addr
rejected_code: 429
policy: local
Apply the configuration:
kubectl apply -f limit-req-ic.yaml
❶ burst: allow one request above the configured rate to be delayed for processing. The value is measured in requests, not requests per second.
Generate three requests to the route:
resp=$(seq 3 | xargs -I{} curl -i "http://127.0.0.1:9080/get" -o /dev/null -s -w "%{http_code}\n") && \
count_200=$(echo "$resp" | grep "200" | wc -l) && \
count_429=$(echo "$resp" | grep "429" | wc -l) && \
echo "200 responses: $count_200 ; 429 responses: $count_429"
You are likely to see that all three requests are successful:
200 responses: 3 ; 429 responses: 0
Set nodelay: true to admit the permitted burst without waiting. It preserves the burst allowance; setting burst: 0 instead removes that capacity. The examples below keep rate: 1 and burst: 1:
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes/limit-req-route" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"plugins": {
"limit-req": {
"nodelay": true
}
}
}'
Update the ADC YAML with nodelay: true:
services:
- name: httpbin
labels:
docs-example: limit-req
routes:
- uris:
- /get
name: limit-req-route
plugins:
limit-req:
rate: 1
burst: 1
key: remote_addr
rejected_code: 429
policy: local
nodelay: true
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
Preview the changes and confirm that they only affect resources for this guide:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=limit-req
Synchronize the configuration with updated plugin settings:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=limit-req
Update the manifest file as such:
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: limit-req-plugin-config
spec:
plugins:
- name: limit-req
config:
rate: 1
burst: 1
key: remote_addr
rejected_code: 429
policy: local
nodelay: true
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: limit-req-route
spec:
ingressClassName: apisix
http:
- name: limit-req-route
match:
paths:
- /get
methods:
- GET
upstreams:
- name: httpbin-external-domain
plugins:
- name: limit-req
config:
rate: 1
burst: 1
key: remote_addr
rejected_code: 429
policy: local
nodelay: true
Apply the updated configuration:
kubectl apply -f limit-req-ic.yaml
Generate three requests to the route again:
resp=$(seq 3 | xargs -I{} curl -i "http://127.0.0.1:9080/get" -o /dev/null -s -w "%{http_code}\n") && \
count_200=$(echo "$resp" | grep "200" | wc -l) && \
count_429=$(echo "$resp" | grep "429" | wc -l) && \
echo "200 responses: $count_200 ; 429 responses: $count_429"
With a fresh counter and requests close enough together to overlap, an illustrative result is:
200 responses: 2 ; 429 responses: 1
Apply Rate Limiting by Remote Address and Consumer Name
The following example demonstrates the rate limiting of requests by a combination of variables, remote_addr and consumer_name.
- Admin API
- ADC
- Ingress Controller
Create a consumer john:
curl "http://127.0.0.1:9180/apisix/admin/consumers" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"username": "john"
}'
Create key-auth credential for the consumer:
curl "http://127.0.0.1:9180/apisix/admin/consumers/john/credentials" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "cred-john-key-auth",
"plugins": {
"key-auth": {
"key": "john-key"
}
}
}'
Create a second consumer jane:
curl "http://127.0.0.1:9180/apisix/admin/consumers" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"username": "jane"
}'
Create key-auth credential for the consumer:
curl "http://127.0.0.1:9180/apisix/admin/consumers/jane/credentials" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "cred-jane-key-auth",
"plugins": {
"key-auth": {
"key": "jane-key"
}
}
}'
Create a route with key-auth and limit-req plugins:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "limit-req-route",
"uri": "/get",
"plugins": {
"key-auth": {},
"limit-req": {
"rate": 1,
"burst": 0,
"key": "$remote_addr $consumer_name",
"key_type": "var_combination",
"rejected_code": 429,
"policy": "local"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
Create two consumers and a route that enables rate limiting by consumers:
consumers:
- username: john
labels:
docs-example: limit-req
credentials:
- name: key-auth
type: key-auth
config:
key: john-key
- username: jane
labels:
docs-example: limit-req
credentials:
- name: key-auth
type: key-auth
config:
key: jane-key
services:
- name: limit-req-service
labels:
docs-example: limit-req
routes:
- name: limit-req-route
uris:
- /get
plugins:
key-auth: {}
limit-req:
rate: 1
burst: 0
key: "$remote_addr $consumer_name"
key_type: var_combination
rejected_code: 429
policy: local
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
Preview the changes and confirm that they only affect resources for this guide:
adc diff -f adc.yaml \
--include-resource-type consumer \
--include-resource-type service \
--label-selector docs-example=limit-req
Synchronize the configuration to the gateway:
adc sync -f adc.yaml \
--include-resource-type consumer \
--include-resource-type service \
--label-selector docs-example=limit-req
Create two consumers and a route that enables rate limiting by consumers:
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: Consumer
metadata:
namespace: aic
name: john
spec:
gatewayRef:
name: apisix
credentials:
- type: key-auth
name: primary-key
config:
key: john-key
---
apiVersion: apisix.apache.org/v1alpha1
kind: Consumer
metadata:
namespace: aic
name: jane
spec:
gatewayRef:
name: apisix
credentials:
- type: key-auth
name: primary-key
config:
key: jane-key
---
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: limit-req-plugin-config
spec:
plugins:
- name: key-auth
config:
_meta:
disable: false
- name: limit-req
config:
rate: 1
burst: 0
key: "$remote_addr $consumer_name"
key_type: var_combination
rejected_code: 429
policy: local
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: limit-req-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /get
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: limit-req-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
apiVersion: apisix.apache.org/v2
kind: ApisixConsumer
metadata:
namespace: aic
name: john
spec:
ingressClassName: apisix
authParameter:
keyAuth:
value:
key: john-key
---
apiVersion: apisix.apache.org/v2
kind: ApisixConsumer
metadata:
namespace: aic
name: jane
spec:
ingressClassName: apisix
authParameter:
keyAuth:
value:
key: jane-key
---
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: limit-req-route
spec:
ingressClassName: apisix
http:
- name: limit-req-route
match:
paths:
- /get
methods:
- GET
upstreams:
- name: httpbin-external-domain
plugins:
- name: key-auth
config:
_meta:
disable: false
- name: limit-req
config:
rate: 1
burst: 0
key: "$remote_addr $consumer_name"
key_type: var_combination
rejected_code: 429
policy: local
Apply the configuration:
kubectl apply -f limit-req-ic.yaml
❶ key-auth: enable key authentication on the route.
❷ key: set to $remote_addr $consumer_name to apply rate limiting quota by remote address and consumer.
❸ key_type: set to var_combination to interpret the key as a combination of variables.
Send two requests simultaneously, each for one consumer:
curl -i "http://127.0.0.1:9080/get" -H 'apikey: jane-key' & \
curl -i "http://127.0.0.1:9080/get" -H 'apikey: john-key' &
You should receive HTTP/1.1 200 OK for both requests, indicating the request has not exceeded the threshold for each consumer.
If you send more requests as either consumer within the same second, you should receive an HTTP/1.1 429 Too Many Requests response.
This verifies the plugin rate limits by the combination of variables, remote_addr and consumer_name.
Store Bucket State in Redis
The following example demonstrates how to store limit-req bucket state in Redis and inspect the stored hash and TTL.
The example uses Docker to connect a running gateway container to a password-protected Redis server. The container should publish Admin API port 9180 and proxy port 9080 on the local host. Export the gateway container name and an Admin API key before starting:
export GATEWAY_CONTAINER="replace-with-gateway-container-name"
export ADMIN_API_KEY="replace-with-admin-api-key"
export REDIS_PASSWORD="redis-password"
export REDIS_DATABASE=1
Create a dedicated network, connect the gateway, and start Redis:
docker network create limit-req-net
docker network connect limit-req-net "$GATEWAY_CONTAINER"
docker run -d \
--name limit-req-redis \
--network limit-req-net \
redis:8.10.2-alpine \
redis-server --requirepass "$REDIS_PASSWORD"
Verify the Redis connection:
docker exec \
-e REDISCLI_AUTH="$REDIS_PASSWORD" \
limit-req-redis \
redis-cli ping
The command should return PONG. If Redis is still starting, wait a moment and repeat the command.
Create a route that uses Redis for its bucket state:
curl "http://127.0.0.1:9180/apisix/admin/routes/limit-req-redis" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"uri": "/redis-rate-limit",
"plugins": {
"proxy-rewrite": {
"uri": "/get"
},
"limit-req": {
"rate": 0.1,
"burst": 6,
"key": "remote_addr",
"rejected_code": 429,
"nodelay": true,
"policy": "redis",
"redis_host": "limit-req-redis",
"redis_port": 6379,
"redis_password": "${REDIS_PASSWORD}",
"redis_database": ${REDIS_DATABASE}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}
EOF
❶ Allows a burst of seven requests, then drains the bucket at one request every ten seconds.
❷ Stores the bucket state in Redis so other gateway instances can use the same state.
❸ Connects to the Redis container and selects database 1.
With a fresh bucket, send eight requests from the same address in less than ten seconds. Seven pass the limiter and one returns HTTP 429.
for i in 1 2 3 4 5 6 7 8; do
curl -sS -o /dev/null -w '%{http_code}\n' "http://127.0.0.1:9080/redis-rate-limit" &
done
wait
Gateway instances share the bucket when they use the same Redis database, route and plugin configuration, and resolved client key.
Inspect the Redis Bucket
This optional verification uses the limit key: message logged at info level by API7 Enterprise 3.10.10 and APISIX 3.21.0.
Set nginx_config.error_log_level to info in the active config.yaml, then reload the Docker gateway:
docker exec "$GATEWAY_CONTAINER" apisix reload
If the container uses a different configuration or reload workflow, apply the log-level change through that deployment before continuing. Repeat the request test, then find the latest key in the container log:
docker logs "$GATEWAY_CONTAINER" 2>&1 \
| grep "limit key:" \
| tail -1
Copy the complete value after limit key: and inspect only that Redis hash:
export LIMIT_KEY='COPY_THE_LOGGED_LIMIT_KEY'
docker exec \
-e REDISCLI_AUTH="$REDIS_PASSWORD" \
limit-req-redis \
redis-cli -n "$REDIS_DATABASE" HGETALL "limit_req:${LIMIT_KEY}"
docker exec \
-e REDISCLI_AUTH="$REDIS_PASSWORD" \
limit-req-redis \
redis-cli -n "$REDIS_DATABASE" TTL "limit_req:${LIMIT_KEY}"
The hash contains excess and last. Each admitted request refreshes the TTL to 61 seconds. A rejection leaves the expiry unchanged. To repeat the test, wait until TTL returns -2.
If you need to clear a stale key, confirm the deployment, gateway release, and Redis database before removing the exact key.
For ADC or Ingress Controller, apply the same plugin fields to the remote-address example and use a Redis address reachable from every gateway instance.
When you finish testing, restore the previous nginx_config.error_log_level value in config.yaml and reload the gateway:
docker exec "$GATEWAY_CONTAINER" apisix reload
Then remove the route, Redis container, and dedicated network:
curl "http://127.0.0.1:9180/apisix/admin/routes/limit-req-redis" -X DELETE \
-H "X-API-KEY: ${ADMIN_API_KEY}"
docker rm -f limit-req-redis
docker network disconnect limit-req-net "$GATEWAY_CONTAINER"
docker network rm limit-req-net