Mark
Mark

Reputation: 841

Request processing threads vs custom threads for async processing vs web server performance

Question: When I create a custom thread to handle incoming HTTP request in asynchronous way, am I actually harming the performance due to introduction of too many threads?

More insight: Lets say an incoming requests require some heavy database operation to be performed. Web server is heavy loaded and at any given moment 10 request processing threads are constantly busy processing requests. Server has 10 cores so lets assume 1 thread per core is running.

The requests are processed in a synchronous way, each request processing threads handles the job from arrival till completion. There is some wait on database required though.

Possible "improvement" would be to change the flow a bit, instead of request processing thread handling the whole request, additional thread is created to handle the heavy database operation and request processing threads is released early.

This raises concerns - now much more than 10 threads on 10 cores are required. - context switching will degrade performance

What I mean by request processing thread: http://docs.oracle.com/cd/E19146-01/821-1834/geeie/index.html

What I mean by custom thread:

void handleHttpMethod(){
  //request processing thread running here
  executorService.submit(new DBTask())
  //request processing thread exits here
}

I know this is a bit opened question but I am really interested in your feedback and comments.

[EDIT] Even more details: I'm running a web application deployed to Glassfish3 server which handles ~1000 requests/sec. Each request involves some DB operation (storing some data and no need to wait for result) which is heavy compared to other logic performed within the request. I'm trying to understand how going async may affect webserver performance, as request processing threads will now have to share the CPU with my custom threads created to handle DB operation.

Upvotes: 2

Views: 829

Answers (1)

Victor Sorokin
Victor Sorokin

Reputation: 12006

Note: below assumes that your DB runs on different host, so DB queries won't take CPU from your web-serving threads.

If DB queries times vary by client you can definitely improve throughput (in terms of number of served requests) by offloading DB requests to "custom" threads.

E.g., if client1Query runs for 10 seconds and there are no free threads, client2Query, which would need only 1 second to complete, will need to wait till either client1Query finishes, or some other thread becomes available.

Upvotes: 1

Related Questions