sagesky36
sagesky36

Reputation: 4692

Asynchronous Controllers

In my MVC projects, I make it a general rule to always use asynchronous controllers (async, Task, await).

Is this a good practice?

I know if you're dealing with IO bound data like pictures, etc, to get retrieved, but what about if a particular method gets hit a lot by multiple users? I would assume this would be a good practice since users would "not have to wait in line" for the previous call to finish executing --- correct?

When would it not be a good idea to use asynchronous controllers?

Upvotes: 0

Views: 114

Answers (1)

NeddySpaghetti
NeddySpaghetti

Reputation: 13495

I think unless you are executing asynchronous code in your controller action you should not make it async. Async controller action methods do not return to the client straight away, the request is parked while the asynchronous action completes and then the request response is sent back after that action completes.

This can make your application more scalable because the request thread is freed up to handle other requests and asynchronous work does not use a thread. So if it is getting hit by lots users this will be good for performance.

However if the work you are doing is not asynchronous it will make no difference, and in fact there will be some overhead as a state machine will be generated for the async method and there maybe extra thread context switching.

I personally would only use asynchronous controllers if the work they are doing is IO bound. If it is CPU bound, you are better of doing it synchronously.

Upvotes: 2

Related Questions