CLOUD CONCEPT

Token Bucket

Token Bucket is a rate-limiting algorithm that permits short bursts of activity while controlling the sustained rate through a finite supply of replenishing tokens.

Cloud Concepts Hub →

What is Token Bucket

Each request or unit of traffic consumes tokens from a bucket that refills at a configured rate up to a maximum capacity. This separates sustained throughput from burst allowance, helping cloud architectures protect downstream capacity and control resource consumption without rejecting every temporary traffic spike.

A token bucket controls how much work a system admits over time. The bucket holds up to a configured number of tokens, and tokens replenish at a configured rate. A request proceeds only when enough tokens are available, consuming the required amount. Tokens added to an already full bucket are discarded, so unused capacity cannot accumulate indefinitely. When tokens are insufficient, the implementation may reject the request, delay it, or place it in a separate queue.

The two main settings are the refill rate and bucket capacity. For example, an API limiter might replenish 100 tokens per second and hold at most 200 tokens, with each request consuming one token. After sufficient idle time, a full bucket can admit a burst of 200 requests, subject to other system limits. Further requests depend on replenishment. In an ideal implementation, admitted work over an interval of t seconds cannot exceed the tokens available at the beginning plus 100 times t. This does not mean that every one-second window is limited to exactly 100 requests.

In cloud architectures, token buckets commonly appear at API gateways, service endpoints, application middleware, and network traffic controls. An API limiter typically measures requests, whereas a network limiter may charge tokens according to packet size or byte count. Weighted request costs are also possible: an expensive report-generation operation could consume more tokens than a lightweight metadata lookup. Always establish the unit being limited before interpreting a configuration.

For cost-optimized designs, place admission control before expensive backend processing where practical. Suppose a public API invokes compute functions that query a database. A token bucket at the gateway can reduce the amount of excess traffic reaching those components, limiting unnecessary compute, database activity, and downstream network traffic. However, rejected requests can still incur charges at the layer receiving them. Throttling is therefore a workload-control mechanism, not a guaranteed spending cap or a substitute for budgeting and security controls.

Bucket capacity is a deliberate trade-off. A larger bucket accommodates brief legitimate spikes but can overwhelm a backend that lacks burst capacity. A smaller bucket protects the backend more tightly but can reject useful requests even when average demand is acceptable. Choose the refill rate from sustainable downstream throughput, and size the burst allowance against available headroom and acceptable latency. A request-rate limit does not directly limit concurrency: slow requests can accumulate even when their arrival rate remains within the configured rate.

Token-bucket throttling is not automatically traffic smoothing. A rejecting limiter admits an allowed burst immediately; delaying traffic requires a shaping or queuing mechanism. A fixed-window counter instead counts requests within defined windows and can allow large spikes around window boundaries. When an exam scenario requires a controlled sustained rate with occasional bursts, token-bucket throttling is an appropriate match. When every accepted job must eventually execute, use durable buffering and controlled consumers rather than relying on rejection alone.

Clients should handle throttling responses, commonly HTTP 429, with bounded exponential backoff and jitter, respecting retry guidance when supplied. Immediate retries can amplify overload and costs. In distributed deployments, independent local buckets multiply the effective allowance unless coordinated or assigned portions of a shared limit. Managed-service throttles may also be best-effort targets rather than exact ceilings, so combine them with backend safeguards and monitor throttled requests, latency, errors, and resource utilization.

Key aspects of Token Bucket

Token buckets combine a bounded burst allowance with a replenishing throughput budget, with practical behavior determined by token units, enforcement scope, and exhaustion policy.

Sustained rate and burst capacity

The refill rate controls long-term admitted throughput, while bucket capacity controls accumulated burst allowance. Configure both against downstream capacity rather than treating the burst setting as another requests-per-second value.

Bounded accumulation

Idle time replenishes tokens only until the bucket is full. A long period of inactivity therefore cannot create an unlimited allowance for a later traffic spike.

Explicit token accounting

Tokens can represent requests, bytes, or weighted work units. The accounting unit determines whether the limiter protects request-processing capacity, network bandwidth, or another constrained resource.

Admission policy on exhaustion

An empty bucket can trigger rejection, waiting, or queuing, depending on the implementation. Token accounting alone does not provide durable buffering or guarantee eventual processing.

Scope and distributed enforcement

Buckets may apply per client, route, tenant, account, or shared service boundary. Independent buckets on multiple instances can increase the aggregate allowance unless their limits are coordinated.

Backend protection with cost trade-offs

Early throttling can prevent excess traffic from consuming expensive downstream resources. It does not eliminate ingress-layer charges, guarantee a spending ceiling, or replace concurrency limits and overload protection.

Cloud services for Token Bucket

Amazon API Gateway can enforce token-bucket admission control on application requests before they reach backend compute and databases. Amazon EC2 and Elastic Load Balancing apply the same general algorithm to management APIs used by deployment and scaling automation. These are separate enforcement domains: limiting application traffic does not reserve capacity for infrastructure API calls, and management API throttling does not regulate application throughput. Configure application limits around downstream capacity, and make infrastructure automation tolerate throttling through bounded retries, backoff, and reduced unnecessary polling.

Amazon API Gateway image Amazon API Gateway

Amazon API Gateway uses token-bucket throttling to apply request-rate and burst limits. Applicable account, stage, route or method, and usage-plan settings depend on the API type; throttling limits are best-effort targets rather than guaranteed request or cost ceilings.

Open Amazon API Gateway →
Amazon EC2 image Amazon EC2

Amazon EC2 uses token buckets to throttle management API requests, with separate resource-based throttling for certain actions. These limits affect operations such as infrastructure provisioning, not the application traffic or network bandwidth of a running instance.

Open Amazon EC2 →