Senior
What happens to the Token Bucket rate limiter in the scenario: 1 request at the beginning of the second, 99 at the end, then another 100 in the next second?
sobes.tech AI
Answer from AI
Token Bucket is a rate limiting algorithm that allows tokens to accumulate at a certain rate up to a maximum bucket size. Each request 'spends' one token.
In your scenario:
- At the beginning of the first second, 1 request arrives — the bucket, assumed full, allows the request, and a token is spent.
- At the end of the first second, 99 requests arrive in a row. If tokens haven't accumulated enough, most of them will be rejected due to insufficient tokens. If the bucket holds 100 tokens, then after the first request at the beginning of the second, 99 tokens remain in the bucket, and 99 requests at the end of the second can be served (if tokens haven't been replenished, then most likely not).
- At the beginning of the next second, tokens start to accumulate again, and 100 requests can be served if the bucket size and replenishment rate allow.
In summary: Token Bucket helps smooth out peak loads, but if requests come too quickly and exceed the number of accumulated tokens, excess requests will be rejected or delayed.