Rate Limiter Algorithms
In this lesson2 sections
Rate Limiter Algorithms
Algorithms for rate limiting
A rate-limiting algorithm defines which requests may proceed and how much state the limiter must keep. Compare these five options against the policy established in the previous lesson:
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 refill rate of tokens per second and a bucket capacity of tokens. Each request costs one token in this example.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, admit only as many requests as available tokens permit and reject the rest. Refill, check, and consumption must form one atomic decision for a shared bucket.
The following illustration demonstrates the token bucket workflow.
The rest of this lesson is for Premium members
Premium opens every lesson in this course, along with all questions and solutions on PracHub.