In this article, We will try to explore Token Bucket algorithm and its implementation in NodeJS for API Rate Limiting in very simple terms.
What is Token Bucket ?
Token Bucket is an algorithm which is used to limit our resources or server usage. It is an algorithm in which we have some finite amount of tokens on our server and whenever a request is made, a token is used for that request full fulfillment. When there are requests more than the number of tokens, then the server denies or awaits all the requests until the tokens are refilled.
The tokens can be refilled in various ways. Two of them are as follows :
-
whenever a request is fulfilled, the token used by the user is returned back to the server.
-
refilling the tokens after a specific interval of time.

Implementation of Token Bucket
- Setup a new npm project and set the "type":"module".

- Install express.
- Create a new file, name it anything, let’s say "index.js". Write a basic express server code in it.
Now we’ll be rate limiting the request to ‘/’. We’ll be programming a middleware to achieve this.
- Create a new file for the middleware, let’s say, "rateLimiter.js" and write the syntax of the middleware function and export it.
In this code, tokens is the array of the tokens which the user can use to make a request to the server. time is a variable in which we store the current time in milliseconds.
We have added simple if else condition in this, if any request comes and if the tokens array has a token, then a token will pop out and the request will be allowed, else the request will be denied.
After the tokens array becomes empty, we need to refill it.
Congratulations ! You have successfully added Rate Limitation to your server
The final code looks like this.
Conclusion
Now, I think we have understood how Token Bucket is implemented and will be able to customize the number of requests and tokens in it. If you have any queries, write in the comments and I will try to help you out.
FAQs:
What is the Token Bucket algorithm? The Token Bucket algorithm is used for rate limiting to control the consumption of resources or server usage. It involves maintaining a bucket of tokens, where each token represents permission to perform a specific action or request.
How does the Token Bucket algorithm work?
Requests consume tokens from the bucket. When the bucket is empty, further requests are either denied or queued until tokens are refilled. Tokens can be refilled at a fixed rate or based on certain conditions.
What is the purpose of rate limiting in APIs?
Rate limiting helps prevent abuse or overload of APIs by restricting the number of requests a client can make within a specified timeframe. It ensures fair usage of resources and protects the server from being overwhelmed.

