How to implement distributed rate limiter?

Dhrumil Upadhyaya picture Dhrumil Upadhyaya · Jul 19, 2015 · Viewed 9.7k times · Source

Let's say, I have P processes running some business logic on N physical machines. These processes call some web service S, say. I want to ensure that not more than X calls are made to the service S per second by all the P processes combined.

How can such a solution be implemented?

Google Guava's Rate Limiter works well for processes running on single box, but not in distributed setup.

Are there any standard, ready to use, solutions available for JAVA? [may be based on zookeeper]

Thanks!

Answer

Bukhtoyarov  Vladimir picture Bukhtoyarov Vladimir · Apr 28, 2017

Bucket4j is java implementation of "token-bucket" rate limiting algorithm. It works both locally and distributed(on top of JCache). For distributed use case you are free to choose any JCache implementation like Hazelcast or Apache Ignite. See this example of using Bucket4j in cluster.