Skip to content

Rate Limiting ​

For policy authors and operators protecting APIs from overuse. Rate limit rules live in the policy documents and run only after a request is permitted; this page covers writing rules, choosing a counter backend, and what clients see when a limit is hit.

A policy decides whether a request is allowed; a rate limit rule decides how often permitted requests may pass. The rules are obligations of a permit: a request that is denied or cannot be decided spends nothing, so it never uses up the quota of legitimate callers. Checks in code, simulate, explain and shadow evaluations spend nothing either.

Rules ​

Every document can hold rules in rateLimits (fields):

json
{
  "schema": 2,
  "revision": 7,
  "publishedAt": "2026-11-01T08:00:00Z",
  "policies": [],
  "bindings": [],
  "rateLimits": [
    {
      "id": "login-per-ip",
      "action": "http:POST",
      "resource": "http:path:/auth/login",
      "key": "environment.ip",
      "permitsPerSecond": 0.1,
      "windowSeconds": 60,
      "strictFailure": true
    },
    {
      "id": "export-free-tier",
      "action": "http:GET",
      "resource": "http:path:/export/**",
      "condition": { "in": ["free", "scope.roles"] },
      "key": "subject.id",
      "permitsPerSecond": 0.2,
      "windowSeconds": 60
    }
  ]
}

A rule matches a permitted request the way a DENY statement would: one of its action patterns, one of its resource patterns, and its condition. A window allows permitsPerSecond × windowSeconds requests, and at least one. A route in a service's route map is not an http request, so a rule on http: and a path does not limit it: write the rule for the route's action too (Authorizing business actions).

Which rules a request is held to:

  • the platform document's rules, for every permitted request;
  • the app document's rules, when the request read that document: it has an app, and the principal holds a role in the request's space or the tenant's document binds one of the app's permission policies to Anyone or to a principal (Role permissions);
  • the tenant document's rules, for requests of that tenant.

They run in that order, platform first, then app, then tenant, and in document order within a document. Each matching rule spends one permit. The first rule whose bucket is exhausted answers 429; the rules after it spend nothing, and the rules before it keep what they spent. Because the platform's rules run first, a platform rule bounds a request before any app or tenant rule spends anything. Rules add up: a tenant or app rule never replaces or relaxes a platform rule, even one with the same id, so a request passes only while every rule it matches has permits left.

Every rule a permitted request matches costs one call to the limiter, made one after another: with the Redis limiter, one round trip each. So a document holds at most 64 rules (DOC-6): a document with more does not compile, and the control plane answers a change that would make one with a COMPILE finding, 422. Keep the rules a request matches few; one rule with a key replaces many rules that differ only in what they count.

Keys ​

key names the attribute whose value picks the bucket: a String or Ip attribute of the catalog, such as environment.ip, subject.id, scope.tenant, subject.attributes.plan or resource.var.orderId. A resource.var.<name> key must be captured by every resource pattern of the rule; when several match, the value is the first pattern's, in written order, under whose captures the rule's condition holds (path variables).

keyBuckets
absentOne bucket shared by every request the rule matches.
presentOne bucket per value. Requests where the value is absent share one bucket of their own.

A value is its bucket as it is, k:<value>, which every limiter writes as UTF-8. A value with an unpaired UTF-16 surrogate (a JSON escape such as "\uD800" in a token attribute) has no UTF-8 encoding: its bucket is e:<value> with each unpaired surrogate as its \uXXXX escape and each \ doubled, so it never counts in the bucket of the value with ? in its place (IDN-10). A value that, so written, is longer than 256 characters (the longest path segment) is named by its hash instead: its bucket is h: and the SHA-256 of k:<value> or e:<value>, in hex, 66 characters whatever the value's length. The same value is always the same bucket, and two values are two buckets however much of them they share; a client that chooses the value (a header the key reads) chooses how many buckets it makes, never how long each one is, in the local limiter's memory or in Redis. A limiter of your own gets only buckets that are well-formed text of at most 258 characters.

Rules of different documents never share buckets: a rule counts under its document and its id, platform/<id>, app:<appId>/<id> or tenant:<tenantId>/<id> (CompiledRateLimit.counterId). A rule id cannot contain : or /, so no rule can name another document's counter: a tenant cannot spend, or reset, the platform's quotas.

A key that reads subject.id gives anonymous callers one shared bucket; key them by environment.ip in a separate rule. The login, refresh and logout routes are anonymous (a refresh carries the access token that expired, PIP-6), and each refresh verifies a signature and reads the session store: hold them to an environment.ip rule, as the clients example does. The routes the starter serves have no limiter of their own. A key or condition that reads an external attribute which cannot be fetched leaves the rule without a bucket: with onMissing: "nomatch" (platform rules only) it is skipped, otherwise strictFailure decides as for a failing limiter.

Counting and backends ​

Both limiters count with the same sliding window: the estimate is the previous window's count, weighted by how much of it still overlaps, plus the current window's count. A request is allowed while the estimate is below the limit; refused requests are not counted. A bucket's window never moves back: the local limiter reads its clock under the bucket's lock, and a time behind the bucket's window (a clock set back) counts in that window, never clearing it (OBL-5); the Redis limiter takes the time from Redis (OBL-4).

LimiterUsed whenScope
ReactiveRedisRateLimitercosec-cocache is on the classpath and a ReactiveStringRedisTemplate bean existsCluster-wide, checked and counted atomically by a Lua script, without blocking the caller's thread. Each bucket is one key, <cosec.key-prefix>:rate-limiter:<rule>:<bucket>, which holds its current and previous window and expires two windows after its last count. The script takes the time from Redis, so instances whose clocks differ count in the same window, and it touches only that key, so it runs on a Redis Cluster as on one node.
LocalRateLimiterotherwise, and for a pipeline built with Pipeline.builder or the test kit's PipelineFixture unless given anotherPer JVM: with N instances the effective limit is N times higher. A bucket is removed once it has been idle for an hour and for two of its rule's windows, when its count can no longer refuse a request. At most 100,000 buckets in all, 50,000 of one rule and 25,000 of one document's rules (below).

The counters are memory a client can grow. They live in the same Redis as the documents, which must not evict (Storage): one key per bucket value, for two windows. A rule whose key reads something the client chooses, a header above all, with a long window, lets a client create keys as fast as it sends requests, and keep them for two windows; a full Redis then refuses every write, publishing and revocations included. That is why a window is at most a day. Key a rule on what a client cannot multiply (the principal, the address a trusted proxy reports) and keep windows short; to keep the counters away from the documents altogether, give the limiter a Redis of its own: declare a RateLimiter bean, ReactiveRedisRateLimiter(ReactiveStringRedisTemplate(<a connection factory of its own>), "<prefix>:rate-limiter"), and the starter's is not created.

The local limiter is bounded ​

LocalRateLimiter keeps its counters in the instance's memory, at most 100,000 buckets in all, 50,000 of one rule (OBL-6) and 25,000 of the rules of one document (the platform's, an app's or a tenant's). It never removes a bucket to make room: the count is the state the rule exists to keep, and dropping it would give its key fresh permits (OBL-5). So a full limiter cannot count a new bucket, and the rule's strictFailure decides as for any failing limiter: a strict rule answers 503, a lenient one is skipped. The log line Rate limiter unavailable: rule [<id>] ... then carries The local rate limiter holds its maximum of buckets (rule [<id>]: <n> of <max>, its document: <n> of <max>, all: <n> of <max>)., and cosec.ratelimit.local.refused counts each bucket refused, by the kind of document whose rule asked (Observability). Buckets that already exist keep counting. A bucket holds at most about 1 KB, however long the key a client sent, since its name is at most 258 characters (Keys): one document's share at most about 25 MB, the whole limiter at most about 100 MB.

A rule whose key reads something a client can vary fills only its own share, and the rules of one tenant or one app only their document's: two such rules of one tenant hold at most a quarter of the buckets, and the other documents' rules keep admitting new buckets. It takes four documents whose rules' keys clients vary to fill the limiter. Such a rule limits nothing, though: each new value is a fresh bucket, and once its share is full every new value is skipped (lenient) or refused (strict). Key local rules on what a client cannot multiply, as for Redis. Other bounds take a RateLimiter bean of your own, LocalRateLimiter(maximumBuckets = ..., maximumBucketsPerRule = ..., maximumBucketsPerOwner = ...); the per-rule bound defaults to half the total, the per-document bound to a quarter.

Your own RateLimiter bean replaces both (Extending). Each call has the rate limit timeout (10 % of cosec.pipeline.timeout, 500ms at the default, Configuration); a call that takes longer counts as a failure.

Responses and headers ​

A request stopped by a rule is answered 429 Too Many Requests with the too-many-requests problem (status codes). A permitted request that spent at least one permit carries the headers of the rule with the fewest permits left; a 429 carries those of the exhausted rule:

HeaderValue
RateLimit-LimitPermits per window of that rule, rounded up.
RateLimit-RemainingHow many more requests its current window would allow right now, rounded up: a part of a permit left still admits one.
RateLimit-ResetSeconds until its current window ends, rounded up to a whole second, at least 1: a client that waits that long is not refused again by the same window.
Retry-AfterOnly on 429: the same as RateLimit-Reset.

A request that matched no rule, or whose rules were all skipped, has none of these headers.

Failure behavior ​

A rule cannot be counted when the limiter fails (Redis unreachable, a script error, a full local limiter), times out, is not configured, or the rule's bucket cannot be read. Its strictFailure decides:

strictFailureThe request
false (default)The rule is skipped; the next rule runs. Logged at WARN.
trueRefused with 503 Service Unavailable (unavailable problem, no Retry-After). Logged at ERROR.

A bucket that cannot be read for any reason other than a missing external attribute refuses the request even without strictFailure. Failure logs are throttled to one per ten seconds. Skipping a lenient rule is one of the documented exceptions to fail closed; set strictFailure on rules that protect against abuse, such as login attempts, and leave it off on rules that only smooth load.

The control API ​

The control API is served behind the pipeline, so a platform rule limits it like any other path, and keyed by scope.tenant it gives each tenant a bucket of its own. Cover the routes that compile, evaluate or check: simulate, explain, analyze and changes, under cosec.control.http.base-path and its /v1:

json
{
  "id": "control-per-tenant",
  "action": "http:POST",
  "resource": [
    "http:path:/cosec/control/v1/simulate",
    "http:path:/cosec/control/v1/explain",
    "http:path:/cosec/control/v1/analyze",
    "http:path:/cosec/control/v1/changes",
    "http:path:/cosec/control/v1/changes/*"
  ],
  "key": "scope.tenant",
  "permitsPerSecond": 0.5,
  "windowSeconds": 60
}
  • The rule belongs in the platform document: a tenant's own rules count in their own buckets and never relax it.
  • The bucket is the caller's tenant, counted after the pipeline permits the request and before the control plane authorizes it: every principal of the tenant that the pipeline lets onto these paths spends it, administrator or not, and every (platform) principal spends the platform's. Permit the control paths in the pipeline only to principals bound to a control role, so that no other principal can hold off a tenant's administrators.
  • An exhausted bucket answers 429 before the control API does any work. The control API serves only these exact paths: one with a trailing / is a 404, and the pipeline refuses one with an empty segment (//) with 400, so the patterns cover every request it serves.
  • Leave the break-glass routes (/break-glass and below) out of it: give them a rule of their own, or none, so that a spent bucket never holds off an emergency revoke. changes/* still covers three steps of break-glass: the :publish that makes an approved break-glass grant take effect, the :publish that recovers a stalled revoke, and :abandon of a break-glass change. Give the platform's bucket room for them, or keep the platform's principals out of the rule with "condition": {"not": {"eq": ["scope.tenant", "(platform)"]}}.
  • documents/{key}:apply and :rollback propose changes too, and can join the rule.

No such rule is published for you, by cosec-migrate bootstrap or otherwise: the limits depend on the deployment's size.

Publishing rules ​

Rules are part of their documents, so they are checked and compiled with them: an invalid id, a duplicate id in one document, more than 64 rules in one document, permitsPerSecond not above 0, windowSeconds below 1 or above a day (86400), a quota permitsPerSecond × windowSeconds above 2⁶³, a key that is not a String or Ip attribute, or a variable a pattern does not capture fails the whole document (Testing policies).

Change rules through the control plane like any other entry (Changes):

  • putRateLimit (rateLimit, optional ifMatch) adds a rule, or replaces the one with its id where it stands; removeRateLimit (id, optional ifMatch) removes one;
  • POST /documents/{key}:apply proposes them from a whole document: MERGE puts new and changed rules, REPLACE also removes the rules the document leaves out. Order is part of the rules (the first exhausted rule answers), so a rule out of place is removed and put again; a rollback restores the rules of its revision the same way.

Each operation needs cosec:write on the entry doc/<document>/rateLimit/<id>, such as doc/tenant/acme/rateLimit/export-per-user. A tenant's rule changes, tightening or loosening, need no second person: its rules count in its own buckets, and every platform and app rule still applies to its requests. A rule change to the platform or app document, removals included, needs approval as any other change there does.

A shadow window compares decisions, and a rule never changes a decision, so a change that touches only rate limit rules shows no difference in its window: the window tells you nothing about the quota.

Next: catch mistakes before they reach production, in Testing policies.

Licensed under the Apache License, Version 2.0.