1

I am using RestTemplate and Spring Boot to call an external service which have rate limiting, in place. For example, if we send more than 10 requests at once, it will throw an error stating- "Too Many Requests".

How can I make sure as a client to not send more than 10 requests at once and also not loosing any incoming requests to me?

Can anyone help me with that?

Edit:

I had an idea of maintaining queue and send requests one by one. But its not optimal to send one request at a time when server can handle 10 requests. Thus, using this approach does not seem optimal.

Kshitiz Sharma
  • 722
  • 6
  • 14
  • 34
  • 1
    https://www.baeldung.com/spring-bucket4j https://medium.com/teamarimac/implementing-throttling-in-java-spring-boot-ec4723cfce9f https://stackoverflow.com/questions/44042412/how-to-set-rate-limit-for-each-user-in-spring-boot I have not done this before, however it is interesting. Some of those links cover throttling from sending to receiving. good luck – JCompetence Feb 25 '21 at 08:33

1 Answers1

0

You can use Java ThreadPool to create 10 threads and use your old queue idea to feed that pool. And then you can use each thread as one single request to the server. I don't know the code details of your project so, I am not sure I can provide you with any code snippet sorry for that.

P.S. I just wanted to share my idea, since I don't have enough Reputation I can't tell you this in the comments section sorry for that.

Akash Jain
  • 316
  • 2
  • 10
  • Yes. Initially I was also thinking the same and infact told the approach to interviewer. But follow up question was how would you make sure that maximum number of requests does not exceeds including all the threads. – Kshitiz Sharma Feb 26 '21 at 04:14
  • External service – Kshitiz Sharma Feb 26 '21 at 04:23
  • I don't think I am getting you there, but you are using 10 threads to represent 10 requests you make to the server. If your client sends 10 requests at once you can use those 10 threads and if he is sending more than that, then you can deny that request. I don't understand the last part `how would you make sure that maximum number of requests does not exceeds including all the threads.` this max number is for external request or your server from which you are calling that external service? – Akash Jain Feb 26 '21 at 04:29
  • This number is for external server. But we want to make sure as a client too that we do not send more than that number otherwise the request might get lost. – Kshitiz Sharma Feb 26 '21 at 04:33