Managing Multiple Threads In Node JS 🧵 | OPTIMIZING NODE JS

  Рет қаралды 23,296

Bleeding Code (By John Jardin)

Bleeding Code (By John Jardin)

3 жыл бұрын

In this 6th video of my "Node JS Performance Optimizations" series, I show you how to handle managing multiple threads in Node JS. We achieve this by using a powerful module called Worker Pool. The video contains important info about what to consider when multithreading your Node application, as well as a live demo of how to implement thread-pool management.
TITLE: MANAGING MULTIPLE THREADS IN NODE JS
Node JS Performance Optimizations KZfaq Playlist:
• Managing The Event Loo...
Subscribe to my Bleeding Code KZfaq Channel here:
/ @bleedingcode
Reference Links
=============
Bleeding Code - Node JS Performance Optimizations GitHub Repo
github.com/bleedingcode/nodej...
Worker Pool Node Module
www.npmjs.com/package/workerpool
AutoCannon Benchmark Testing
github.com/mcollina/autocanno...
Bleeding Code on Social Media
==========================
bleedingcode.com
/ bleedcode
/ bleedingcode

Пікірлер: 62
@BleedingCode
@BleedingCode 3 жыл бұрын
For those already running multithreading in their Node Apps, post a comment below letting us know what tools you use to achieve this 👍.
@MrTejomay
@MrTejomay 3 жыл бұрын
Hey, great content again. Are you on Linkedin?
@BleedingCode
@BleedingCode 3 жыл бұрын
@@MrTejomay Yup. John Vincent Jardin 😎
@Pareshbpatel
@Pareshbpatel 6 ай бұрын
An excellent introduction to managing multiple threads in Node JS using Worker Pool. Thanks. Subscribed! {2024-01-15}, {2024-01-23}
@BleedingCode
@BleedingCode 5 ай бұрын
Awesome. I'm glad you enjoyed it :)
@neljpj
@neljpj 2 жыл бұрын
I really enjoy your videos, I hope you’ll be making more!
@BleedingCode
@BleedingCode 2 жыл бұрын
Thanks Hannes. I appreciate it 👍
@leonk7951
@leonk7951 2 жыл бұрын
i think its really powerful yet very easy to implement. thanks for your video. its really helpful
@BleedingCode
@BleedingCode 2 жыл бұрын
Hi Leon. I'm very glad it helped. Thanks for watching 🙏
@coderllama5302
@coderllama5302 3 жыл бұрын
Amazing!
@BleedingCode
@BleedingCode 3 жыл бұрын
Thank you 🙏
@johnas3
@johnas3 2 жыл бұрын
Thanks for your video, you saved my day 🙏
@BleedingCode
@BleedingCode 2 жыл бұрын
That's great to know 👏
@DAaaBbbIii
@DAaaBbbIii 3 жыл бұрын
John, you're back! ❤️
@BleedingCode
@BleedingCode 3 жыл бұрын
Hahaha yeah it only took like 6 months, but expect more frequent content 💪.
@adigupta857
@adigupta857 3 жыл бұрын
Thanks for this amazing video ❤️❤️❤️, I was looking forward to this. I was trying to scale up my application by using worker threads, I made my own tool which use node native workers and manages them in parallel. But this implementation using workerpool was super cool , thanks 😊..... I want more insightful videos on important domains like this ❤️❤️
@BleedingCode
@BleedingCode 3 жыл бұрын
You are very welcome Adi. Sorry it took so long. Yes I will try my best to continue this series of videos...I do have a lot of related content I want to get going with as well 😎
@kaushikrishi01
@kaushikrishi01 3 жыл бұрын
Could you share the github repo of the implementation brother ? @Adi Gupta
@TechPuzzle_Haven
@TechPuzzle_Haven 3 жыл бұрын
Excellent Videos. Thanks a lot. I hope to see more videos like this.
@BleedingCode
@BleedingCode 3 жыл бұрын
Thanks again Ashutosh. I see you're going through the playlist? 😎
@TechPuzzle_Haven
@TechPuzzle_Haven 2 жыл бұрын
Yes.. I have watched your all videos. Please make more videos on this topics. Thanks you
@johnnyelcoste
@johnnyelcoste 3 жыл бұрын
excellent!
@BleedingCode
@BleedingCode 3 жыл бұрын
Thank you 👍
@ngneerin
@ngneerin 2 жыл бұрын
This is gold
@BleedingCode
@BleedingCode 2 жыл бұрын
Awesome thank you :)
@arunkutz
@arunkutz 3 жыл бұрын
There were some CPU intensive Functions and i was looking into what could be done to speed it up in node. Thank you for the video... It was very helpful ... :)
@BleedingCode
@BleedingCode 3 жыл бұрын
You're very welcome Arun. If you have time, maybe check out the other videos in this series. Some nice gems in there 😎.
@arunkutz
@arunkutz 3 жыл бұрын
@@BleedingCode Yes .. Absolutely ... I have seen the playlist ... Will definitely go through it 😊😊😊😊
@MohamedWaheedAtef
@MohamedWaheedAtef 3 жыл бұрын
Great videos, I wish you the best of luck 😊, I really recommend you zoom in on the code areas and terminal areas in the video, zoom in and out when appropriate because the text is too small on smartphones
@BleedingCode
@BleedingCode 3 жыл бұрын
Hi Mohamed. Thank you so much for the feedback. Ok that's a good point. No one has mentioned that to date. I will make sure to keep that in mind and accommodate mobile devices for future content 👍
@MohamedWaheedAtef
@MohamedWaheedAtef 3 жыл бұрын
@@BleedingCode you are very welcome, best of luck 😊
@smnomad9276
@smnomad9276 2 жыл бұрын
Just discovered your channel and i love it 🔥 I have bots written in Javascript, can multithreading improve their speed?
@BleedingCode
@BleedingCode 2 жыл бұрын
Hi there. Thanks for the compliment :). Yes, multithreading can help in many ways...it's just important to understand that it helps with Userland code (i.e. your actual JS code), not things like TCP requests, I/O related tasks, etc.
@himanshuuniyal2341
@himanshuuniyal2341 2 жыл бұрын
It is very helpful in increasing performance, just had a query how would you suggest creating test cases if we are using the proxy object worker functions, means at the time of running test cases init(responsible for loading workers in pool) will not be there and if we want to stub the object, it will say "trying to stub a property of null", as a proxy will be initializing at server run only.
@BleedingCode
@BleedingCode 2 жыл бұрын
Hi there. Good question, as the same issue occurs when trying to debug logic passed through to Worker Threads. My only suggestion when it comes to debugging or running Unit Tests, is to have a switch that runs the function bypassing the Worker Threads and running on a single thread.
@pra9shinde
@pra9shinde 2 жыл бұрын
Bro create a udemy course for advance nodejs, include all this stuff You are amazing
@BleedingCode
@BleedingCode 2 жыл бұрын
Hi Pranav. Thanks so much for that compliment. I would love to do that in the near future, although right now I'm just trying to find the time to publish my next video 😎.
@soniablanche5672
@soniablanche5672 Жыл бұрын
It's funny because Nodejs's own crypto library runs on a different thread when it's hashing or encrypting something
@udemyaccount4082
@udemyaccount4082 2 жыл бұрын
Holy shit! Hey, I really like the way the controller.get() grabs the worker proxy from that context. It's like static, isn't it? Is Static context an idea in Node.js? Please let me know I am super curious and restless when I don't know these things. Thanks
@BleedingCode
@BleedingCode Жыл бұрын
Sorry for the late reply. I create and hold the controller in memory via a commonjs file. The controller.get() simply grabs hold of that in-memory object. But yes, I'm simulating a static object that's only initiated on load of the app 😎.
@bules12
@bules12 3 жыл бұрын
I really loved your video, I'm a little new in Node and I would like to ask for help I'm trying to have more than 100 nodeJs programs running at the same time. To do that, I'm using clusters, making each worker deploying one express app server and calling it later, creating a lot of servers and calling them The question is: How could I use worker threads or Pools to do this? Cheers :)
@BleedingCode
@BleedingCode 3 жыл бұрын
Hi Bules12. Thank you very much for watching. So, maybe to help me understand better, what is the reasoning behind needing to spawn 100 node processes? How many physical CPUs does the VM or machine have?
@bules12
@bules12 3 жыл бұрын
@@BleedingCode thanks for your answer! I’m doing web scraping, so I need to do a lot of request to various products repeatedly I want to track a lot of products, that’s why I need 100+ node processes I’m using Ryzen 5 5600 (12 logical cores)
@BleedingCode
@BleedingCode 3 жыл бұрын
Hmmm, so if you're looking for concurrency, you might not have to spawn so many processes. It's actually costing you a lot of unnecessary CPU processing and memory usage. Worker Pool is definitely going to help you free up resources by using Worker Threads. I would recommend taking a look at my other videos in my "NodeJS Performance Optimization" playlist, especially the videos titled: "Managing the Event Loop Phases" and "Determining your application baseline performance". The key is to keep the primary thread free to execute incoming requests. If you can achieve this and you have the processing power on the VM, you could run many thousands of requests per second with 1 Node Application.
@bules12
@bules12 3 жыл бұрын
@@BleedingCode that’s truly a dream, I have a lot to study (From NodeJs) If I manage to do it using pools and worker threads successfully I’ll tell you Thanks ❤️
@rage128
@rage128 2 жыл бұрын
Thanks you.. but i have some issue. İ downloaded your code exacly with .env file. minWorkers = 1 ... 8 there is no effect on autocannon avarage response count ? is there any other setting to enable pooling ?
@BleedingCode
@BleedingCode 2 жыл бұрын
Hi there. What is the specs of your machine?
@sudhir-pl6tz
@sudhir-pl6tz Жыл бұрын
Hi am confused between worker_threads and workerpools could you please explain?
@BleedingCode
@BleedingCode Жыл бұрын
Hi there. Sorry for the delayed response. Workerpools strategically use Worker threads behind the scenes to make life easier for devs. If you had to use worker threads manually, you'd have to spend a lot more time and know more about thread management.
@akshay__sood
@akshay__sood 2 жыл бұрын
in which phase of event loop does database queries get executed? I/O polling phase? also does these queries make use of libuv's thread pool?
@BleedingCode
@BleedingCode 2 жыл бұрын
Yes it's I/O and it gets offloaded to Libuv. It's important to understand that a db query is made up of a number of calls, some synchronous, some async.
@akshay__sood
@akshay__sood 2 жыл бұрын
@@BleedingCode in this chain of calls be it sync or async but these calls will get executed in libuv's thread pool right?
@BleedingCode
@BleedingCode 2 жыл бұрын
@@akshay__sood So, this is the thing. It's not grouped. Calls that are meant to run via libuv are transferred to libuv. If calls afterward are synchronous, they are run synchronously.
@akshay__sood
@akshay__sood 2 жыл бұрын
@@BleedingCode gotcha! Talk about it in a video with an example because database queries are primary part of backend engineering. You can talk about it when you revise your Node JS performance series as per your recent poll. Remember to mention my name!😁
@radrianm5
@radrianm5 2 жыл бұрын
Why use this WorkerPool instead of just setting the UV_THREADPOOL_SIZE like in your previous video? Can we implement both?
@BleedingCode
@BleedingCode 2 жыл бұрын
Hi Rinaldi. The WorkerPool (aka Worker Threads) manages userland code (aka the physical JS logic that you write). The Libuv Threadpool manages OS-related tasks. Yes, you can absolutely implement both with no conflicts 👍
@vaibhavm1986
@vaibhavm1986 Жыл бұрын
Hi I am making 5 API calls in and i can see 5 console log at same time how come is possible with 4 threads
@BleedingCode
@BleedingCode Жыл бұрын
Hi there. Can you elaborate the issue? If you're making these calls on your local machine, the speed would be near instant, making it feel like all 5 logs occurred at the same time.
@grijjly6091
@grijjly6091 11 ай бұрын
how can you dare use workerpool package ? , it is fully overhead when functions and parameters sending worker via stringified ? just create your own worker init functions there, just send parameters and wait for message, this video series about optimization, this is disgusting
@BleedingCode
@BleedingCode 5 ай бұрын
Thanks for your honest feedback.
@MrTejomay
@MrTejomay 3 жыл бұрын
Enjoyed it. Are you on Linkedin @bleeding Code?
@BleedingCode
@BleedingCode 3 жыл бұрын
Thank you Tejomay. Yes I am - John Vincent Jardin 👍
Managing The Event Loop Phases ⭕️ | OPTIMIZING NODE JS
23:32
Bleeding Code (By John Jardin)
Рет қаралды 18 М.
How Many Balloons Does It Take To Fly?
00:18
MrBeast
Рет қаралды 201 МЛН
Node.js Worker Threads & PM2 Tutorial
29:12
Michael Guay
Рет қаралды 7 М.
Multitasking in NodeJS: Worker Pool
12:38
Akilesh Rao
Рет қаралды 1,4 М.
Worker threads. Многопоточность в Node.js, тесты.
13:21
Мультипарадигмал
Рет қаралды 6 М.
PROOF JavaScript is a Multi-Threaded language
8:21
Beyond Fireship
Рет қаралды 271 М.
Increase Node JS Performance With Thread Pool Management 📊 | OPTIMIZING NODE JS
12:29
Bleeding Code (By John Jardin)
Рет қаралды 15 М.
Avoiding long running HTTP API requests.
7:24
CodeOpinion
Рет қаралды 14 М.
How to use Multithreading with "worker threads" in Node.js?
19:34
Software Developer Diaries
Рет қаралды 55 М.
Three Concepts Every Node.js Developer Should Understand
39:28
Michael Guay
Рет қаралды 10 М.
Concurrency vs Parallelism in Node.js
7:47
Mehul - Codedamn
Рет қаралды 41 М.
Node.js Tutorial - 61 - Cluster Module
15:10
Codevolution
Рет қаралды 33 М.
Копия iPhone с WildBerries
1:00
Wylsacom
Рет қаралды 8 МЛН
Опасность фирменной зарядки Apple
0:57
SuperCrastan
Рет қаралды 11 МЛН
Частая ошибка геймеров? 😐 Dareu A710X
1:00
Вэйми
Рет қаралды 4,3 МЛН
Лучший браузер!
0:27
Honey Montana
Рет қаралды 977 М.
Какой ноутбук взять для учёбы? #msi #rtx4090 #laptop #юмор #игровой #apple #shorts
0:18