Tutorials  Articles  Notifications  Login  Signup


RK

Rajan Kumar

Founder at HackersFriend May 9, 2022, 9:25 p.m. ⋅ 2826 views

Leaky Bucket algorithm for Rate limiting system design


Designing a rate limiting system for any API can be done in many ways. In this article I'll talk about one of them called Leaky Bucket algorithm.

In Leaky bucket algorithm, we assume incoming traffic to be water and we assume there a bucket which is having a hole, which can leak this water, at a constant rate. If this bucket is full, incoming water will be spilled or ignored. This way leaky bucket algorithm ensures a constant, limited and smooth flow of requests and put a rate limit on API calls.

Leaky bucket algorigthm
Leaky bucket algorithm

In implementation we introduce a constant variable, that denotes capacity of the bucket, and another counter to store how many open requests are there. Then, with every incoming request, this counter gets incremented and with every served request, this counter gets reduced, if total open requests gets equal to the bucket size, we will ignore all incoming requests.

 



HackerFriend Logo

Join the community of 1 Lakh+ Developers

Create a free account and get access to tutorials, jobs, hackathons, developer events and neatly written articles.


Create a free account