...

/

Rate Limiting Using Token Bucket Filter

Rate Limiting Using Token Bucket Filter

Implementing rate limiting using a naive token bucket filter algorithm.

Problem Statement

This is an actual interview question asked at Uber and Oracle. Imagine you have a bucket that gets filled with tokens at the rate of 1 token per second. The bucket can hold a maximum of N tokens. Implement a thread-safe class that lets threads get a token when one is available. If no token is available, then the token-requesting threads should block. The class should expose an API called getToken that various threads can call to get a token.

Press + to interact

Solution

This problem is a naive form of a class of algorithms called the “token bucket” algorithms. A complimentary set of algorithms is called “leaky bucket” algorithms. One application of these algorithms is shaping network traffic flows. This particular problem is interesting because the majority of candidates incorrectly start with a multithreaded approach when taking a stab at the problem. One is tempted to create a background thread to fill the bucket with tokens at regular intervals but there is a far simpler solution devoid of threads and a message to make judicious use of threads. This question tests a candidate’s comprehension prowess as well as ...