1

At what point is it advisable to use async controllers in ASP.NET MVC.

Is there any coding or performance costs involved?

MSDN recommends using it for long running processes, but I was just curious if it would it be beneficial if we used it as a complete replacement to normal controllers?

We are planning to use WCF services with our controller methods.

Servy
  • 202,030
  • 26
  • 332
  • 449
Ammar
  • 686
  • 11
  • 28

2 Answers2

4

First, async is not synonymous with "performance". In fact, using async can actually decrease performance as there's a non-trivial amount of overhead involved in async.

What async does do is release threads back to the pool when they're in a wait-state. This means that your web server is given a higher threshold before it exhausts it's "max requests" or, in other words, runs out of free threads to handle new requests.

In a synchronous request, the thread is tied up for the entire request. If there's some period of waiting involved (network latency from an API call, etc.) it's holding on to that thread even though no work is actually being done. If you got hit with 1000 simultaneous requests (the typical out-of-the-box max requests for a web server), then each further request would be queued until one for the first 1000 threads was returned to the pool.

In an async request, as soon as the thread is waiting on something to happen (i.e. not doing work), it is given back to the pool, even though the original request it was serving has not yet completed. This allows a new request to be served. When the original task that forfeited the thread completes, a new thread is requested from the pool to continue servicing that request. This effectively gives your server a little breathing room when under load. Other than that, async does nothing, at least in the context of a request being served by a web server.

In general, using async is recommended, because even that little bit of breathing room it provides may mean the difference between your server handling load or falling down. However, you should gauge your usage of async to ensure that you're actually buying something worthwhile of the overhead it adds. For example, MVC 6 lets you do things like render partials asynchronously. If your server is equipped with an enterprise class 15,000 RPM hard drive or an SSD, though, the period of waiting the thread would experience would likely be so minuscule that the passing of the thread back and forth would actually take more time than the operation itself, run synchronously.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
1

I would say that this topic is nicely covered on this post: When should I use Async Controllers in ASP.NET MVC?

My opinion is that it's good to use async actions when you call async methods in it (like I/O operations), it's not especially bad when you make an async action without any async calls inside, but:

  • You will have a needless thread switch, not a big performance penalty, but not nice either
  • VS will warn you, that there is no await in your async action, which can lead to unnecessary Task.Run calls
Community
  • 1
  • 1
alek kowalczyk
  • 4,896
  • 1
  • 26
  • 55