0

My app makes http requests to another server on behalf of users. This question concerns doing this in the optimal way.

The app may need to return the result of many http requests (to foreign server) to a user as quickly as possible. I have used threading to optimise this as best I can. However, I don't know enough to understand what happens when multiple users use the site at once. Bizarrely, when I simulated a few users making requests to the (local) development server at the same time, the response time didn't appear to suffer at all.

I was thinking of making a class that queues http requests and processes them when it can, however, I wouldn't know whether to instantiate it at App level or User level.

The local Web server is webrick and the production server is puma.

Questions:

  1. What happens when multiple users make requests to my site (where, for each request, my site must in turn make multiple requests to another site)?
  2. How do I optimise response times?
  3. Is having the users of my site send requests to the foreign site directly (ajax) a possibility?
Vorpulus Lyphane
  • 660
  • 6
  • 19
  • I've posted a simpler version of this question [here](http://stackoverflow.com/questions/38453330/rails-sending-multiple-http-requests-to-a-another-server). – Vorpulus Lyphane Jul 19 '16 at 08:38

1 Answers1

0

Answering your questions one by one:

  1. Yes, for every user request, your site will make a separate request to the "other site" that you have mentioned
  2. For this, you can cache/store the response from that "other site" that you have mentioned in the Rails cache and can reuse it depending on the validity and applicability of the data that is returned by that other server. More on Rails caching
  3. You can do it the way you want. But, if the "other site" that you have mentioned is not owned by you, then it is always better to make client side request to your servers and then let your server handle bringing the response in the most optimised way possible (by either storing or caching the response). This way, you will always have the ball in your court and you need not worry about if at all the response from that "other site" changes

Please let me know if this helps or you need more info.

Tushar
  • 166
  • 2
  • 11
  • thanks - appreciate you taking the time to respond and your answers to 2. and 3. are very helpful. Question 1 is the main one, though, and it was intended to be asked in the context of everything else I've written. I.e., for a single user, I can use threads to minimise the time taken to get multiple responses from the foreign server. But what happens when multiple users make a request to my site? How does my server go about getting all of these responses and how do I optimise? Say the optimal number of threads for a single user is 10. Should I use 10 threads TOTAL for multiple users? – Vorpulus Lyphane Jul 18 '16 at 09:35