Asynchronous programming is a method of programming that allows more than one task to be executed at the same time. When a task has completed its execution, the main thread is notified to collect the result. Asynchronous programming is a better fit for code that must respond to events – for example, any kind of graphical UI.
In recent years, many technologies such as Node.js that rely on asynchronous programming has shown tremendous growth.
A common misconception
Many developers believe that asynchronous programming improves the performance of an application. But is it really so? The answer is NO. It’s just a common misconception.
It’s true that asynchronous programming allows executing more than one task at the same time. But that doesn’t mean it will improve the performance.
Asynchronous programming neither improves performance nor triggers execution speed.
What is Asynchronous Programming?
Well, If asynchronous programming does not improve the performance of an application, why should we use it?
The answer is simple. An asynchronous program can make the system capable of doing more work increasing the number of requests that can be handled at the same time with the same resources than the synchronous one, thus improving throughput.
Asynchronous programming = Improved throughput.
Let me explain this with the help of an example.
Imagine you go to a restaurant. A waiter comes to your table to take your order and gives it to the kitchen. While the chef is preparing your order, the waiter goes to another table and takes the order and delivers it to the kitchen. So the same person can serve many different tables without waiting for the chef to prepare the first order. This is what we call non-blocking or asynchronous architecture.
What is synchronous programming?
In contrast to non-blocking or asynchronous programming, we have synchronous programming. To understand how synchronous programming works, come back to our example. Imagine a dedicated waiter was allocated for you. He will now take your order, deliver it to the kitchen, and wait there for the chef to prepare your food. During this time, the waiter is not doing anything.
This is how the synchronous program works. When you make a request or have a task to run, a thread is allocated to handle it. If the response to your request involves something time-consuming like an I/O operation or a database query, the thread remains idle until the operation is complete. Similarly, a new thread is allocated to a new request or task.
If there is a large number of concurrent tasks, the program may run out of threads, putting new tasks to wait until a thread is available.
But in asynchronous programming, a single thread will handle several tasks without waiting for the result. When the result is ready, the thread will collect the result and present it to you.
So we can say that the asynchronous method results in better utilization of the available resources.
Asynchronous != Multithreading
Asynchronous programming is not the same as multithreading. It is the ability of a central processing unit (CPU) to provide multiple threads of execution concurrently.
In other words, work is done on totally separate threads, with no interaction between individual threads.
Asynchronous != Multithreading
When to use asynchronous programming
Asynchronous programming should be used only where there is a need to perform time-consuming I/O bound operations. There is no need to use asynchronous programming for performing CPU intensive operations.
When to use synchronous programming
If your application does not have do perform I/O bound or other time-consuming operations that involve waiting, asynchronous programming can do nothing beneficial.
Operations such as video rendering or mathematical calculations are completely dependent on the CPU and its computational capability. In such cases, if you use asynchronous programming, it may result in high CPU usage.
Wrapping it up
Using asynchronous programming makes the system capable of handling more requests at the same time, increasing the throughput.