# Limit Count Advanced Configuration

The `limit-count-advanced` plugin enforces API rate limiting with a fixed window or sliding window algorithm, restricting requests within a time window. Requests over the quota are rejected.<!-- --> <!-- -->Configuration covers plugin parameters.

## 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).

note

In API7 Enterprise (from 3.8.17), you should configure one of the following parameter sets, but not both:

* `count`, `time_window`
* `rules`

- count

  integer | string

  valid value:

  greater than 0

  ***

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

  In API7 Enterprise (from 3.8.17), this parameter also supports the string data type and allows the use of [built-in variables](https://docs.api7.ai/api7-gateway/reference/built-in-variables.md) prefixed with a dollar sign (`$`).

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

- time\_window

  integer | string

  valid value:

  greater than 0

  ***

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

  In API7 Enterprise (from 3.8.17), this parameter also supports the string data type and allows the use of [built-in variables](https://docs.api7.ai/api7-gateway/reference/built-in-variables.md) prefixed with a dollar sign (`$`).

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

- window\_type

  string

  default: `fixed`

  valid value:

  `fixed` or `sliding`

  ***

  Rate limiting algorithm, fixed window or sliding window.

- 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/api7-gateway/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.

- 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 rate limiting counter.

  Set to `local` to store the counter in memory locally.

  Set to `redis` to store the counter on a Redis instance.

  Set to `redis-cluster` to store the counter in a Redis cluster.

  Set to `redis-sentinel` to store the counter on the Redis primary node managed by Redis Sentinel, which ensures high availability by automatically promoting a replica to primary in case of failure. Redis Sentinel provides high availability for Redis when not using Redis Cluster.

- redis\_sentinels

  array\[object]

  ***

  An array of Redis Sentinel nodes (host and port). Required when `policy` is `redis-sentinel`.

- redis\_master\_name

  string

  ***

  The name of the Redis master group that Sentinels are monitoring. Required when `policy` is `redis-sentinel`.

- redis\_role

  string

  default: `master`

  valid value:

  `master` or `slave`

  ***

  The Redis node role to connect to. Configurable when `policy` is `redis-sentinel`. Set to `master` to connect to the current Redis master, and set to `slave` to connect to a Redis replica.

- redis\_connect\_timeout

  integer

  default: `1000`

  valid value:

  greater than or equal to 1

  ***

  Timeout in milliseconds for establishing a connection to a Redis node. Configurable when `policy` is `redis-sentinel`.

- redis\_read\_timeout

  integer

  default: `1000`

  valid value:

  greater than or equal to 1

  ***

  Timeout in milliseconds for reading data from a Redis node. Configurable when `policy` is `redis-sentinel`.

- sentinel\_username

  string

  ***

  Username used to authenticate with the Redis Sentinel instance. Configurable when `policy` is `redis-sentinel`.

- sentinel\_password

  string

  ***

  Password used to authenticate with the Redis Sentinel instance. Configurable when `policy` is `redis-sentinel`.

- allow\_degradation

  boolean

  default: `false`

  ***

  If true, allow the gateway to continue handling requests without the plugin when the plugin or its dependencies become unavailable.

- rules

  array\[object]

  ***

  An array of rate-limiting rules that are applied sequentially.

  Available in API7 Enterprise from 3.8.17.

  * count

    integer | string

    required

    valid value:

    greater than 0

    ***

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

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

  * time\_window

    integer | string

    required

    valid value:

    greater than 0

    ***

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

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

  * key

    string

    required

    ***

    A variable expression that resolves to the key used to count requests for this rule. Prefix each variable with `$`; both `$variable` and `${variable}` forms are accepted. For example, use `${http_custom_a}` or `${http_custom_a}_${http_custom_b}`.

    The top-level `key_type` does not apply to rules. A rule applies when at least one variable reference resolves to a request value or its `??` default. A missing variable without a default contributes an empty string but does not count as resolved, so a partial expression can still apply when another variable resolves. An expression with no resolved variables, including a value with no variable references, is skipped.

    If `rules` is configured and no rule applies to a request, the gateway returns HTTP 500 by default. Setting `allow_degradation` to `true` lets the request continue without rate limiting.

    In an ADC configuration, escape a gateway expression with a simple name, such as `${http_user}`, as `${http_user}`. ADC treats an unescaped `${NAME}` containing only word characters as load-time environment interpolation; dotted gateway variable names pass through unchanged.

  * header\_prefix

    string

    ***

    Prefix for all rate limiting response headers. Available in API7 Enterprise from version 3.8.19.

    When configured, the prefix is inserted after `X-` in the header name. For example, with `header_prefix` set to `test`, the headers become `X-Test-RateLimit-Limit`, `X-Test-RateLimit-Remaining`, and `X-Test-RateLimit-Reset`.

    When not configured, the index of the rule in the rules array is used as the prefix. For example, headers for the first rule will be `X-1-RateLimit-Limit`, `X-1-RateLimit-Remaining`, and `X-1-RateLimit-Reset`.

- show\_limit\_quota\_header

  boolean

  default: `true`

  ***

  If true, includes the rate limiting response headers. Specifically, if `rules` is not set, the headers are:

  * `X-RateLimit-Limit` shows the total quota.
  * `X-RateLimit-Remaining` shows the remaining quota.
  * `X-RateLimit-Reset` shows the algorithm-specific reset or delay value in seconds.

  <br />When `rules` is set, a prefix (followed by a hyphen) is inserted after `X-`. See `rules.header_prefix` for details.

- 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`.

- redis\_password

  string

  ***

  The password of the Redis node when `policy` is `redis`, or `redis-cluster`.

- 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

  ***

  The server name (SNI) sent in the TLS handshake with Redis when `policy` is `redis` and `redis_ssl` is `true`. If unset, `redis_host` is sent, unless it is an IP address. Introduced in API7 Enterprise 3.9.21 and 3.10.8.

- 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`

  ***

  Time in milliseconds that an idle Redis connection is kept alive in the connection pool before being closed. When `policy` is `redis` or `redis-cluster`, the default is `10000`. When `policy` is `redis-sentinel`, the default is `60000`.

  For `redis` and `redis-cluster`, this parameter was introduced in API7 Enterprise 3.9.16 and 3.10.3.

- redis\_keepalive\_pool

  integer

  default: `100`

  valid value:

  greater than or equal to 1

  ***

  Maximum number of idle Redis connections in the keepalive pool. Used when `policy` is `redis` or `redis-cluster`.

  Introduced in API7 Enterprise 3.9.16 and 3.10.3.

- redis\_cluster\_nodes

  array\[string]

  ***

  The list of Redis cluster nodes with at least two addresses. 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`.

- sync\_interval

  number

  default: `-1`

  valid value:

  greater than or equal to 0.1, or the default -1

  ***

  The frequency of synchronizing counter data to Redis. Available only in Enterprise.

  The `sync_interval` value should be smaller than `time_window`. A value of `1` results in synchronizing counter data every second. A value of `-1` yields no change in synchronizing behaviour, i.e. counter data will be synchronized for each request.
