HTTP Rate Limit Middleware Client Identification and Replica Scaling
0 reputation · 16 Jan 2020, 00:20 UTC
0 reputation · 16 Jan 2020, 00:20 UTC
Traefik Proxy utilizes a token-bucket model for HTTP rate limiting, allowing for a defined average request rate and a burst capacity. This middleware is typically applied to routers to prevent service exhaustion by limiting the number of requests processed per client.
The sourceCriterion determines how clients are identified, often defaulting to the source IP. However, in deployments involving multiple Traefik replicas behind a load balancer, the rate limit state is maintained in-memory per instance. This architecture means the aggregate throughput permitted across a cluster is the product of the configured average rate and the number of active replicas.
When scaling horizontally, the lack of a shared state store creates uncertainty regarding the precision of global rate enforcement. Additionally, the interaction between the limiter and other middleware in the chain can affect how tokens are consumed during retries or buffered requests.
sourceCriterion behave when Traefik is deployed behind an external proxy that modifies the source IP?1. Distributed state support
Traefik’s rate‑limit middleware can operate in a distributed mode when you are running v2.5 or newer. In this mode you configure an external Redis or Memcached store; all replicas share the same token bucket, so the global limit is enforced across the cluster.
2. Client identification behind a proxy
By default the sourceCriterion uses the request’s source IP. If an upstream proxy rewrites that IP, Traefik will see the proxy’s IP unless you enable the forwardedHeaders middleware and trust the proxy’s X‑Forwarded‑For or X‑Real‑IP headers. When properly configured, the rate limit key will be the original client IP, not the proxy’s.
traefik --versionrateLimit block. If it contains a redis or memcached subsection, distributed mode is already enabled.forwardedHeaders middleware with a trustedIPs list that includes your load balancer’s IP range.rateLimit middleware with a distributed configuration, e.g.:
http:
middlewares:
rl-global:
rateLimit:
average: 100
burst: 200
redis:
address: "redis://redis:6379"
key: "traefik:rateLimit"
forwardedHeaders middleware:
http:
middlewares:
fh-proxy:
forwardedHeaders:
trustedIPs:
- "10.0.0.0/24"
X‑Forwarded‑For or X‑Real‑IP headers with the original client IP.≥ 2.5?trustedIPs correctly covering the load balancer?If you are unsure about the Traefik version, run traefik --version and share the output. This will determine whether distributed mode is available and guide the configuration steps accordingly.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.