Token bucket explained: set average rate and burst capacity
A token bucket controls average traffic by letting requests consume tokens that refill at a configured rate. It also permits a short burst up to the bucket's capacity. Set refill rate and capacity separately to control sustained traffic and bursts.
A token bucket controls average traffic by letting requests consume tokens that refill at a configured rate. It also permits a short burst up to the bucket's capacity. Set refill rate and capacity separately to control sustained traffic and bursts.
How requests consume tokens
Let the bucket hold at most B tokens and refill at rate R tokens per unit of time. A request costs C tokens. Before checking a request, add tokens for elapsed time, capped at B. If the balance is at least C, admit the request and subtract C; otherwise reject it or queue it until enough tokens accrue. Cost 1 means one token per request; weighted work or bytes can use different costs.
With continuous refill, the balance is min(B, previous balance + R × elapsed time). For an illustrative bucket with B=8, R=2/second, and cost 1, an idle bucket fills to 8 and can admit a burst of 8. Once drained, it has 2 tokens after one second, so can admit 2 more. Its long-run average is at most 2 requests/second, although a short interval can exceed that rate. These values are hypothetical.
RFC 3290 describes a simple token-bucket profile as allowing at most R × t + B data over any interval t. The refill rate sets sustained throughput; capacity sets the burst bound. Choose R from sustainable downstream capacity and B from acceptable short bursts. Too little capacity can reject legitimate batches; too much can pass a large load after idle time.
Before configuring, decide what each token represents, whose bucket is being limited (user, tenant, or service), whether excess requests are rejected or queued, and where state is shared. Per-instance local buckets can multiply the effective allowance as instances scale. A global limit needs shared state or explicit capacity partitioning, with corresponding availability, cost, and accuracy trade-offs. Queues also need bounded size and delay.
Envoy v1.40 example
Envoy's type.v3.TokenBucket uses max_tokens, tokens_per_fill, and fill_interval. It starts full, adds the configured number at each interval, and never exceeds capacity. This periodic refill may differ from the continuous mathematical model above.
token_bucket:
max_tokens: 8
tokens_per_fill: 2
fill_interval: 1sThis illustrative setting starts with up to 8 tokens and replenishes 2 every second. Envoy's HTTP local rate-limit filter returns 429 by default when empty. Optional Retry-After is off by default; when enabled, it reports the seconds until the next token, with a minimum of one second. In v1.40, the bucket is shared per Envoy process by default, not automatically across hosts. The filter can instead use per-downstream-connection buckets. Check version-specific docs before using other releases. Inspect enabled, ok, rate_limited, and enforced counters to distinguish checks, decisions, and enforcement.
Before rollout, write down expected outcomes for a full bucket, a drained burst, refill boundaries, and concurrent requests across instances. Verify normal and depleted behavior in a test environment; monitor limiter counters alongside downstream load; document rollback and any temporary bypass conditions.
A token bucket meters request rate and bursts. It does not cap in-flight work or guarantee backend capacity. Use concurrency limits for long-running work and consider shaping/queues when smooth output is the goal. RFC 3290 distinguishes arrival metering with token buckets from leaky-bucket output shaping.
Sources
[RFC 3290, Appendix A](https://www.rfc-editor.org/rfc/rfc3290.html)
[Envoy v1.40 TokenBucket API](https://www.envoyproxy.io/docs/envoy/v1.40.0/api-v3/type/v3/token_bucket.proto)
[Envoy v1.40 HTTP local rate limit](https://www.envoyproxy.io/docs/envoy/v1.40.0/configuration/http/http_filters/local_rate_limit_filter)