Kuadrant Rate Limiting¶
A Kuadrant RateLimitPolicy custom resource, often abbreviated "RateLimitPolicy":
- Targets Gateway API networking resources such as HTTPRoutes, GRPCRoutes, and Gateways, using these resources to obtain additional context, i.e., which traffic workload (HTTP attributes, hostnames, user attributes, etc) to rate limit.
- Supports targeting subsets (sections) of a network resource to apply the limits to.
- Abstracts the details of the underlying Rate Limit protocol and configuration resources, that have a much broader remit and surface area.
- Enables cluster operators to set defaults that govern behavior at the lower levels of the network, until a more specific policy is applied.
How it works¶
Envoy's Rate Limit Service Protocol¶
Kuadrant's Rate Limit implementation relies on the Envoy's Rate Limit Service (RLS) protocol. The workflow per request goes:
- On incoming request, the gateway checks the matching rules for enforcing rate limits, as stated in the RateLimitPolicy custom resources and targeted Gateway API networking objects
- If the request matches, the gateway sends one RateLimitRequest to the external rate limiting service ("Limitador").
- The external rate limiting service responds with a RateLimitResponse back to the gateway with either an
OKorOVER_LIMITresponse code.
A RateLimitPolicy and its targeted Gateway API networking resource contain all the statements to configure both the ingress gateway and the external rate limiting service.
The RateLimitPolicy custom resource¶
Overview¶
The RateLimitPolicy spec includes, basically, two parts:
- A reference to an existing Gateway API resource (
spec.targetRef) - Limit definitions (
spec.limits)
Each limit definition includes:
- A set of rate limits (
spec.limits.<limit-name>.rates[]) - (Optional) A set of dynamic counter qualifiers (
spec.limits.<limit-name>.counters[]) - (Optional) A set of additional dynamic conditions to activate the limit (
spec.limits.<limit-name>.when[])
The limit definitions (limits) can be declared at the top-level level of the spec (with the semantics of defaults) or alternatively within explicit defaults or overrides blocks.
Check out Kuadrant RFC 0002 to learn more about the Well-known Attributes that can be used to define counter qualifiers (counters) and conditions (when). |
Check out the API reference for a full specification of the RateLimitPolicy CRD.
Using the RateLimitPolicy¶
Targeting a HTTPRoute or GRPCRoute networking resource¶
When a RateLimitPolicy targets a HTTPRoute or GRPCRoute, the policy is enforced to all traffic routed according to the rules and hostnames specified in the route, across all Gateways referenced in the spec.parentRefs field of the route.
Target a HTTPRoute (or GRPCRoute) by setting the spec.targetRef field of the RateLimitPolicy as follows:
apiVersion: kuadrant.io/v1
kind: RateLimitPolicy
metadata:
name: <RateLimitPolicy name>
spec:
targetRef:
group: gateway.networking.k8s.io
kind: HTTPRoute
name: <HTTPRoute Name>
limits: { … }

Hostnames and wildcards¶
If a RateLimitPolicy targets a route defined for *.com and another RateLimitPolicy targets another route for api.com, the Kuadrant control plane will not merge these two RateLimitPolicies. Unless one of the policies declare an overrides set of limites, the control plane will configure to mimic the behavior of gateway implementation by which the "most specific hostname wins", thus enforcing only the corresponding applicable policies and limit definitions.
E.g., by default, a request coming for api.com will be rate limited according to the rules from the RateLimitPolicy that targets the route for api.com; while a request for other.com will be rate limited with the rules from the RateLimitPolicy targeting the route for *.com.
See more examples in Overlapping Gateway and HTTPRoute RateLimitPolicies.
Targeting a Gateway networking resource¶
A RateLimitPolicy that targets a Gateway can declare a block of defaults (spec.defaults) or a block of overrides (spec.overrides). As a standard, gateway policies that do not specify neither defaults nor overrides, act as defaults.
When declaring defaults, a RateLimitPolicy which targets a Gateway will be enforced to all HTTP traffic hitting the gateway, unless a more specific RateLimitPolicy targeting a matching HTTPRoute or GRPCRoute exists. Any new route referencing the gateway as parent will be automatically covered by the default RateLimitPolicy, as well as changes in existing routes.
Defaults provide cluster operators with the ability to protect the infrastructure against unplanned and malicious network traffic attempt, such as by setting safe default limits on hostnames and hostname wildcards.
Inversely, a gateway policy that specify overrides declares a set of rules to be enforced on all routes attached to the gateway, thus atomically replacing any more specific policy occasionally attached to any of those routes.
Target a Gateway HTTPRoute by setting the spec.targetRef field of the RateLimitPolicy as follows:
apiVersion: kuadrant.io/v1
kind: RateLimitPolicy
metadata:
name: <RateLimitPolicy name>
spec:
targetRef:
group: gateway.networking.k8s.io
kind: Gateway
name: <Gateway Name>
defaults: # alternatively: `overrides`
limits: { … }

Overlapping Gateway and HTTPRoute RateLimitPolicies¶
Two possible semantics are to be considered here – gateway policy defaults vs gateway policy overrides.
Gateway RateLimitPolicies that declare defaults (or alternatively neither defaults nor overrides) protect all traffic routed through the gateway except where a more specific HTTPRoute or GRPCRoute RateLimitPolicy exists, in which case the route-level RateLimitPolicy prevails.
Example with 4 RateLimitPolicies, 3 HTTPRoutes and 1 Gateway default (plus 2 HTTPRoute and 2 Gateways without RateLimitPolicies attached):
- RateLimitPolicy A → HTTPRoute A (
a.toystore.com) → Gateway G (*.com) - RateLimitPolicy B → HTTPRoute B (
b.toystore.com) → Gateway G (*.com) - RateLimitPolicy W → HTTPRoute W (
*.toystore.com) → Gateway G (*.com) - RateLimitPolicy G (defaults) → Gateway G (
*.com)
Expected behavior:
- Request to
a.toystore.com→ RateLimitPolicy A will be enforced - Request to
b.toystore.com→ RateLimitPolicy B will be enforced - Request to
other.toystore.com→ RateLimitPolicy W will be enforced - Request to
other.com(suppose a route exists) → RateLimitPolicy G will be enforced - Request to
yet-another.net(suppose a route and gateway exist) → No RateLimitPolicy will be enforced
Gateway RateLimitPolicies that declare overrides protect all traffic routed through the gateway, regardless of existence of any more specific HTTPRoute RateLimitPolicy.
Example with 4 RateLimitPolicies, 3 HTTPRoutes and 1 Gateway override (plus 2 HTTPRoute and 2 Gateways without RateLimitPolicies attached):
- RateLimitPolicy A → HTTPRoute A (
a.toystore.com) → Gateway G (*.com) - RateLimitPolicy B → HTTPRoute B (
b.toystore.com) → Gateway G (*.com) - RateLimitPolicy W → HTTPRoute W (
*.toystore.com) → Gateway G (*.com) - RateLimitPolicy G (overrides) → Gateway G (
*.com)
Expected behavior:
- Request to
a.toystore.com→ RateLimitPolicy G will be enforced - Request to
b.toystore.com→ RateLimitPolicy G will be enforced - Request to
other.toystore.com→ RateLimitPolicy G will be enforced - Request to
other.com(suppose a route exists) → RateLimitPolicy G will be enforced - Request to
yet-another.net(suppose a route and gateway exist) → No RateLimitPolicy will be enforced
Limit definition¶
A limit will be activated whenever a request comes in and the request matches:
- all of the
whenconditions specified in the limit.
A limit can define:
- counters that are qualified based on dynamic values fetched from the request, or
- global counters (implicitly, when no qualified counter is specified)
A limit is composed of one or more rate limits.
E.g.
spec:
limits:
"toystore-all":
rates:
- limit: 5000
window: 1s
"toystore-api-per-username":
rates:
- limit: 100
window: 1s
- limit: 1000
window: 1m
counters:
- expression: auth.identity.username
when:
- predicate: request.host == 'api.toystore.com'
"toystore-admin-unverified-users":
rates:
- limit: 250
window: 1s
when:
- predicate: request.host == 'admin.toystore.com'
- predicate: !auth.identity.email_verified
| Request to | Rate limits enforced |
|---|---|
api.toystore.com |
100rps/username or 1000rpm/username (whatever happens first) |
admin.toystore.com |
250rps |
other.toystore.com |
5000rps |
when conditions¶
when conditions can be used to scope a limit (i.e. to filter the traffic to which a limit definition applies) without any coupling to the underlying network topology, i.e. without making direct references to HTTPRouteRules.
Use when conditions to conditionally activate limits based on attributes that cannot be expressed in the HTTPRoutes' spec.hostnames and spec.rules.matches fields, or in general in RateLimitPolicies that target a Gateway.
The selectors within the when conditions of a RateLimitPolicy are a subset of Kuadrant's Well-known Attributes (RFC 0002). Check out the reference for the full list of supported selectors.
Examples¶
Check out the following user guides for examples of rate limiting services with Kuadrant:
- Simple Rate Limiting for Applications
- Authenticated Rate Limiting for Application
- Gateway Rate Limiting for Cluster Operators
- Authenticated Rate Limiting with JWTs and Kubernetes RBAC
- Authenticated Rate Limiting for gRPC Services
Known limitations¶
- RateLimitPolicies can only target HTTPRoutes/GRPCRoutes/Gateways defined within the same namespace of the RateLimitPolicy.
- Mixed HTTPRoute and GRPCRoute sharing the same hostname on a Gateway is not supported. Gateway implementations enforce hostname uniqueness across route types — use separate hostnames for HTTP and gRPC traffic.
Implementation details¶
Driven by limitations related to how Istio injects configuration in the filter chains of the ingress gateways, Kuadrant relies on Envoy's Wasm Network filter in the data plane, to manage the integration with rate limiting service ("Limitador"), instead of the Rate Limit filter.
Motivation: Multiple rate limit domains
The first limitation comes from having only one filter chain per listener. This often leads to one single global rate limiting filter configuration per gateway, and therefore to a shared rate limit domain across applications and policies. Even though, in a rate limit filter, the triggering of rate limit calls, via actions to build so-called "descriptors", can be defined at the level of the virtual host and/or specific route rule, the overall rate limit configuration is only one, i.e., always the same rate limit domain for all calls to Limitador.
On the other hand, the possibility to configure and invoke the rate limit service for multiple domains depending on the context allows to isolate groups of policy rules, as well as to optimize performance in the rate limit service, which can rely on the domain for indexation.
Motivation: Fine-grained matching rules A second limitation of configuring the rate limit filter via Istio, particularly from Gateway API resources, is that rate limit descriptors at the level of a specific HTTP route rule require "named routes" – defined only in an Istio VirtualService resource and referred in an EnvoyFilter one. Because Gateway API HTTPRoute rules lack a "name" property1, as well as the Istio VirtualService resources are only ephemeral data structures handled by Istio in-memory in its implementation of gateway configuration for Gateway API, where the names of individual route rules are auto-generated and not referable by users in a policy23, rate limiting by attributes of the HTTP request (e.g., path, method, headers, etc) would be very limited while depending only on Envoy's Rate Limit filter.
Motivated by the desire to support multiple rate limit domains per ingress gateway, as well as fine-grained HTTP route matching rules for rate limiting, Kuadrant implements a wasm-shim that handles the rules to invoke the rate limiting service, complying with Envoy's Rate Limit Service (RLS) protocol.
The wasm module integrates with the gateway in the data plane via Wasm Network filter, and parses a configuration composed out of user-defined RateLimitPolicy resources by the Kuadrant control plane. Whereas the rate limiting service ("Limitador") remains an implementation of Envoy's RLS protocol, capable of being integrated directly via Rate Limit extension or by Kuadrant, via wasm module for the Istio Gateway API implementation.
As a consequence of this design:
- Users can define fine-grained rate limit rules that match their Gateway and HTTPRoute definitions including for subsections of these.
- Rate limit definitions are insulated, not leaking across unrelated policies or applications.
- Conditions to activate limits are evaluated in the context of the gateway process, reducing the gRPC calls to the external rate limiting service only to the cases where rate limit counters are known in advance to have to be checked/incremented.
- The rate limiting service can rely on the indexation to look up for groups of limit definitions and counters.
- Components remain compliant with industry protocols and flexible for different integration options.
The Kuadrant control plane generates the wasm-shim configuration automatically from the RateLimitPolicy custom resources and embeds it in the data-plane resource for the gateway provider in use: an EnvoyFilter for Istio, or an EnvoyExtensionPolicy for Envoy Gateway. For a RateLimitPolicy targeting a HTTPRoute with two route rules (/assets and /v1, each with its own limit), the configuration looks like the following (shown decoded and abridged for readability):
services:
ratelimit-service:
endpoint: kuadrant-ratelimit-service
type: dynamic
failureMode: allow
timeout: 100ms
grpcService: envoy.service.ratelimit.v3.RateLimitService
grpcMethod: ShouldRateLimit
# auth-service and ratelimit-{check,commit,report,reserve}-service also appear
# in the service catalog; omitted here for brevity. A second, near-identical
# actionSet for the "/v1" rule (routeRuleConditions match "/v1") is also omitted.
actionSets:
- name: 8d30e0020e3086fa8eeee5d5b98d4f6e00369ee228879ddda6eaf051ad218293
routeRuleConditions:
hostnames:
- "*.toystore.com"
predicates:
- request.url_path.startsWith("/assets")
actions:
- type: grpc
predicate: request.path.startsWith('/assets') || request.path.startsWith('/v1')
terminal: false
isGuard: true
var: ratelimit_response
service: ratelimit-service
label: ratelimit
messageBuilder: |-
envoy.service.ratelimit.v3.RateLimitRequest {
domain: "default/toystore",
hits_addend: 1u,
descriptors: [envoy.extensions.common.ratelimit.v3.RateLimitDescriptor { entries: ((request.path.startsWith('/assets')) ? [envoy.extensions.common.ratelimit.v3.RateLimitDescriptor.Entry { key: "limit.assets__fa4bb078", value: string(1) }] : []) + ((request.path.startsWith('/v1')) ? [envoy.extensions.common.ratelimit.v3.RateLimitDescriptor.Entry { key: "limit.v1__1064421f", value: string(1) }] : []) }]
}
onReply:
- type: deny
predicate: ratelimit_response.overall_code == 2
terminal: true
isGuard: true
denyWith: 'DenyResponse{status: 429u, headers: ratelimit_response.response_headers_to_add, body: "Too Many Requests\n"}'
- type: headers
predicate: ratelimit_response.overall_code == 1
terminal: false
isGuard: true
target: response
headers: ratelimit_response.response_headers_to_add
- type: fail
predicate: ratelimit_response.overall_code != 1 && ratelimit_response.overall_code != 2
terminal: true
isGuard: true
logMessage: Unknown rate limit response code from ratelimit_response
sources:
- ratelimitpolicy.kuadrant.io:default/toystore
descriptorService: kuadrant-operator-grpc
Note: The configuration above is shown decoded for readability. In the resource actually generated by the control plane, the wasm-shim configuration is gzip-compressed and base64-encoded, wrapped in a self-describing envelope, to keep large configurations within the Kubernetes resource size limit:
maxDecompressedBytestells the wasm-shim how much the config is allowed to decompress to (a decompression-bomb guard). It defaults to 32 MiB and is tunable with theWASM_MAX_DECODED_CONFIG_SIZEoperator environment variable, whose value is a Kubernetes resource quantity. For example:# operator Deployment env WASM_MAX_DECODED_CONFIG_SIZE=128Mi # or 64Mi, 32Mi (default), 60M, 33554432Newly generated envelopes advertise the operator's current cap in
maxDecompressedBytes, and the wasm-shim enforces that advertised value on the data plane. When the operator reads an existing resource to reconcile, it bounds decompression with its own current cap rather than the value stored in the envelope; if the cap has changed, the operator rewrites the resource so the advertised value catches up. To recover the human-readable configuration, decode thedatafield, e.g.echo '<data value>' | base64 -d | gunzip.