# Limit Count Configuration

The `limit-count` plugin enforces API rate limits with fixed or sliding windows, rejecting requests after a configurable quota is exhausted.<!-- --> <!-- -->Configuration covers plugin parameters, plugin metadata, and a related variable.

## Parameters[​](#parameters "Direct link to Parameters")

See plugin [common configurations](https://docs.api7.ai/apisix/reference/plugin-common-configurations.md) for configuration options available to all plugins.

This plugin supports gateway references in string-valued configuration fields. Use the `$env://` prefix for environment variables or the `$secret://` prefix for values from a secret manager, such as HashiCorp Vault’s [KV secrets engine](https://developer.hashicorp.com/vault/docs/secrets/kv). The gateway resolves these references at runtime. ADC `${NAME}` interpolation is different because ADC resolves it while loading the configuration file, before synchronization. For more information, see [environment variables in plugin](https://docs.api7.ai/apisix/reference/environment-variables.md#plugins) and [secrets](https://docs.api7.ai/apisix/key-concepts/secrets.md).

* count

  integer | string

  valid value:

  greater than 0

  ***

  The maximum number of requests allowed within a given time interval.

  A string value can reference a [built-in variable](https://docs.api7.ai/apisix/reference/built-in-variables.md) by prefixing the variable with a dollar sign (`$`). Introduced in API7 Enterprise 3.9.14 and 3.10.1, and APISIX 3.16.0. Earlier versions accept only integer values.

  Required with `time_window` when `rules` is not configured. Do not configure `count` or `time_window` together with `rules`.

  A string value must resolve to a positive integer no greater than `9007199254740991`. An invalid value returns `500 Internal Server Error` unless `allow_degradation` is `true`. Introduced in API7 Enterprise 3.9.16 and 3.10.2, and APISIX 3.18.0.

* time\_window

  integer | string

  valid value:

  greater than 0

  ***

  The time interval corresponding to the rate limiting `count` in seconds.

  A string value can reference a [built-in variable](https://docs.api7.ai/apisix/reference/built-in-variables.md) by prefixing the variable with a dollar sign (`$`). Introduced in API7 Enterprise 3.9.14 and 3.10.1, and APISIX 3.16.0. Earlier versions accept only integer values.

  Required with `count` when `rules` is not configured. Do not configure `count` or `time_window` together with `rules`.

  A string value must resolve to a positive integer no greater than `9007199254740991`. An invalid value returns `500 Internal Server Error` unless `allow_degradation` is `true`. Introduced in API7 Enterprise 3.9.16 and 3.10.2, and APISIX 3.18.0.

* key\_type

  string

  default: `var`

  valid value:

  `var`, `var_combination`, or `constant`

  ***

  The type of key.

  If the `key_type` is `var`, the `key` is interpreted as 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.

* key

  string

  default: `remote_addr`

  ***

  The key to count requests by.

  If the `key_type` is `var`, the `key` is interpreted as a variable. The variable does not need to be prefixed by a dollar sign (`$`). See [built-in variables](https://docs.api7.ai/apisix/reference/built-in-variables.md) for available variables.

  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.

  Outside `rules` mode, an unresolved `var` or `var_combination` key is counted by the client IP, `remote_addr`.

* rejected\_code

  integer

  default: `503`

  valid value:

  between 200 and 599 inclusive

  ***

  The HTTP status code returned when a request is rejected for exceeding the threshold.

* rejected\_msg

  string

  valid value:

  any non-empty string

  ***

  The response body returned when a request is rejected for exceeding the threshold.

* policy

  string

  default: `local`

  valid value:

  `local`, `redis`, `redis-cluster`, or `redis-sentinel`

  ***

  The policy for the rate limiting counter. Required for API7 Enterprise and optional for APISIX.

  When set to `local`, the counter is stored in memory locally. When set to `redis`, the counter is stored on a Redis instance. When set to `redis-cluster`, the counter is stored in a Redis cluster. When set to `redis-sentinel`, the counter is stored on Redis nodes managed by Redis Sentinel.

  The `redis-sentinel` value adds high availability through Sentinel-managed failover. Introduced in API7 Enterprise 3.9.14 and 3.10.1, and APISIX 3.18.0.

* window\_type

  string

  default: `fixed`

  valid value:

  `fixed` or `sliding`

  ***

  The rate limiting window algorithm. When set to `fixed`, a fixed window algorithm is used, where each time window enforces the quota independently. When set to `sliding`, a sliding window algorithm is used, which smooths out bursts at window boundaries by weighting the previous window when calculating the current count.

  Introduced in API7 Enterprise 3.9.14 and 3.10.1, and APISIX 3.18.0.

* sync\_interval

  number

  default: `-1`

  valid value:

  `-1` or greater than or equal to `0.1`; must be smaller than a numeric top-level `time_window`

  ***

  The interval in seconds at which the local counter is synchronized to the shared store (Redis). Only takes effect when `policy` is `redis`, `redis-cluster`, or `redis-sentinel`. A value of `-1` disables delayed synchronization, so each request synchronizes directly. When enabled, the value should not be smaller than `0.1` and should be smaller than `time_window`. Enabling delayed synchronization reduces the number of round trips to the shared store at the cost of slightly looser enforcement.

  At runtime, if the applicable `time_window` is less than or equal to `sync_interval`, the gateway falls back to direct synchronization for that request. This can occur with variable-resolved values or values inside `rules`, which are evaluated at request time.

  Introduced in API7 Enterprise 3.9.14 and 3.10.1, and APISIX 3.18.0.

* allow\_degradation

  boolean

  default: `false`

  ***

  If false, a counter-backend failure, invalid variable-resolved `count` or `time_window`, or an entirely skipped `rules` array returns `500 Internal Server Error`.

  If true, continue handling the request without rate limiting when one of these failures occurs.

* show\_limit\_quota\_header

  boolean

  default: `true`

  ***

  If true, include quota headers on the response.

  With the default names, `X-RateLimit-Limit` is the total quota, `X-RateLimit-Remaining` is how many requests remain in the window, and `X-RateLimit-Reset` is the algorithm-specific reset or delay value in seconds.

  You can rename those headers with plugin metadata. When `rules` is configured, each rule inserts its `header_prefix` (or the rule index if omitted) before `RateLimit-` so Limit, Remaining, and Reset stay distinct per rule. See `rules.header_prefix`.

* group

  string

  valid value:

  non-empty

  ***

  The `group` ID for the plugin, such that routes of the same `group` can share the same rate limiting counter.

* redis\_host

  string

  ***

  The address of the Redis node. Required when `policy` is `redis`.

* redis\_port

  integer

  default: `6379`

  valid value:

  greater than or equal to 1

  ***

  The port of the Redis node when `policy` is `redis`.

* redis\_username

  string

  ***

  The username for Redis if Redis ACL is used. If you use the legacy authentication method `requirepass`, configure only the `redis_password`. Used when `policy` is `redis` or `redis-sentinel`.

* redis\_password

  string

  ***

  The password of the Redis node when `policy` is `redis`, `redis-cluster`, or `redis-sentinel`. The password is encrypted at rest in API7 Enterprise. In APISIX, [enable data encryption](https://docs.api7.ai/apisix/production/security/data-encryption-with-keyring.md) to encrypt it before etcd storage. Encryption was introduced in API7 Enterprise 3.9.16 and 3.10.2, and APISIX 3.18.0.

* redis\_database

  integer

  default: `0`

  valid value:

  greater than or equal to 0

  ***

  The database number in Redis when `policy` is `redis` or `redis-sentinel`.

* redis\_ssl

  boolean

  default: `false`

  ***

  If true, use SSL to connect to Redis when `policy` is `redis`.

* redis\_ssl\_verify

  boolean

  default: `false`

  ***

  If true, verify the server SSL certificate when `policy` is `redis`.

* redis\_server\_name

  string

  ***

  TLS server name used for SNI and, when certificate verification is enabled, hostname verification. Applies when `policy` is `redis` and `redis_ssl` is `true`. Defaults to `redis_host`. If the selected value is an IP address, no server name is sent. To verify a DNS-named certificate when connecting by IP, set this field to the certificate's DNS name and enable `redis_ssl_verify`.

  Introduced in API7 Enterprise 3.9.21 and 3.10.8, and in APISIX 3.19.0. This field applies to the single-node Redis connector, not Redis Cluster or Sentinel.

* redis\_timeout

  integer

  default: `1000`

  valid value:

  greater than or equal to 1

  ***

  The Redis timeout value in milliseconds when `policy` is `redis` or `redis-cluster`.

* redis\_keepalive\_timeout

  integer

  valid value:

  greater than or equal to 1000 for `redis` and `redis-cluster`; greater than or equal to 1 for `redis-sentinel`

  ***

  Keepalive timeout in milliseconds for Redis connections. When `policy` is `redis` or `redis-cluster`, the default is `10000` and the minimum is `1000`. When `policy` is `redis-sentinel`, the default is `60000` and the minimum is `1`.

  Introduced in API7 Enterprise 3.9.16 and 3.10.3, and APISIX 3.15.0. Sentinel defaults were introduced in API7 Enterprise 3.9.14 and 3.10.1, and APISIX 3.18.0.

* redis\_keepalive\_pool

  integer

  default: `100`

  valid value:

  greater than or equal to 1

  ***

  Keepalive pool size for Redis when `policy` is `redis` or `redis-cluster`.

  Introduced in API7 Enterprise 3.9.16 and 3.10.3, and APISIX 3.15.0.

* redis\_cluster\_nodes

  array\[string]

  ***

  The list of Redis cluster nodes with at least one address. Required when `policy` is `redis-cluster`.

* redis\_cluster\_name

  string

  ***

  The name of the Redis cluster. Required when `policy` is `redis-cluster`.

* redis\_cluster\_ssl

  boolean

  default: `false`

  ***

  If true, use SSL to connect to Redis cluster when `policy` is `redis-cluster`.

* redis\_cluster\_ssl\_verify

  boolean

  default: `false`

  ***

  If true, verify the server SSL certificate when `policy` is `redis-cluster`.

* redis\_sentinels

  array\[object]

  ***

  The list of Redis Sentinel nodes, with at least one node. Required when `policy` is `redis-sentinel`. Each node is an object with `host` (string) and `port` (integer between 1 and 65535).

  Introduced in API7 Enterprise 3.9.14 and 3.10.1, and APISIX 3.18.0.

* redis\_master\_name

  string

  ***

  The name of the Redis master monitored by Sentinel. Required when `policy` is `redis-sentinel`.

  Introduced in API7 Enterprise 3.9.14 and 3.10.1, and APISIX 3.18.0.

* redis\_role

  string

  default: `master`

  valid value:

  `master` or `slave`

  ***

  The role of the Redis node to connect to when `policy` is `redis-sentinel`. Use `master` for read and write operations, or `slave` for read-only replicas.

  Introduced in API7 Enterprise 3.9.14 and 3.10.1, and APISIX 3.18.0.

* redis\_connect\_timeout

  integer

  default: `1000`

  valid value:

  greater than or equal to 1

  ***

  The connection timeout in milliseconds when `policy` is `redis-sentinel`.

  Introduced in API7 Enterprise 3.9.14 and 3.10.1, and APISIX 3.18.0.

* redis\_read\_timeout

  integer

  default: `1000`

  valid value:

  greater than or equal to 1

  ***

  The read timeout in milliseconds when `policy` is `redis-sentinel`.

  Introduced in API7 Enterprise 3.9.14 and 3.10.1, and APISIX 3.18.0.

* sentinel\_username

  string

  ***

  The username used to authenticate with the Redis Sentinel nodes when `policy` is `redis-sentinel`.

  Introduced in API7 Enterprise 3.9.14 and 3.10.1, and APISIX 3.18.0.

* sentinel\_password

  string

  ***

  The password used to authenticate with the Redis Sentinel nodes when `policy` is `redis-sentinel`.

  Introduced in API7 Enterprise 3.9.14 and 3.10.1, and APISIX 3.18.0.

  The password is encrypted at rest in API7 Enterprise. In APISIX, [enable data encryption](https://docs.api7.ai/apisix/production/security/data-encryption-with-keyring.md) to encrypt it before etcd storage. Encryption was introduced in API7 Enterprise 3.9.16 and 3.10.2, and APISIX 3.18.0.

* rules

  array\[object]

  ***

  An array of rate-limiting rules that are applied sequentially. Do not configure `rules` together with the top-level `count`, `time_window`, or `group` fields. The top-level `key` and `key_type` are not used in rules mode. Rule keys must be unique. A rule whose `key` variable is absent from a request is skipped.

  Introduced in API7 Enterprise 3.9.14 and 3.10.1, and APISIX 3.16.0.

  * count

    integer | string

    required

    valid value:

    greater than 0

    ***

    The maximum number of requests allowed within the given `time_window`.

    This parameter also supports the string data type and allows the use of [built-in variables](https://docs.api7.ai/apisix/reference/built-in-variables.md) prefixed with a dollar sign (`$`).

    A string value must resolve to a positive integer no greater than `9007199254740991`. If the rule applies and the value is invalid, the request returns `500 Internal Server Error` unless `allow_degradation` is `true`. Introduced in API7 Enterprise 3.9.16 and 3.10.2, and APISIX 3.18.0.

  * time\_window

    integer | string

    required

    valid value:

    greater than 0

    ***

    The time interval in seconds for the rate limiting `count`.

    This parameter also supports the string data type and allows the use of [built-in variables](https://docs.api7.ai/apisix/reference/built-in-variables.md) prefixed with a dollar sign (`$`).

    A string value must resolve to a positive integer no greater than `9007199254740991`. If the rule applies and the value is invalid, the request returns `500 Internal Server Error` unless `allow_degradation` is `true`. Introduced in API7 Enterprise 3.9.16 and 3.10.2, and APISIX 3.18.0.

  * key

    string

    required

    ***

    A variable expression that resolves to the key used to count requests for this rule. Prefix every APISIX [built-in variable](https://docs.api7.ai/apisix/reference/built-in-variables.md) or NGINX variable with a dollar sign (`$`), for example `$remote_addr` or `$remote_addr $http_x_tenant`.

    The gateway skips any rule whose key has no resolvable variable. The client IP fallback and `key_type` apply only to the top-level key.

    If every rule is skipped, the request returns `500 Internal Server Error` unless `allow_degradation` is `true`. The error log reads `failed to get rate limit rules: nil`. Check the rule keys for missing variables.

  * header\_prefix

    string

    ***

    A prefix inserted before `RateLimit-` in this rule's quota headers so each rule stays distinguishable. With the default names, `foo` produces `X-foo-RateLimit-Limit`, `X-foo-RateLimit-Remaining`, and `X-foo-RateLimit-Reset`. Those headers still mean total quota, remaining quota, and the algorithm-specific reset or delay value in seconds. If omitted, the rule's array index is used, so the first rule becomes `X-1-RateLimit-Limit`. Only sent when `show_limit_quota_header` is `true`.

## Plugin Metadata[​](#plugin-metadata "Direct link to Plugin Metadata")

* limit\_header

  string

  default: `X-RateLimit-Limit`

  ***

  Default response header name for the total rate limit quota.

* remaining\_header

  string

  default: `X-RateLimit-Remaining`

  ***

  Default response header name for the remaining rate limit quota.

* reset\_header

  string

  default: `X-RateLimit-Reset`

  ***

  Default response header name for the algorithm-specific reset or delay value in seconds.

## Related Variable[​](#related-variable "Direct link to Related Variable")

The plugin records how it evaluated each request in `$rate_limiting_info`, which can be added to the access log. See [Rate Limiting Information](https://docs.api7.ai/hub/limit-count-rate-limiting-info.md) for the Limit Count fields and the [API7 Gateway built-in variable reference](https://docs.api7.ai/api7-gateway/reference/built-in-variables.md#rate-limiting-information) for other rate-limiting plugins.
