rateLimit: idle buckets expire before refilling, allowing clients to exceed the configured rate when burst > 1 and rate < 1 req/s
Welcome!
- Yes, I've searched similar issues on GitHub and didn't find any.
- Yes, I've searched similar issues on the Traefik community forum and didn't find any.
What did you do?
We use the rateLimit middleware to enforce per-tenant quotas expressed per minute (for example average: 30, period: 1m, burst: 30, keyed on a request header).
A client sending bursts separated by a few seconds of silence gets a full bucket back after each pause, and can exceed the configured rate by a large factor.
Minimal reproduction (Traefik v3.7.10, file provider, traefik/whoami backend):
docker-compose.yml
services:
traefik:
image: traefik:v3.7.10
command:
- --entrypoints.web.address=:8080
- --providers.file.filename=/etc/traefik/dynamic.yml
ports:
- "8080:8080"
volumes:
- ./dynamic.yml:/etc/traefik/dynamic.yml:ro
whoami:
image: traefik/whoamidynamic.yml
http:
routers:
r:
entryPoints: [web]
rule: PathPrefix(`/`)
middlewares: [rl]
service: s
middlewares:
rl:
rateLimit:
average: 30
period: 1m
burst: 30
sourceCriterion:
requestHeaderName: X-Tenant-Id
services:
s:
loadBalancer:
servers:
- url: http://whoami:80repro.sh
#!/usr/bin/env bash
# Usage: ./repro.sh <pause_seconds>
burst() {
seq 40 | xargs -P 20 -I{} curl -s -o /dev/null -w "%{http_code}\n" \
-H "X-Tenant-Id: tenant-a" http://localhost:8080/ | grep -c 200
}
for i in 1 2 3; do
echo "burst $i: $(burst) accepted"
[ "$i" -lt 3 ] && sleep "$1"
doneRun ./repro.sh 1, wait at least 60 seconds for the bucket to refill, then run ./repro.sh 5.
What did you see instead?
| Pause between bursts | Accepted per burst | Total | Elapsed |
|---|---|---|---|
| 1 s | 33, 0, 1 | 34 | 2.2 s |
| 5 s | 30, 30, 30 | 90 | 10.1 s |
With a 1 s pause, the limiter behaves roughly as a token bucket should. With a 5 s pause, 90 requests are accepted in about 10 seconds while the configured rate is 30 per minute. In a standard token bucket, an empty bucket with a capacity of 30 refilled at 0.5 token/s needs 60 seconds to be full again.
Root cause. In pkg/middlewares/ratelimiter/rate_limiter.go, the TTL of an idle bucket only depends on the rate:
ttl := 1
if rtl >= 1 {
ttl++
} else if rtl > 0 {
ttl += int(1 / rtl)
}For 30 req/min, rtl = 0.5, so ttl = 3 seconds. In in_memory_limiter.go, an expired bucket is recreated with rate.NewLimiter(i.rate, int(i.burst)), which starts full. The ttlmap also stores expiries as Unix seconds, so the effective TTL is between ttl - 1 and ttl seconds.
The TTL is sized so that about one token is refilled during the idle period. This is harmless when burst is 1 (the default), because the bucket would be full anyway when evicted. With a larger burst, the bucket is evicted while still mostly empty, and the client gets the missing tokens for free. The same ttl is passed to the Redis limiter, so the Redis backend is expected to behave the same way.
The effect is negligible for high rates (for example 100 req/s with burst 200, where refill time and TTL are both about 2 s), which may explain why it has gone unnoticed. It becomes large for per-minute limits: roughly burst / (ttl - 1) requests per second can be obtained instead of average / period.
This behavior is not documented on the rateLimit reference page.
Side note: the first burst of the 1 s run accepted 33 requests instead of 30. inMemoryRateLimiter.Allow does Get then Set without holding a lock across both, so concurrent first requests from a new source can each create their own full limiter. This is a separate, smaller effect and not the subject of this issue.
Suggested fix. Never evict a bucket before it would have refilled completely:
if rtl > 0 {
if refill := int(math.Ceil(float64(burst) / rtl)); refill > ttl {
ttl = refill
}
}With this change, an evicted bucket is always equivalent to a full one, so eviction no longer grants extra tokens. Memory stays bounded by maxSources; buckets live up to burst / rate seconds after the last request instead of about 1 / rate seconds.
This matches other token bucket implementations, for example Bucket4j's ExpirationAfterWriteStrategy.basedOnTimeForRefillingBucketUpToMax. If the current behavior is intentional, documenting it would help users who configure per-minute limits.
Tested with the patch. Built from the v3.7.10 tag with only the change above (CGO_ENABLED=0 go build ./cmd/traefik), same configuration, ./repro.sh 5:
| Build | Accepted per burst | Total | Elapsed |
|---|---|---|---|
| v3.7.10 | 30, 30, 30 | 90 | 10.1 s |
| v3.7.10 + patch | 30, 2, 3 | 35 | 10.1 s |
With the patch, the second and third bursts only get the tokens refilled during the pauses (0.5 token/s).
Related but distinct: #13704 (expired buckets are not reclaimed until the map is full). That issue is about memory; this one is about buckets expiring too early.
Possibly related: https://community.traefik.io/t/inconsistency-with-rate-limit-middleware/18190 (inconsistent 429s with 10 requests per minute, burst 8, header-based source).
What version of Traefik are you using?
Version: 3.7.10
Codename: langres
Go version: go1.26.5What is your environment & configuration?
See the reproduction above (Docker, file provider). Originally observed with a single Traefik replica and in-memory rate limiting.
If applicable, please paste the log output in DEBUG level
No errors are logged. Rejected requests return 429 with a Retry-After header as expected. The issue is only that a bucket idle for a few seconds is recreated full.
Source: traefik/traefik