title: How to properly throttle web requests to external systems?
body: <p>My Java web application pulls some data from external systems (JSON over HTTP) both live whenever the users of my application request it and batch (nightly updates for cases where no user has requested it). The data changes so caching options are likely exhausted.</p>
  
  <p>The external systems have some throttling in place, the exact parameters of which I don't know, and which likely change depending on system load (e.g., peak times 10 requests per second from one IP address, off-peak times 100 requests per second from open IP address). If the requests are too frequent, they time out or return HTTP 503.</p>
  
  <p>Right now I am attempting the request 5 times with 2000ms delay between each, giving up if an error is received each time. This is not optimal as sometimes at peak-times nearly all requests fail; I could avoid making these requests and perhaps get at least some to succeed instead.</p>
  
  <p>My goals are to have a somewhat simple, reliable design, and enough flexibility so that I could both pull some metrics from the throttler to understand how well the external systems are responding (and thus adjust how often they are invoked), and to auto-adjust the interval with which I call them (individually per system) so that it is optimal both on off-peak and peak hours.</p>
  
  <p>My infrastructure is Java with RabbitMQ over MongoDB over Linux.</p>
  
  <p>I'm thinking of three main options:</p>
  
  <ol>
  <li><p>Since I already have RabbitMQ used for batch processing, I could just introduce a queue to which the web processes would send the requests they have for external systems, then worker processes would read from that queue, throttle themselves as needed, and return the results. This would allow running multiple parallel worker processes on more servers if needed. My main concern is that it isn't a very simple solution, and how to manage peak-hour throughput being low and thus the web processes waiting for a long while. Also this converts my RabbitMQ into a critical single failure point; if it dies the whole system stops (as opposed to the nightly batch processes just not running any more, which is less critical). I suppose <a href="http://www.rabbitmq.com/tutorials/tutorial-six-java.html" rel="nofollow noreferrer">rpc</a> is the correct pattern of RabbitMQ usage, but not sure. Edit - I've posted a related question <a href="https://stackoverflow.com/questions/18416525/how-to-properly-implement-rabbitmq-rpc-from-java-servlet-web-container">How to properly implement RabbitMQ RPC from Java servlet web container?</a> on how to implement this.</p></li>
  <li><p>Introduce nginx (e.g. <a href="http://nginx.org/en/docs/http/ngx_http_limit_req_module.html" rel="nofollow noreferrer">ngx_http_limit_req_module</a>), HAProxy (<a href="http://blog.serverfault.com/2010/08/26/1016491873/" rel="nofollow noreferrer">link</a>) or other proxy software to the mix (as reverse proxies?), have them take care of the throttling through some configuration magic. The pro is that I don't have to make code changes. The con is that it is more technology used, and one I've not used before, so chances of misconfiguring something are quite high. It would also likely not be easy to do dynamic throttling depending on external server load, or prioritizing live requests over batch requests, or get statistics of how the throttling is doing. Also, most documentation and examples will likely be on throttling incoming requests, not outgoing.</p></li>
  <li><p>Do a pure-Java solution (e.g., leaky bucket implementation). Would be simple in the sense that it is "just code", but the devil is in the details; debugging all the deadlocks, starvations and race conditions isn't always fun.</p></li>
  </ol>
  
  <p>What am I missing here? </p>
  
  <p>Which is the best solution in this case?</p>
  
  <p>P.S. Somewhat related question - what's the proper approach to log all the external system invocations, so that statistics are collected as to how often I invoke them, and what the success rate is?</p>
  
  <p>E.g., after every invocation I'd invoke something like .logExternalSystemInvocation(externalSystemName, wasSuccessful, elapsedTimeMills), and then get some aggregate data out of it whenever needed.</p>
  
  <p>Is there a standard library/tool to use, or do I have to roll my own?</p>
  
  <p>If I use option 1. with RabbitMQ, is there a way to organize the flow so that I get this out of the box from the RabbitMQ console? I wouldn't want to send all failed messages to poison queue, it would fill up too quickly though and in most cases there is no need to re-process these failed requests as the user has already sadly moved on.</p>