Reputation: 15
I have a Web Application where i receive a lot(100K per hour) of http requests. I do not have any concerns regarding the Servlet container thread handling.
However, for each such web request, I would like to further multithread the process so that i can increase the response time of each request.
I would like to use ExecutorService , initialize newFixedThreadPool for every such request and use future object to stitch back the response from all concurrent threads(for each request) and finally submit the response back to the client.
The advantage i see with this approach is my response time is drastically reduced as multiple threads are acting upon the same request rather than a single thread.
Please suggest if this is the right way?
Upvotes: 0
Views: 104
Reputation: 27115
Please suggest, is this the right way?
Your server is handling almost 30 requests per second on average. How long does each request take to complete? How much CPU time does each request use? What percent of the available CPU is that? How much time does each request spent waiting for I/O, or waiting to talk to other services (e.g., a database)? If there's significant compute time, what is the computation? Does it make sense to parallelize it?
Nobody can say whether multi-threading is "the right way" without knowing the answers to those questions.
Edit: You say each request takes 400ms. OK, if they're coming in at a steady 100K per hour pace, that means that at any given instant, your server will be processing about eleven simultaneous requests.
That's useful information, but maybe not as useful as the other questions that I asked: How much CPU time does each request use? What percent of the available CPU is that? How much time does it spend waiting for I/O? What percent of the available I/O bandwidth is that?
You say that each request is composed of several independent parts.
Maybe it will shorten the overall response time if you change the code to handle each part in a separate thread, and maybe it won't. There's just not enough information here---information about the hardware platform, information about your application, information about the resources that it uses---to predict the outcome.
Have you tried it?
You make it sound like you already know how to make the change. You make it sound like it won't be difficult. Sometimes, doing an experiment is the easiest way to find out.
Upvotes: 2