cduggan cduggan - 1 month ago 12
Java Question

Can I use Akka to throttle web service requests made in a distributed system?

Can Akka provide web request throttling for a distributed application (Java/JEE)?

My application consumes a number of web services each of which allows a maximum number of concurrent requests. I've looked at


  • TimerBasedThrottle - suitable for single node application, but not possible to manage the number or requests sent in distributed setting.

  • Balancing dispatcher - shares a single mailbox between Actors sending messages to idle Actors, but it does not enforce a limit per second/minute, etc.



What is the best way to achieve this with Akka?

Answer

If you want to control and limit the number of certain actions performed by a distributed system, then your distributed nodes will have to communicate to make that happen. The easiest is to have one central actor which keeps track of all requests made to a certain target web service and handing out tickets allowing them to happen (delaying or dropping requests when going over the configured limit). If you do not want to incur the overhead of inter-node communication, then you will have to partition your quota such as to guarantee that by throttling all nodes individually their sum cannot exceed the limit, but this is obviously wasteful unless the request originators are perfectly balanced.