Rate Limit
Limits how many tiles can be requested in a period.
Description
More details on how policies and rules are evaluated and what users see can be found in Rate Limiting.
Like other entities, the rate limit configuration uses a parameter called name to dictate the store that holds the counters, but the policies and options below are shared by every store. Rate limiting is disabled by default.
Layers pick a policy with their own ratelimit parameter, or opt out with skipratelimit. See Layer. Layers that don’t pick one use the policy named by the top-level defaultratelimit, or the first policy if that isn’t set.
Choosing a store
| Goal | Store | Notes |
|---|---|---|
Develop or run a single instance |
Not shared between instances. Counts are lost on restart |
|
Share limits between instances |
Recommended for production. Needs a matching datastore |
|
Keep long quotas in a database you already back up |
Updates a row per rule on every request, so only suits moderate traffic |
|
Temporarily turn rate limiting off |
Policies are still validated but never enforced |
Example
-
YAML
-
JSON
ratelimit:
name: redis
datastore: redis-0
headers: standard
onError: allow
policies:
- id: standard
rules:
- segment:
- user
limit: 50
period: 1
- id: premium
extend:
- standard
rules:
- segment:
- tenant
limit: 100000
calendar: month
overrides:
- tenants:
- acme
limit: 5000000
defaultratelimit: standard
{
"ratelimit": {
"name": "redis",
"datastore": "redis-0",
"headers": "standard",
"onError": "allow",
"policies": [
{
"id": "standard",
"rules": [
{
"segment": [
"user"
],
"limit": 50,
"period": 1
}
]
},
{
"id": "premium",
"extend": [
"standard"
],
"rules": [
{
"segment": [
"tenant"
],
"limit": 100000,
"calendar": "month",
"overrides": [
{
"tenants": [
"acme"
],
"limit": 5000000
}
]
}
]
}
]
},
"defaultratelimit": "standard"
}
Configuration
| Parameter | Description | Type | Required | Default |
|---|---|---|---|---|
|
The sets of limits layers can use. See Policies |
Policy[] |
Yes, unless |
None |
|
Which headers describe the limits on tile responses. One of |
string |
No |
standard |
|
How long (in milliseconds) to wait for the store before treating the check as failed |
uint |
No |
100 |
|
What to do when the store fails or times out. |
string |
No |
allow |
|
How many leading bits of an IPv6 address identify a client when segmenting by |
uint |
No |
64 |
defaultratelimit sits at the top level of the configuration, beside ratelimit, and holds the ID of the policy used by layers that don’t pick one. It defaults to the first policy.
Policies
| Parameter | Description | Type | Required | Default |
|---|---|---|---|---|
|
A unique identifier for the policy, used by layers, |
string |
Yes |
None |
|
The limits this policy enforces. See Rules. No two rules in a policy may have the same segment and period |
Rule[] |
No |
None |
|
The IDs of other policies whose rules this policy also enforces. Their counters are shared, not copied. Loops are rejected |
string[] |
No |
None |
Rules
| Parameter | Description | Type | Required | Default |
|---|---|---|---|---|
|
How many requests are allowed per period. Must be at least 1 |
uint |
Yes |
None |
|
The length of a rolling window, in seconds. Mutually exclusive with Calendar |
uint |
No |
None |
|
Reset at the start of each calendar period in UTC. One of |
string |
No |
None |
|
What gets its own counter. Any of |
string[] |
Yes |
None |
|
Different limits for specific requests. The first match wins. See Overrides |
Override[] |
No |
None |
One of Period or Calendar is required.
Overrides
An override matches a request if any of its conditions match.
| Parameter | Description | Type | Required | Default |
|---|---|---|---|---|
|
User IDs to match |
string[] |
No |
None |
|
Tenant IDs to match |
string[] |
No |
None |
|
Client IP addresses or CIDR ranges to match, such as |
string[] |
No |
None |
|
If true, match requests without a user or tenant ID for the values in the rule’s |
bool |
No |
false |
|
The limit to use instead of the rule’s. 0 rejects every request and -1 removes the limit. Must be -1 or greater |
int |
Yes |
None |
At least one of Users, Tenants, IPs, or Anonymous is required.