What Is The Difference Between NodeJS Sync(Synchronous) vs ASync (Asynchronous)

If the execution of a program takes place in a linear sequence, it is synchronous processing. For example, as you can see in the code block below, an entire line will be read and executed before the process proceeds to the next line.

This process works properly with only one request. In the case of multiple requests, then you have to wait for the previous request to finish. This can be as interesting as watching a filling a glass of water. To overcome this, you can handle the request asynchronously. This way, you will push any process that is taking too long from the execution stack to the background so that other code can be executed. However, Once the background work is done, the program will be pushed back again to the execution stack and processed further

Request Processing

Node.js handles a library named llibuv. This libary handles asynchronous I/O pretty well. However, Instead of spawning multiple threads for each request, it manages a thread pool very efficiently with the help of the OS kernel. Then as soon as a new request lands on a Node.js server, it delegates most of the work to other system workers. As the background process and finish their job, they emit events to Node.js callbacks registered on that event. See below the diagram.

Node.js event loop architecture

Node.js is greatly faster than multithreaded systems, even with a single thread. Hence, Node.js uses event loop architecture with thread pool management, which makes it powerful and faster.

Leave a Reply

Your email address will not be published. Required fields are marked *