Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

Token Bucket Algorithm

Sumit Rawal answered on May 14, 2023 Popularity 8/10 Helpfulness 4/10

Contents


More Related Answers

  • Leaking bucket algorithm
  • token bucket algorithm in javascript

  • Token Bucket Algorithm

    1

    Stripe (read here) uses a token bucket algorithm to throttle their API requests.

    As per the Stripe tech blog:

    We use the token bucket algorithm to do rate limiting. This algorithm has a centralized bucket host where you take tokens on each request, and slowly drip more tokens into the bucket. If the bucket is empty, reject the request. In our case, every Stripe user has a bucket, and every time they make a request we remove a token from that bucket. We implement our rate limiters using Redis.

    Image source System Design Interview — An insider’s guide by Alex Xu

    The bucket has tokens with a pre-defined capacity. When a request comes in it takes a token from the bucket and is processed further. If there is no token to be picked, the request is dropped and the user will have to retry.

    Example use-case:

    We have our rate-limiting rules set to 3 requests per user per minute.

    User1 makes the request at 00 time interval, currently the bucket is full with 3 tokens so request will be processed. Tokens will be updated in the bucket to 2 now.

    User1 makes a 2nd request at 10th sec, bucket has 2 tokens so again the request will be processed further. Tokens will be updated in the bucket to 1 now.

    User1 makes a 3rd request at say 30th sec, bucket has 1 token so again the request will be processed further. Now the bucket is empty till the whole 1 minute completes.

    User1 makes a 4th request at say 55th sec, the bucket has 0 tokens so the request is throttled and the user is returned with a 429 status code — too many requests and asked to retry it later in some time. The HTTP 429 Too Many Requests response status code indicates the user has sent too many requests in a given amount of time.

    At the completion of that 1 minute, the token count is refreshed at a fixed rate and the bucket is again full with 3 tokens and requests can be processed again for that particular user.

    In simple words:

    In the Token Bucket algorithm, we process a token from the bucket for every request. New tokens are added to the bucket with rate r. The bucket can hold a maximum of b tokens. If a request comes and the bucket is full it is discarded.

    The token bucket algorithm takes two parameters:

    Bucket size: the maximum number of tokens allowed in the bucket

    Refill rate: number of tokens put into the bucket every second  

    Popularity 8/10 Helpfulness 4/10 Language javascript
    Source: Grepper
    Link to this answer
    Share Copy Link
    Contributed on May 14 2023
    Sumit Rawal
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.