Implementing leaky bucket using bucket4j to rate limit an API

546 views Asked by At

We are writing APIs in microservice based architecture. Since one perticular API does lot of computation and consume a lot of resources, we want to rate limit this API. We have used bucket4j to rate limit the API with token bucket algorithm. But we want to implement leaky bucket algorithm.

Does bucket4j support leaky bucket algorithm ? If yes can someone please provide me an example showing that. If no then what will be other possible solution? Should I simply try using blocking queue of Java ? Any implementation example will be of great help

1

There are 1 answers

0
stelios.anastasakis On

Even though bucket4j is a token-bucket implementation, the default limits (in simple bandwidths) are set as Greedy. Greedy means, with an example, that Refill.greedy(10, Duration.ofSeconds(1)); will create 1 token every 100ms "after it was consumed from a bucket". So if you work on maximum rate, then you have a leaky bucket in practise.

Now, there is another config that could help you implement this for every other case. Havent tried that but maybe you could go for it and let us know. If you set Initial tokens as 1, maybe this will do the trick.

Last thought, a brute force way would be possible here. Leaky means that you get a constant rate of, lets say 1 event every 1sec. You can implement that by just setting Refill.greedy(1, Duration.ofSeconds(1));.