Skip to main content

Limit Count

The limit-count plugin uses fixed or sliding windows to limit the number of requests associated with a key during a configured time interval. Requests exceeding the quota are rejected, and responses include quota headers by default.

Local vs Redis Rate Limiting​

The limit-count 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 policy is set or when policy is local.
  • 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. Set policy to redis for a single Redis instance, redis-cluster for a Redis cluster, or redis-sentinel for Redis nodes managed by Redis Sentinel.

API7 Enterprise retains the separate limit-count-advanced plugin for backward compatibility. See Configuration for field availability and behavior.

Examples​

The examples below demonstrate how you can configure limit-count in different scenarios.

Apply Rate Limiting by Remote Address​

The following example demonstrates the rate limiting of requests by a single variable, remote_addr.

Create a route with limit-count plugin that allows for a quota of 1 within a 30-second window per remote address:

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "limit-count-route",
"uri": "/get",
"plugins": {
"limit-count": {
"count": 1,
"time_window": 30,
"rejected_code": 429,
"key_type": "var",
"key": "remote_addr",
"policy": "local"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'

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 30-second time interval, you should receive an HTTP/1.1 429 Too Many Requests response, indicating the request surpasses the quota threshold.

Apply Rate Limiting with a Sliding Window​

The following example demonstrates how to use a sliding window to smooth traffic bursts at window boundaries by weighting the previous window's count. Sliding windows were introduced in API7 Enterprise 3.9.14 and 3.10.1, and APISIX 3.18.0.

Create a route with the limit-count plugin that allows a quota of 10 requests within a 30-second sliding window per remote address:

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "limit-count-route",
"uri": "/get",
"plugins": {
"limit-count": {
"count": 10,
"time_window": 30,
"rejected_code": 429,
"key_type": "var",
"key": "remote_addr",
"policy": "local",
"window_type": "sliding"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'

Send a request to verify:

curl -i "http://127.0.0.1:9080/get"

You should see an HTTP/1.1 200 OK response. As you keep sending requests, the sliding window enforces the quota more evenly across the boundary between consecutive windows than the fixed window does.

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. It allows for a quota of 1 within a 30-second window per remote address and for each consumer.

Create 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 consumer john:

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 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 consumer jane:

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-count plugins:

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "limit-count-route",
"uri": "/get",
"plugins": {
"key-auth": {},
"limit-count": {
"count": 1,
"time_window": 30,
"rejected_code": 429,
"key_type": "var_combination",
"key": "$remote_addr $consumer_name",
"policy": "local"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'

❶ key-auth: enable key authentication on the route.

❷ key_type: set to var_combination to interpret the key as a combination of variables.

❸ key: set to $remote_addr $consumer_name to apply rate limiting quota by remote address and for each consumer.

Send a request as the consumer jane:

curl -i "http://127.0.0.1:9080/get" -H 'apikey: jane-key'

You should see an HTTP/1.1 200 OK response with the corresponding response body.

This request has consumed all the quota set for the time window. If you send the same request as the consumer jane within the same 30-second time interval, you should receive an HTTP/1.1 429 Too Many Requests response, indicating the request surpasses the quota threshold.

Send the same request as the consumer john within the same 30-second time interval:

curl -i "http://127.0.0.1:9080/get" -H 'apikey: john-key'

You should see an HTTP/1.1 200 OK response with the corresponding response body, indicating the request is not rate limited.

If you send the same request as the consumer john again within the same 30-second interval, 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.

Count Requests by Path with a Dynamic Limit​

This example assigns five requests per minute to /get, uses a fallback quota of two requests for /anything/fallback, and skips the plugin on other paths. Replace the illustrative path mapping with a trusted policy source. Do not let the client choose its own quota.

adc.yaml
services:
- name: dynamic-limit-service
labels:
docs-example: limit-count-dynamic
routes:
- name: dynamic-limit-route
uris:
- "/*"
plugins:
serverless-pre-function:
phase: rewrite
functions:
- |
return function(conf, ctx)
local core = require("apisix.core")
core.ctx.register_var("docs_path_limit", function(request_ctx)
return request_ctx.var.uri == "/get" and 5 or nil
end)
end
limit-count:
_meta:
filter:
- [uri, "~~", "^/(get|anything/fallback)$"]
count: "${docs_path_limit ?? 2}"
time_window: 60
key_type: var
key: uri
policy: local
window_type: fixed
allow_degradation: false
rejected_code: 429
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1

Preview the resources owned by this example:

adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=limit-count-dynamic

Synchronize the reviewed configuration:

adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=limit-count-dynamic

❶ Registers docs_path_limit during rewrite. It resolves to 5 for /get and is missing on /anything/fallback.

❷ Runs limit-count only on /get and /anything/fallback.

❸ Uses 2 when docs_path_limit is missing. Both count and time_window can use the ?? fallback syntax, and the resolved value must be a positive integer.

❹ Keeps separate counters for the normalized request paths. Query strings do not create separate keys.

Send requests to each path in a fresh one-minute window:

for request in 1 2 3 4 5 6; do
curl -sS -o /dev/null -w '%{http_code}\n' \
"http://127.0.0.1:9080/get"
done

for request in 1 2 3; do
curl -sS -o /dev/null -w '%{http_code}\n' \
"http://127.0.0.1:9080/anything/fallback"
done

curl -sS -D - -o /dev/null \
"http://127.0.0.1:9080/anything"

The results should show each configured path behavior:

  • /get returns five 200 responses followed by 429.
  • /anything/fallback returns 200, 200, and 429.
  • /anything returns HTTP/1.1 200 OK without X-RateLimit-* headers because the filter skips the plugin.

For shared counters, apply the same variable and filter settings to the complete Redis example. For smoother enforcement at window boundaries, use the sliding-window example.

Count Requests by Authenticated Identity​

In API7 Gateway, the authentication method determines which identity the limiter can use.

Authentication on the requestVariable for the rule key
Key Auth, Basic Auth, JWT Auth, HMAC Auth, or LDAP Auth attaches a Consumer.$consumer_name
An authentication plugin selects an anonymous Consumer.$consumer_name, which resolves to the anonymous Consumer username.
OpenID Connect, SAML Auth, Feishu Auth, DingTalk Auth, or Developer Portal OAuth supplies an external user.A field supplied by the authentication flow, using ${external_user.FIELD}. For example, OpenID Connect can provide ${external_user.preferred_username}.
Unauthenticated request with no anonymous Consumer.Use an explicit IP rule to count these requests by address.

The following Admin API example verifies that Basic Auth and Key Auth consumers receive separate quotas through the same route. Export an admin-role key as ADMIN_API_KEY before starting.

Create the Basic Auth consumer and credential:

curl "http://127.0.0.1:9180/apisix/admin/consumers" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"username": "basic-user"
}'

curl "http://127.0.0.1:9180/apisix/admin/consumers/basic-user/credentials" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"id": "basic-user-credential",
"plugins": {
"basic-auth": {
"username": "basic-user",
"password": "basic-password"
}
}
}'

Create the Key Auth consumer and credential:

curl "http://127.0.0.1:9180/apisix/admin/consumers" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"username": "key-user"
}'

curl "http://127.0.0.1:9180/apisix/admin/consumers/key-user/credentials" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"id": "key-user-credential",
"plugins": {
"key-auth": {
"key": "key-user-key"
}
}
}'

Create a route that accepts either credential and applies a two-request quota to each Consumer:

curl "http://127.0.0.1:9180/apisix/admin/routes/identity-limit" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"uri": "/anything/identity-limit",
"plugins": {
"multi-auth": {
"auth_plugins": [
{
"basic-auth": {}
},
{
"key-auth": {
"header": "apikey",
"query": "apikey"
}
}
]
},
"limit-count": {
"rules": [
{
"key": "consumer:$consumer_name",
"count": 2,
"time_window": 60
}
],
"rejected_code": 429
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'

❶ Accepts either Basic Auth or Key Auth and attaches the matched Consumer to the request.

❷ Uses a separate rule key for each Consumer.

❸ Allows two requests per Consumer in each 60-second window.

Send three Basic Auth requests within one minute:

for request in 1 2 3; do
curl -sS -u "basic-user:basic-password" \
-o /dev/null -w '%{http_code}\n' \
"http://127.0.0.1:9080/anything/identity-limit"
done

The responses should be 200, 200, and 429. The Key Auth consumer has a separate counter, so its first request succeeds:

curl -i "http://127.0.0.1:9080/anything/identity-limit" \
-H "apikey: key-user-key"

When a rule key does not resolve, the gateway skips that rule. If every rule is skipped, the request returns HTTP 500 unless allow_degradation is enabled.

For an authentication flow that supplies external_user, replace the rule key with a field supplied by that flow.

The client IP fallback applies only outside rules mode. See Limit Count configuration.

When you finish testing, delete the route and test consumers:

curl "http://127.0.0.1:9180/apisix/admin/routes/identity-limit" -X DELETE \
-H "X-API-KEY: ${ADMIN_API_KEY}"

curl "http://127.0.0.1:9180/apisix/admin/consumers/basic-user" -X DELETE \
-H "X-API-KEY: ${ADMIN_API_KEY}"

curl "http://127.0.0.1:9180/apisix/admin/consumers/key-user" -X DELETE \
-H "X-API-KEY: ${ADMIN_API_KEY}"

Share Quota among Routes​

The following example demonstrates the sharing of rate limiting quota among multiple routes by configuring the group of the limit-count plugin.

Note that the configurations of the limit-count plugin of the same group should be identical. To avoid update anomalies and repetitive configurations, you can create a service with limit-count plugin and upstream for routes to connect to.

Create a service with rate limiting group:

curl "http://127.0.0.1:9180/apisix/admin/services" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "limit-count-service",
"plugins": {
"limit-count": {
"count": 1,
"time_window": 30,
"rejected_code": 429,
"policy": "local",
"group": "srv1"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'

Create two routes that use the same service to share the quota:

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "limit-count-route-1",
"service_id": "limit-count-service",
"uri": "/get1",
"plugins": {
"proxy-rewrite": {
"uri": "/get"
}
}
}'
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "limit-count-route-2",
"service_id": "limit-count-service",
"uri": "/get2",
"plugins": {
"proxy-rewrite": {
"uri": "/get"
}
}
}'
note

The proxy-rewrite plugin is used to rewrite the URI to /get so that requests are forwarded to the correct endpoint.

Send a request to route /get1:

curl -i "http://127.0.0.1:9080/get1"

You should see an HTTP/1.1 200 OK response with the corresponding response body.

Send the same request to route /get2 within the same 30-second time interval:

curl -i "http://127.0.0.1:9080/get2"

You should receive an HTTP/1.1 429 Too Many Requests response, which verifies the two routes share the same rate limiting quota.

Share Quota Among APISIX Nodes with a Redis Server​

The following example demonstrates rate limiting across multiple APISIX nodes with a Redis server, so different APISIX nodes share the same rate limiting quota.

Configure the route with Redis connection details. Adjust the Redis host, credentials, and database for your environment.

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "limit-count-route",
"uri": "/get",
"plugins": {
"limit-count": {
"count": 1,
"time_window": 30,
"rejected_code": 429,
"key": "remote_addr",
"policy": "redis",
"redis_host": "192.168.xxx.xxx",
"redis_port": 6379,
"redis_password": "p@ssw0rd",
"redis_database": 1
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'

❶ policy: set to redis to use a Redis instance for rate limiting.

❷ redis_host: set to Redis instance IP address.

❸ redis_port: set to Redis instance listening port.

❹ redis_password: set to the password of the Redis instance, if any.

❺ redis_database: set to the database number in the Redis instance.

Send a request to an APISIX instance:

curl -i "http://127.0.0.1:9080/get"

You should see an HTTP/1.1 200 OK response with the corresponding response body.

Send the same request to a different APISIX instance within the same 30-second interval. You should receive an HTTP/1.1 429 Too Many Requests response, verifying that routes on different APISIX nodes share the same quota.

Reduce Redis round trips

To batch Redis updates in the complete example above, add sync_interval: 1 to the limit-count configuration. Delayed synchronization was introduced in API7 Enterprise 3.9.14 and 3.10.1, and APISIX 3.18.0.

Until synchronization, workers can hold different local increments, so concurrent requests can temporarily exceed the configured count. In rules mode, each rule synchronizes its state independently.

Use direct synchronization when exact cross-node enforcement matters more than reducing Redis traffic. See Configuration for supported policies and value constraints, and Rate Limiting Information for request-level diagnostics.

Share Quota Among APISIX Nodes with a Redis Cluster​

You can also use a Redis cluster to apply the same quota across multiple APISIX nodes, such that different APISIX nodes share the same rate limiting quota.

Ensure that your Redis instances are running in cluster mode. A minimum of two nodes are required for the limit-count plugin configurations.

Configure the route with Redis cluster details. Adjust the cluster nodes, credentials, and cluster name for your environment.

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "limit-count-route",
"uri": "/get",
"plugins": {
"limit-count": {
"count": 1,
"time_window": 30,
"rejected_code": 429,
"key": "remote_addr",
"policy": "redis-cluster",
"redis_cluster_nodes": [
"192.168.xxx.xxx:6379",
"192.168.xxx.xxx:16379"
],
"redis_password": "p@ssw0rd",
"redis_cluster_name": "redis-cluster",
"redis_cluster_ssl": true
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'

❶ policy: set to redis-cluster to use a Redis cluster for rate limiting.

❷ redis_cluster_nodes: set to Redis node addresses in the Redis cluster.

❸ redis_password: set to the password of the Redis cluster, if any.

❹ redis_cluster_name: set to the Redis cluster name.

➎ redis_cluster_ssl: enable SSL/TLS communication with Redis cluster.

Send a request to an APISIX instance:

curl -i "http://127.0.0.1:9080/get"

You should see an HTTP/1.1 200 OK response with the corresponding response body.

Send the same request to a different APISIX instance within the same 30-second interval. You should receive an HTTP/1.1 429 Too Many Requests response, verifying that routes on different APISIX nodes share the same quota.

Share Quota Among APISIX Nodes with Redis Sentinel​

Redis Sentinel provides a shared counter backend and discovers a promoted Redis master after failover. Introduced in API7 Enterprise 3.9.14 and 3.10.1, and APISIX 3.18.0.

Configure the route with Redis Sentinel details. Adjust the Sentinel nodes, credentials, and monitored master name for your environment.

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "limit-count-route",
"uri": "/get",
"plugins": {
"limit-count": {
"count": 1,
"time_window": 30,
"rejected_code": 429,
"key": "remote_addr",
"policy": "redis-sentinel",
"redis_sentinels": [
{ "host": "192.168.xxx.xxx", "port": 26379 },
{ "host": "192.168.xxx.xxx", "port": 26380 }
],
"redis_master_name": "mymaster",
"sentinel_username": "sentinel-user",
"sentinel_password": "p@ssw0rd",
"redis_database": 1
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'

Where:

  • policy is set to redis-sentinel to use Redis nodes managed by Redis Sentinel.
  • redis_sentinels lists the Sentinel nodes, each with a host and port.
  • redis_master_name is the name of the master that Sentinel monitors.
  • sentinel_username and sentinel_password authenticate with the Sentinel nodes, if required.

Send a request to an APISIX instance:

curl -i "http://127.0.0.1:9080/get"

You should see an HTTP/1.1 200 OK response with the corresponding response body.

Send the same request to a different APISIX instance within the same 30-second interval. You should receive an HTTP/1.1 429 Too Many Requests response, verifying that routes on different APISIX nodes share the same quota.

Rate Limit with Anonymous Consumer​

The following example demonstrates how you can configure different rate limiting policies by regular and anonymous consumers, where the anonymous consumer does not need to authenticate and has less quota. While this example uses key-auth for authentication, the anonymous consumer can also be configured with basic-auth, jwt-auth, and hmac-auth.

Create consumer john with a quota of 3:

curl "http://127.0.0.1:9180/apisix/admin/consumers" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"username": "john",
"plugins": {
"limit-count": {
"count": 3,
"time_window": 30,
"rejected_code": 429,
"policy": "local"
}
}
}'

Create key-auth credential for consumer john:

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 an anonymous user anonymous with a quota of 1:

curl "http://127.0.0.1:9180/apisix/admin/consumers" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"username": "anonymous",
"plugins": {
"limit-count": {
"count": 1,
"time_window": 30,
"rejected_code": 429,
"policy": "local"
}
}
}'

Create a route with key-auth plugin that accepts anonymous consumer:

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "key-auth-route",
"uri": "/anything",
"plugins": {
"key-auth": {
"anonymous_consumer": "anonymous"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'

To verify, send five consecutive requests with john's key:

resp=$(seq 5 | xargs -I{} curl "http://127.0.0.1:9080/anything" -H 'apikey: john-key' -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": $count_200, "429": $count_429

You should see the following response, showing that out of the 5 requests, 3 requests were successful (status code 200) while the others were rejected (status code 429).

200: 3, 429: 2

Send five anonymous requests:

resp=$(seq 5 | xargs -I{} curl "http://127.0.0.1:9080/anything" -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": $count_200, "429": $count_429

You should see the following response, showing that only one request was successful:

200: 1, 429: 4

Customize Rate Limiting Headers​

The following example demonstrates how you can use plugin metadata to customize the rate limiting response header names, which are by default X-RateLimit-Limit, X-RateLimit-Remaining, and X-RateLimit-Reset.

Configure plugin metadata to customize rate limiting headers:

curl "http://127.0.0.1:9180/apisix/admin/plugin_metadata/limit-count" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"limit_header": "X-Custom-RateLimit-Limit",
"remaining_header": "X-Custom-RateLimit-Remaining",
"reset_header": "X-Custom-RateLimit-Reset"
}'

Create a route with limit-count plugin:

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "limit-count-route",
"uri": "/anything/limit-count-metadata",
"plugins": {
"limit-count": {
"count": 1,
"time_window": 30,
"rejected_code": 429,
"key_type": "var",
"key": "remote_addr",
"policy": "local"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'

Send a request to verify:

curl -i "http://127.0.0.1:9080/anything/limit-count-metadata"

You should receive an HTTP/1.1 200 OK response and see the following headers:

X-Custom-RateLimit-Limit: 1
X-Custom-RateLimit-Remaining: 0
X-Custom-RateLimit-Reset: 28