DEV Community

How to Manage Multiple Threads in Node JS

💻 Bleeding Code-By John Jardin on March 31, 2021

In this post I'm going to show you how to potentially triple your Node application's performance by managing multiple threads. This is an importa...
Collapse
 
srikanth597 profile image
srikanth597

Hi
Great article and I already watched your YouTube video some days ago.

Here's my doubt.
According to ur 2nd point , Worker threads/ child process can only process synchronous logic.
But bcrypt module is an asynchronous task
These 2 statements looks to me contradictory

Collapse
 
bleedingcode profile image
💻 Bleeding Code-By John Jardin

Hi there thank you very much for watching and for the comment.

So, with my 2nd point...take note that I used the "bcryptjs", and not the well known "bcrypt" module, which would get offloaded to the libuv thread-pool because it runs on the OS level. "bcryptjs" is purposely designed to be written completely in JavaScript and to be synchronous, because it's actually a security feature to hash passwords synchronously and cause the delay.

I hope that clears up my 2nd point? Below is from their NPM Docs:

"While bcrypt.js is compatible to the C++ bcrypt binding, it is written in pure JavaScript and thus slower (about 30%), effectively reducing the number of iterations that can be processed in an equal time span."

Collapse
 
srikanth597 profile image
srikanth597

yes, it makes all sense now.
Thanks for your efforts,
But I wonder what kind of such huge computational tasks exists especially on DB operations from Node.js programs so that I could use these techniques to improve the performance w.r.t to DB operations

Thread Thread
 
bleedingcode profile image
💻 Bleeding Code-By John Jardin

Always a pleasure. Most of the time it's going to come to advanced business logic that needs to perform a series of processes on returned data from DB or 3rd party queries, etc.

What you always want to be doing is keeping the primary thread and event loop spinning as fast as it can and processing those incoming requests. The moment you have tasks that contain synchronous logic that cause even the slightest of delays, hand it over to Worker Pool 👍.

Collapse
 
febryanph profile image
febryanph • Edited

In my opinion, he write the bcryptHash function using async await statement, it mean the return from that function not a promise again, so the process was changed to synchronous. CMIIW.

Collapse
 
srikanth597 profile image
srikanth597

hmm,
but in my opinion, i think if any function that surrounded by ASYNC statement would always return the promise, no matter if it's really asynchronous or not.

Thread Thread
 
febryanph profile image
febryanph

I think you miss some statement, not offending your opinion but if you write await on the body function that wrapped with async statement, the return will synchronus. You can check on this link javascript.info/async-await#await, with example showAvatar function.

First Picture

Second Picture

I give an example, on the first picture, the variable githubUser using await statement to retrieve the value from fetch, it will return the actual value that send by the server.

On the second picture, the return was Promise that we knew it asynchronous. The value can be fulfilled or rejected and need chained function to process the result.

Thread Thread
 
srikanth597 profile image
srikanth597

I understand your point, it's definitely returning resolved value or rejected value.

Collapse
 
metammodern profile image
Buniamin Shabanov

Hi John! Thank you for your article, it helps me a lot currently. But in TypeScript it seems that the pool variable has no minWorkers/maxWorkers comment. I could not find it in documentation also. Did you use an older version of this library?

Collapse
 
bleedingcode profile image
💻 Bleeding Code-By John Jardin

Hi Bunyamin. I'm glad the article helped 👍. The current version is 6.1.4 and the one I created a video for was 6.1.0, so it's pretty recent.

Also, in their GitHub docs, you will see references to minWorkers and maxWorkers. Strange how it's not available in the TS Interfaces.

github.com/josdejong/workerpool

From Their README:

The following options are available:

minWorkers: number | 'max'. The minimum number of workers that must be initialized and kept available. Setting this to 'max' will create maxWorkers default workers (see below).

maxWorkers: number. The default number of maxWorkers is the number of CPU's minus one. When the number of CPU's could not be determined (for example in older browsers), maxWorkers is set to 3.

Collapse
 
metammodern profile image
Buniamin Shabanov

Yes, I know it is written in docs.I was talking not about the options, but about the data after the pool starts. So, anyway, I found the answer. If someone will have issues like me: The pool data was moved into seperate .stats() method, so you can view all worker number there).
Anyway, thanks John)

Thread Thread
 
bleedingcode profile image
💻 Bleeding Code-By John Jardin

Oh I see. Bleh sorry for misunderstanding your question. I'm glad you came right and this is a great find 👏.