Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use async pattern in Node.js CPU-bound code?

I am using bcrypt in NodeJS to generate password hash. Bcrypt docs says that we can use async version of the genSalt(), compare() and hash() functions.

NodeJS is single-threaded, so theoretically if I use a CPU-bound code will block the thread even using async await. What will change in my application if I use the async await functions in this case? What scenario will CPU-bound codes benefit from using async await pattern?

like image 461
Gustavo Piucco Avatar asked Feb 07 '19 01:02

Gustavo Piucco


People also ask

Why is it important for Node to handle asynchronous code?

With asynchronous programming, we can execute other code while we wait for long activities like network requests to finish. JavaScript code is executed on a single thread within a computer process. Its code is processed synchronously on this thread, with only one instruction run at a time.

Why node JS is not good for CPU-intensive applications?

However, there is a downside to Node. js being single-threaded. The single-threaded implementation makes Node a bad choice for CPU-intensive programs. When a time-consuming task is running in the program it blocks the event loop from moving forward for a longer period.

Why Async is used?

Note: The purpose of async / await is to simplify the syntax necessary to consume promise-based APIs. The behavior of async / await is similar to combining generators and promises. Async functions always return a promise.

How does node js work asynchronously without multithreading?

Node is multithreaded. The main event loop is single-threaded by nature. But the I/O is run on separate threads/processes, because the I/O APIs in Node. js is asynchronous/non-blocking by design, in order to accommodate the singlethreaded event loop.


1 Answers

Node.js is single-threaded in the sense that its main event loop runs on a single thread, but that doesn't mean it is incapable of using worker threads in its standard API for things like I/O and cryptography (both of which do work on separate threads).

For writing 3rd party libraries, such as bcrypt and others, we are able to write C++ add-ons for Node.js that make use of a thread pool provided by libuv, the library backing the event loop in Node.js. And with the introduction of Node.js Worker Threads in v10.5, we are able to write multi-threaded programs without needing to write any C++.

Looking at bcrypt's documentation, they mention that they use a thread pool to avoid blocking main loop:

If you are using bcrypt on a simple script, using the sync mode is perfectly fine. However, if you are using bcrypt on a server, the async mode is recommended. This is because the hashing done by bcrypt is CPU intensive, so the sync version will block the event loop and prevent your application from servicing any other inbound requests or events. The async version uses a thread pool which does not block the main event loop.

like image 115
Christian Santos Avatar answered Sep 28 '22 02:09

Christian Santos