On asynchronous processing of ASP.NET 4.5 page requests

Posted: (EET/GMT+2)

 

Especially in .NET 4.5, asynchronous code execution possibilities are almost everywhere in the framework, including ASP.NET web applications. I've lately received multiple questions on the possible benefits of asynchronous processing in ASP.NET web applications, and while it's certainly a useful possibility, there are also many misconceptions about the technology.

First and foremost, asynchronous solutions are not a silver bullet to solve all your scalability and performance problems. If taking into consideration only a single, synchronous request that returns data from an SQL Server database, this same request will probably execute slower if simply converted to use the latest .NET 4.5 asynchronous APIs, if you don't at the same improve the implementation you have.

Put another way, a single request to your ASP.NET web application can become a little slower to process overall, but the real benefit of asynchronous implementations comes from the fact that it allows your application to serve many more users, and improve the response time of those requests that don't necessarily work with your database.

Although there are many places and ways you can improve your .NET web applications with .NET 4.5's asynchronous programming features, I tend to think the best benefits can be got from routines/requests/methods that are I/O bound. Most notably, this affects code that processes SQL databases and network calls to other applications or systems.

In this case, making the SQL query or network call asynchronous with C#'s async/await keywords, you can give other requests the ability to better use your server's resources. By default, the .NET thread pool is limited to 5,000 threads. This is a lot for small applications, but for larger ones, this could quickly be consumed. Similarly, your own code can do something else while you wait for your database backend to respond.

Now, back to asynchronous implementations in ASP.NET web applications. Firstly, asynchronous processing isn't new to ASP.NET 4.5. In fact, it was one of the new features in ASP.NET 2.0 (remember 2005?), but the newest ASP.NET 4.5 version greatly enhances these possibilities.

In case you are interested in the practical details, there's good introduction to asynchronous coding with ASP.NET 4.5 on the official pages. Check it out.

Happy hacking!