How ASP. NET MVC correctly uses asynchronous programming technology

Source: Internet
Author: User

First, what is synchronous and asynchronous?

Synchronization (English: synchronization), which refers to the coordination of events occurring in a system and the consistency and unification of time. To be blunt is that multiple tasks are performed one at a time and only one task is executing at the same moment.

Asynchronous (English: asynchronization), which means that the CPU temporarily holds the response of the current request, processes the next request, and starts running when the callback notification is received by polling or otherwise. Multithreading takes an asynchronous operation into another thread, completes the notification through a polling or callback method, but completes the port, takes over the dispatch of an asynchronous operation by the operating system, and, through a hardware interrupt, triggers a callback method at completion, which does not require an additional thread to be consumed.

Ii. when should we use an asynchronous controller in an ASP. NET MVC project?

2.1. Why is it necessary to use async in ASP.

IIS has a thread pool to handle the user's request, and when a new request comes over, the thread in the pool is dispatched to process the request, however, but with high concurrency, the threads in the pool are not able to meet so many requests at the time. Each thread in the pool is in a busy state when the request is processed and the thread that processes the request is blocked, and the thread cannot service another request, and if the request queue is full, the WEB server rejects the request and is in an HTTP 503 busy state. If you are dealing with some high latency, such as a network operation, most of these threads just wait for the state to do nothing for most of the time, so that the thread can use asynchronous programming to make better use of it.

Three, synchronous and asynchronous usage scenarios

Scenario Description: If a request generates a network call that takes two seconds to complete, it takes two seconds for the request to execute synchronously or asynchronously. However, during an asynchronous call, the server does not block the response to other requests while waiting for the first request to complete. Therefore, when there are many requests to invoke a long-running operation, an asynchronous request can prevent the request from queuing up.

Scenario Two: Suppose 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. Therefore: The asynchronous action method is useful when an action must perform several independent long-running operations.

3.1. Use a synchronous pipeline when the following conditions are true:

1), operation is very simple or short running time.

2), simplicity is more important than efficiency.

3), this operation is primarily a CPU operation rather than an operation that contains a large amount of disk or network overhead. The use of asynchronous operation methods on CPU bound operations does not provide any benefit and also results in more overhead.



3.2. Use an asynchronous pipeline when the following conditions are true:

1), the operation is network bound or I/O bound instead of CPU bound.

2), test shows that blocking operations are a bottleneck for site performance and that IIS can service more requests by using asynchronous action methods for these blocking calls.

Parallelism is more important than the simplicity of the code.

3), you want to provide a mechanism that allows users to cancel long-running requests.



Iv. Q&a Links

4.1. Since async can greatly provide application responsiveness? So what would be the effect of ASP. NET MVC If it was all used with an async controller? Will it become a high-throughput, high-concurrency site?

The

Simply adding an async to the code does not actually bring any performance gains, and it must be executed asynchronously (IO) to really increase throughput. Asynchronous controllers are mostly used for I/O intensive operations, such as reading and writing data, and the operations are more independent, while CPU-intensive operations are not applicable to asynchrony-whether you are asynchronous or synchronous, the final CPU will be full. So an asynchronous operation can actually achieve the effect of increasing the number of concurrent numbers, but it depends on where you use it. All use of asynchronous controllers does not absolutely improve site performance.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.