How to correctly use asynchronous programming technology and asynchronous programming technology

Source: Internet
Author: User
Tags website performance

How to correctly use asynchronous programming technology and asynchronous programming technology
1. What is synchronous and asynchronous?

SynchronizationSynchronization refers to coordination between events in a system, and consistency and unification occurs in time. To put it bluntly, multiple tasks are executed one by one, and only one task is executed at a time.

AsynchronousAsynchronization: enables the CPU to temporarily hold the response of the current request and process the next request. After receiving a callback notification through polling or other means, it starts to run. Multithreading puts asynchronous operations into another thread for running. The notification is sent through polling or callback. However, the operating system takes over Asynchronous Operation Scheduling and hardware interruption, the callback method is triggered upon completion. This method does not require additional threads.

Ii. When should I use an asynchronous controller in the ASP. NET MVC project?

2.1. Why does ASP. net mvc need to be asynchronous?

IIS has a thread pool to process user requests. When a new request comes, the thread in the scheduling pool is used to process the request. However, when the concurrency is high, when the threads in the Pool cannot meet so many requests, when every thread in the pool is busy, the request processing thread will be blocked, the thread cannot provide services to another request. If the request queue is full, the Web server rejects the request and is in the HTTP 503 busy state. If it is to handle some high latency, such as network operations, most of such threads only wait for the status for most of the time and do nothing, such threads can use asynchronous programming to make full use of it.

Iii. synchronous and asynchronous use cases

Scenario 1:If a request generates a network call that takes two seconds, it takes two seconds for both synchronous and asynchronous requests. However, during asynchronous calls, the server does not block responses to other requests while waiting for the completion of the first request. Therefore, when many requests call long-running operations, asynchronous requests can prevent requests from queuing.

Scenario 2:Suppose I have three operations, which take 500,600 milliseconds and 700 milliseconds respectively. If synchronous call is used, the total response time will be slightly more than 1800 milliseconds. However, for asynchronous calls (concurrency), the total response time will slightly exceed 700 milliseconds, because it is the longest task/operation duration. Therefore, when an action must execute multiple independent long-term operations, the asynchronous action method is very useful.

3.1 Use a synchronization pipeline when the following conditions are met:

1) The operation is very simple or the running time is very short.

2) Simplicity is more important than efficiency.

3) this operation mainly involves CPU operations rather than operations that contain a large amount of disk or network overhead. The use of asynchronous methods for CPU binding does not provide any benefits and lead to more overhead.

3.2 use an asynchronous pipeline when the following conditions are met:

1) The operation is network bound or I/O bound, not CPU bound.

2) the test shows that blocking operations are a bottleneck on website performance. IIS can provide services for more requests by using asynchronous operation methods for these blocking calls.

Parallelism is more important than code simplicity.

3) You want to provide a mechanism for users to cancel long-running requests.

Iv. Q &

4.1 since Asynchronization can greatly provide application response capabilities? If ASP. net mvc uses all asynchronous controllers (AsyncController), What will happen? Will it become a high-throughput, high-concurrency website?

Simply adding an async to the code will not actually improve the performance. It is necessary to Implement Asynchronous local (IO) asynchronous execution to truly increase the throughput. Asynchronous Controller is mostly used for I/O-intensive operations, such as reading and writing data, and operations are relatively independent. CPU-intensive operations are not applicable to Asynchronization-whether asynchronous processing or synchronous processing, eventually, the CPU is full. So asynchronous operations can indeed improve the number of concurrent operations, but it depends on where you use it. Using asynchronous Controller does not definitely improve the performance of the site.

Vi. References

How does stackoverflow tell you when to use an asynchronous controller in ASP. NET?

Use asynchronous controllers in ASP. NET MVC

When does ASP. net mvc use Asynchronous Action?

If ASP. net mvc uses asynchronous Controller, what will happen? Will it become a high-throughput, high-concurrency website?


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.