Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rate limiting requests and Amazon SQS

I have a question about how to slow down my api requests. For a particular third party API I am hitting allows me to make 3 requests every 2 seconds. If I go over that number I am returned status code 429 along with a wait time in milliseconds.

This api is called often and is a direct result of my own server having incoming requests which are not rate limited.

Since I do not have any need for synchronous handling of the third party api requests I decided to offload the work to my elastic beanstalk worker box on AWS which by default reads from Amazon SQS.

As a result, my worker will throw the SQS message back into the queue if a status code 429 is returned from the third party api. This inevitably makes the api call work when the waitime is reached. This however seems like a bad solution

Is there any way to tell the daemon on the worker box to leave the message in the queue for the allotted wait time? Or can I perhaps set the rate at which the daemon will read from the queue? I'm looking for a proper way (implementation specific) to rate limit using the worker and the queue on AWS. Thank you so much for the help!

EDIT: I would have assumed that there are configurations that could be modified on AWS to do what I am asking but either way I'm looking for specific solutions for the setup I described. I'm not quite sure how to modify or control the daemon on the elastic beanstalk worker box.

like image 617
AIntel Avatar asked Nov 14 '16 18:11

AIntel


1 Answers

As I understand, you have bunch of triggers for calling a 3rd party service and you need to rate-limit your API calls.

The best way is to rate-limit the daemon that is reading from SQS. Depending on the language in which the daemon is written, you should be able to easily find rate-limiter libraries that you can reuse. For e.g., Java and Python have well-tested libraries here and here respectively.

Keep in mind that these libraries will allow X requests per second per worker. If you have one daemon running, X will be 1.5, for your use case. If you have two daemons (for e.g., one each on two different machines), X should be 0.75

like image 197
ketan vijayvargiya Avatar answered Sep 21 '22 12:09

ketan vijayvargiya