You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/latest/plugins/limit-count.md
+35-2Lines changed: 35 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ keywords:
4
4
- Apache APISIX
5
5
- API Gateway
6
6
- Limit Count
7
-
description: The limit-count plugin uses a fixed window algorithm to limit the rate of requests by the number of requests within a given time interval. Requests exceeding the configured quota will be rejected.
7
+
description: The limit-count plugin limits the rate of requests by the number of requests within a given time interval. It supports both fixed window and sliding window behaviors. Requests exceeding the configured quota will be rejected.
8
8
---
9
9
10
10
<!--
@@ -32,7 +32,12 @@ description: The limit-count plugin uses a fixed window algorithm to limit the r
32
32
33
33
## Description
34
34
35
-
The `limit-count` plugin uses a fixed window algorithm to limit the rate of requests by the number of requests within a given time interval. Requests exceeding the configured quota will be rejected.
35
+
The `limit-count` plugin limits the rate of requests by the number of requests within a given time interval. It supports both **fixed window** and **sliding window** behaviors.
36
+
37
+
- When `window_type` is `fixed` (default), the plugin uses a fixed window algorithm.
38
+
- When `window_type` is `sliding`, and the `policy` is `redis` or `redis-cluster`, the plugin enforces an exact **N requests per rolling time window** using a sliding window algorithm.
39
+
40
+
Requests exceeding the configured quota will be rejected.
36
41
37
42
You may see the following rate limiting headers in the response:
38
43
@@ -46,6 +51,7 @@ You may see the following rate limiting headers in the response:
| count | integer | True || > 0 | The maximum number of requests allowed within a given time interval. |
48
53
| time_window | integer | True || > 0 | The time interval corresponding to the rate limiting `count` in seconds. |
54
+
| window_type | string | False | fixed |["fixed","sliding"]| The window behavior type. `fixed` uses a fixed window algorithm. `sliding` uses a sliding window algorithm to enforce an exact number of requests per rolling window. `sliding` is only supported when `policy` is `redis` or `redis-cluster`. |
49
55
| key_type | string | False | var |["var","var_combination","constant"]| The type of key. If the `key_type` is `var`, the `key` is interpreted a variable. If the `key_type` is `var_combination`, the `key` is interpreted as a combination of variables. If the `key_type` is `constant`, the `key` is interpreted as a constant. |
50
56
| key | string | False | remote_addr || The key to count requests by. If the `key_type` is `var`, the `key` is interpreted a variable. The variable does not need to be prefixed by a dollar sign (`$`). If the `key_type` is `var_combination`, the `key` is interpreted as a combination of variables. All variables should be prefixed by dollar signs (`$`). For example, to configure the `key` to use a combination of two request headers `custom-a` and `custom-b`, the `key` should be configured as `$http_custom_a $http_custom_b`. If the `key_type` is `constant`, the `key` is interpreted as a constant value. |
51
57
| rejected_code | integer | False | 503 |[200,...,599]| The HTTP status code returned when a request is rejected for exceeding the threshold. |
@@ -401,6 +407,33 @@ You should see an `HTTP/1.1 200 OK` response with the corresponding response bod
401
407
402
408
Send the same request to a different APISIX instance within the same 30-second time interval, you should receive an `HTTP/1.1 429 Too Many Requests` response, verifying routes configured in different APISIX nodes share the same quota.
403
409
410
+
### Performance considerations (sliding window)
411
+
412
+
When `window_type` is set to `sliding` and the `policy` is `redis` or `redis-cluster`, this Plugin uses a Redis ZSET to store timestamps for recent requests (a sliding log).
413
+
414
+
Roughly, the memory usage for sliding window per Redis instance can be approximated as:
415
+
416
+
\[
417
+
\text{Memory} \approx K \times C \times B
418
+
\]
419
+
420
+
Where:
421
+
422
+
-\(K\): number of active keys that receive traffic within a `time_window`
423
+
-\(C\): `count` (maximum requests allowed per key within the window)
424
+
-\(B\): bytes per ZSET entry (timestamp + member + metadata). A conservative estimate is around 100 bytes.
- Monitor Redis memory and CPU when enabling sliding windows.
434
+
- Prefer relatively small `count` values (tens to low hundreds) for keys with high QPS.
435
+
- Consider using `window_type = "fixed"` (or `limit-req`) for very high throughput keys with large `count` or very high key cardinality.
436
+
404
437
### Rate Limit with Anonymous Consumer
405
438
406
439
does not need to authenticate and has less quotas. While this example uses [`key-auth`](./key-auth.md) for authentication, the anonymous Consumer can also be configured with [`basic-auth`](./basic-auth.md), [`jwt-auth`](./jwt-auth.md), and [`hmac-auth`](./hmac-auth.md).
0 commit comments