Rate Limiter Algorithms
Review five common rate limiting algorithms: token bucket, leaky bucket, fixed window counter, sliding window log, and sliding window counter. For each algorithm, define the key parameters and trade-offs, particularly around burst tolerance and memory usage. Select the appropriate algorithm based on traffic patterns and stability requirements.
Algorithms for rate limiting
Rate limiters rely on efficient algorithms to control traffic flow. While requirements vary, we will focus on the most popular options:
Token bucket
Leaking bucket
Fixed window counter
Sliding window log
Sliding window counter
Token bucket algorithm
The Token Bucket algorithm uses a container with a predefined capacity. Tokens are added at a constant rate. When a request arrives, it attempts to consume a token. If the bucket is empty, the request is rejected.
The algorithm operates as follows:
Assume a rate limit of
A new token is added to the bucket every
1/Rseconds.If the bucket reaches capacity
, incoming tokens are discarded. Incoming requests consume tokens. If
requests arrive and at least tokens exist, the requests are processed. If the bucket has fewer tokens than incoming requests, the system processes only the available number and discards the rest.
The following illustration demonstrates the token bucket workflow.
In this example, the bucket capacity is three, and it refills at a rate of three tokens per minute.
Essential parameters
Key parameters for the token bucket algorithm include:
Bucket capacity
: The maximum number of tokens the bucket can hold. Rate limit
: The number of requests allowed per unit of time. Refill rate
...