Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance difference between Synchronous HTTP Handler and Asynchronous HTTP Handler

Is there a performance difference between Synchronous HTTP Handler and Asynchronous HTTP Handler? IHttpHandler vs IHttpAsyncHandler

Why choose one over another?

What are the benefits?

like image 927
DarthVader Avatar asked Aug 18 '11 02:08

DarthVader


1 Answers

ASP.NET uses the thread pool to process incoming HTTP requests.

When an IHttpHandler is called, a thread pool thread is used to run that request and the same thread is used to process the entire request. If that request calls out to a database or another web service or anything else that can take time, the thread pool thread waits. This means thread pool threads spend time waiting on things when they could be used to process other requests.

In contrast, when an IHttpAsyncHandler, a mechanism exists to allow the request to register a callback and return the thread pool thread to the pool before the request is fully processed. The thread pool thread starts doing some processing for the request. Probably calls some async method on a database call or web service or something and then registers a callback for ASP.NET to call when that call returns. At that point, the thread pool thread that was processing the HTTP request is returned to the pool to process another HTTP request. When the database call or whatever does come back, ASP.NET triggers the registered callback on a new thread pool thread. The end result is you don't have thread pool threads waiting on I/O bound operations and you can use your thread pool more efficiently.

For very high concurrency applications (hundreds or thousands of truly simultaneous users), IHttpAsyncHandler can provide a huge boost in concurrency. With a smaller number of users, there can still be a benefit if you have very long running requests (like a Long Polling request). However, programming under the IHttpAsyncHandler is more complicated, so shouldn't be used when it isn't really needed.

like image 68
Samuel Neff Avatar answered Sep 24 '22 05:09

Samuel Neff