Reprinted from the Blog Garden: http://farb.cnblogs.com/
Today inadvertently see StackOverflow a very good question and answer, personally feel very valuable, so translate come and share with you! I hope we can communicate with each other.
When is asynchronous controller (async Controllers) used in ASP. I have so many concerns when using asynchronous operations in ASP. When did asynchronous operations improve the performance of my application and when did it not improve?
- Is it really good to use asynchronous operations everywhere in ASP.
- For the Awaitable method: Should I use the ASYNC/AWAIT keyword when querying the database (by ef/bhibernate/other ORM)?
- In a single action method, how many times can I use the await keyword asynchronously to query the database?
The asynchronous action method is useful when an action must perform several independent long-running operations.
Let's say I have three operations, which take 500, 600, and 700 milliseconds, respectively. With synchronous invocation, the total response time will be slightly more than 1800 milliseconds. However, if it is an asynchronous call (concurrency), the total response time will be slightly more than 700 milliseconds, because that is the longest task/operation duration.
A classic usage of the asynchronous controller class is for long-running Web service calls.
Should the database be called asynchronously?
The IIS thread pool can often handle more blocking requests than a database server. If the database encounters a bottleneck, asynchronous calls do not speed up the response of the database. Because there is no current-limit mechanism, using asynchronous calls to efficiently distribute more tasks to a overwhelmed database server will only give the database more burden. If your database encounters a bottleneck, the asynchronous call will not be a magic bullet.
Also, async does not imply concurrency. Asynchronous execution frees up a valuable thread pool threads that are blocking external resources without complexity or performance loss. This means that the same IIS machine can handle more concurrent requests than it will run faster.
You can take a look at this article on MSDN. The author spends a lot of effort on this blog to describe when async should be used in ASP instead of how to use it.
Answer title question:
First, to understand that async/await is the root of the release thread. In GUI applications, the GUI thread is primarily released, so the user experience is better. In server applications (including ASP. NET MVC), the request thread is primarily freed, so the server can scale.
In particular, it does not:
Make your personal request complete faster. In fact, they will finish (only a little) more slowly. When an await is encountered, it is returned to the caller/browser. Await will only "yield" to the ASP, not the browser.
Answer question 1:
When you want to do I/O, I can say that the use of asynchronous operations everywhere is good. Although it may not necessarily be beneficial (see below).
However, it is not good to use asynchronous operations for CPU-constrained methods. Sometimes developers think that the benefits of async can be gained by calling Task.run in the controller, which is a scary point. Because that code ends up releasing the request thread by opening another thread, it doesn't benefit at all (in fact, they consume the overhead of extra thread switching).
Answer question 2:
You can use any waiting method that you can take advantage of. If your ORM does not support async, then don't try to wrap it in task.run or anything similar to that.
Note that I said "you can use". If you're talking about a single-database ASP. NET MVC, then (basically OK) you won't get any scalability benefits from Async. This is because IIS can handle more concurrent requests than a single instance of SQL Server (or other classic RDBMS). However, if your backend is a more modern--sql server cluster, Azure SQL, NoSQL and so on-your backend is scalable and the bottleneck of scalability is IIS, then you can get scalability benefits from Async.
Answer question 3:
How many times you love to use, as long as you like. Note, however, that many ORM have the principle of "one operation at a time". In particular, EF allows only one single operation per DbContext, which is true whether the operation is synchronous or asynchronous.
And again, remember the scalability of your backend. If you run into a single instance of SQL Server, and IIS is already able to put SQL Server at full capacity, then SQL Server double or three times times the pressure is no good for you.
See StackOverflow How to answer when to use an asynchronous controller in ASP.