0

I'm using the cluster module to have multiple worker that fetch data from an API, process it and write an aggregate to the DB. The problem is, that the API has limited the requests per second. Now I'm searching for a solution to sync the limitation across all workers.

I'm thankful for every hint to solve this.

Christian D.
  • 65
  • 2
  • 9

1 Answers1

0

If you have a limit of number of requests per second, you could keep track of how many requests you have left in the master thread and each child could ask the master thread if it can send a request before sending, and the master thread would only fulfill the request when it has requests available for the current second. Here is another answer showing how master -> slave communication works.

At the end of each second, you would then reset the master thread to the number of requests available.

This approach would be best for achieving the maximum, however a much simpler approach would be to start N number of thread and allow them to make K number of requests per second, where K * N is just less than the number of requests allowed per second. The safest and least likely way to hit the limit with this is to do a setTimeout between the end of one request and start of the next request, but that would avoid the delay it takes processing the request. The next best option is for each thread to fire N number of requests at the start of the second and not firing again until the next second.

Your safest solution is to not go close to the limit and instead stick to max of N/2 requests per second where N is the max number of requests per second.

Community
  • 1
  • 1
Benjamin Kaiser
  • 2,177
  • 22
  • 24