Increase Node JS Performance With Thread Pool Management 📊 | OPTIMIZING NODE JS

  Рет қаралды 15,348

Bleeding Code (By John Jardin)

Bleeding Code (By John Jardin)

3 жыл бұрын

In this 5th video of my "Node JS Performance Optimizations" series, I show you how to increase app performance with thread pool management. We achieve this by understanding how Libuv works, how the thread pool works and how to configure the number of threads based on your machine specs.
TITLE: INCREASE NODE JS PERFORMANCE WITH THREAD POOL MANAGEMENT
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...
AutoCannon Benchmark Testing
github.com/mcollina/autocanno...
Libuv
libuv.org
Bleeding Code on Social Media
==========================
bleedingcode.com
/ bleedcode
/ bleedingcode

Пікірлер: 89
@BleedingCode
@BleedingCode 3 жыл бұрын
Hi coders 👋. I'm curious...who of you knew about this Libuv Thread Pool setting? Cheers 😎
@LawZist
@LawZist 3 жыл бұрын
Not enough
@Omer23007
@Omer23007 Жыл бұрын
WOW
@aminazgol3918
@aminazgol3918 5 ай бұрын
wonderful, very clean easy, and well-structured explanation. thank you sir
@BleedingCode
@BleedingCode 4 ай бұрын
Glad it was helpful :)
@juraev0056
@juraev0056 3 жыл бұрын
Please keep doing this kind of content. I am tired of beginner stuff already
@BleedingCode
@BleedingCode 3 жыл бұрын
Thanks for the comment. That's definitely my intention for many of my videos to come 👍.
@markmiller8571
@markmiller8571 3 жыл бұрын
Wow, that is amazing information and a great tutorial! Thanks so much!!
@BleedingCode
@BleedingCode 3 жыл бұрын
Glad you enjoyed it 👏
@reanibutani9555
@reanibutani9555 3 жыл бұрын
I do have enjoyed the video, thank you!!! I am just starting in the nodejs path, and this video is really helpful
@BleedingCode
@BleedingCode 3 жыл бұрын
Thank you Rea. Glad you enjoyed it 👍👍
@arunavachakraborty5557
@arunavachakraborty5557 2 жыл бұрын
Brilliant ! Subscribed ! Keep going ! :)
@BleedingCode
@BleedingCode 2 жыл бұрын
Thank you 🙏
@kid_kulafu_1727
@kid_kulafu_1727 3 жыл бұрын
I subscribed in a heartbeat.
@BleedingCode
@BleedingCode 3 жыл бұрын
Many thanks Raphael 🙏
@LawZist
@LawZist 3 жыл бұрын
This is really really good stuff!
@BleedingCode
@BleedingCode 3 жыл бұрын
Many thanks 👍
@jovert2445
@jovert2445 3 жыл бұрын
You deserve more subscribers, keep it up 🍻
@BleedingCode
@BleedingCode 3 жыл бұрын
Thank you Jovert for this awesome comment 🙏
@DR-ee4wv
@DR-ee4wv 2 жыл бұрын
just got subscribed for this video ❤️❤️❤️❤️❤️
@BleedingCode
@BleedingCode 2 жыл бұрын
Thank you very much 🙏
@AkshayJaiswal
@AkshayJaiswal 2 жыл бұрын
This is exactly i was looking for. Thank you so much this turorial was very helpfull.
@BleedingCode
@BleedingCode 2 жыл бұрын
You're very welcome :)
@AkshayJaiswal
@AkshayJaiswal 2 жыл бұрын
@@BleedingCode I had one question. If i use web worker in my node project will that effect thread pool?
@BleedingCode
@BleedingCode 2 жыл бұрын
@@AkshayJaiswal It shouldn't. The Libuv Threadpool manages async IO tasks, where Worker Threads and, to my understanding, web workers, focus on userland code (i.e. your actual JS logic)
@aliarslanansari
@aliarslanansari 3 жыл бұрын
Amazing tutorial 🤩
@BleedingCode
@BleedingCode 3 жыл бұрын
Thank you Ali. I really appreciate it 👍
@grijjly6091
@grijjly6091 10 ай бұрын
i wanna say something here, if your server doing io things instead high cpu jobs, you can set threadpool size something 256-512-1024, its because reading or writing file is blocking operation and not cpu insentetive which mean , you can handle more client but context change overhead which is ignorable for file operations
@ahlambeyoud1709
@ahlambeyoud1709 3 жыл бұрын
Thank you very much, very clear explanation great content, I hope you make a video about query optimization as well :)
@BleedingCode
@BleedingCode 3 жыл бұрын
Thank you very much Julia. Regarding Query Optimization, are you referring to MongoDB queries, or something else? Cheers
@ahlambeyoud1709
@ahlambeyoud1709 3 жыл бұрын
MongoDB :)
@BleedingCode
@BleedingCode 3 жыл бұрын
@@ahlambeyoud1709 Awesome. I’ve got some great stuff for Mongo. Need to get 2 more videos on NodeJS Performance Optimization out the way. I’ll then start diversifying. Stay tuned 😎
@ahlambeyoud1709
@ahlambeyoud1709 3 жыл бұрын
@@BleedingCode I am very interested in the topic of node optimization as well I am really looking forward to that, I am a bit confused on how to use clusters and redis cashing and pm2 how do we use them all together? can we use them all together? I hope your content will help, I am really looking forward to it :) thank you
@snoblik69
@snoblik69 3 ай бұрын
Hi, very well explained, thank you! I have a question about the v8-pool-size=num parameter. When it is good to increase the value of UV_THREADPOOL_SIZE according to the number of logical cores, what value should the parameter V8-POOL-SIZE be? Thanks in advance for the recommendation. I can't find a relevant, factually supported comment on this anywhere on the internet.
@AndrosAndWindows
@AndrosAndWindows 3 жыл бұрын
Subscribed Loved the video and also can you please please make a video on how to install Libuv and how to use it in production ?
@BleedingCode
@BleedingCode 3 жыл бұрын
Thank you very much Ramsai. Tell me, do you mean installing libuv by itself? Because it comes with NodeJS. It doesn't need to be installed separately unless you want to develop apps outside the scope of NodeJS but using libuv.
@AndrosAndWindows
@AndrosAndWindows 3 жыл бұрын
@@BleedingCode Thanks you so much I don't know it comes with Nodejs . So now I can use Libuv in nodejs itself how you instructed in the video ?
@inclinedscorpio
@inclinedscorpio 2 жыл бұрын
Hi There! Thanks for this video, I have a doubt! I want to use the dynamic way of using multithreading- I have dockerised my application- handled by Kubernetes, currently using VM's to manage the K8 node. Do you see any potential problem which could occur due to using dynamic way? I couldn't understand how to give it through env or CMD as using software on different VM and different Machine will require to understand core everytime so I was hoping to use the dynamic way! Your thoughts? :)
@BleedingCode
@BleedingCode 2 жыл бұрын
Hi there. Thanks for the feedback. There are no problems using the dynamic way. I also use Docker and deploy to k8s clusters. The most important is that the ENV is set as early as possible and not changed again 👍
@inclinedscorpio
@inclinedscorpio 2 жыл бұрын
@@BleedingCode Thanks ❤️
@matthew78uk
@matthew78uk Жыл бұрын
Hi John, probably a silly question: you mentioned that increasing the thread pool size above the number of the logical CPU would not improve the results in the benchmark, but could it make it worse? If not wouldn't it make sense to just set the thread pool to 1024 so that all the logical cpus would be used by default?
@BleedingCode
@BleedingCode Жыл бұрын
Hi Matteo. Great question. The problem is that NodeJS will create 1024 threads, which will then be shared between all logical CPUs. It doesn't make sense, but it's what will happen. This is will have a negative effect on the logical CPUs.
@ashutoshtiwari4258
@ashutoshtiwari4258 2 жыл бұрын
Hi There ! Thanks for the video 🤟🏼 I have a doubt. In my Nodejs application (connected to DB) .... Requests is processed and returned within 100ms but in UI it shows it took 600ms - 1.5sec, what can be the reason for this ? I am getting around 100 requests per second apart from that request... Is there any way I can debug it?
@BleedingCode
@BleedingCode 2 жыл бұрын
Hi there. You're very welcome for the video. Regarding the UI response being different, is it not including the download time of the response data? Postman is a great tool to check exactly where the time is going in the real world.
@MrBrainStarX
@MrBrainStarX 3 жыл бұрын
Hey man, first of all thx for the video. I have a question about that topic: I am currently using the cluster module for running a processes on each cpu(in paralell), can I still use the thread pool management in addition ?
@BleedingCode
@BleedingCode 3 жыл бұрын
You are very welcome. Yes you can still use thread-pool management and manage how many threads will spawn instead of the default 4. However, these will be duplicated per process, because child processes or clustering (which uses child processes), does not share memory, hence it will be difficult for one libuv threadpool to interact with multiple node processes. Make sense?
@MrBrainStarX
@MrBrainStarX 3 жыл бұрын
@@BleedingCode I do understand, thank you very much.
@sakshamsrivastava6280
@sakshamsrivastava6280 3 жыл бұрын
@@MrBrainStarX Hey, can you please help me out with the explaination if you understood ? should we use it with it or not
@MrBrainStarX
@MrBrainStarX 3 жыл бұрын
@@sakshamsrivastava6280 Hey, it's up to you. But you can use it without a problem, at least I got no errors. So you can run multiple threads per process.
@blameItleaveit
@blameItleaveit 3 жыл бұрын
If you provide all of your availabile threads to nodejs, what about other proces that requires thread? Generally there are multiple process running which are hungry for threads.
@BleedingCode
@BleedingCode 3 жыл бұрын
Hi Dev. Thanks for commenting. Are you referring to other Node JS processes or just any process running on the VM or machine? Running of your Node app and the threads that are created happens automatically and is scheduled by the OS. It's still preferred to have access to all CPUs instead of just one, even if the other CPUs are busy. Remember it's the main thread we want to primarily keep active and performant.
@aminazgol3918
@aminazgol3918 5 ай бұрын
does dynamic configuration work as intended on a docker image?
@BleedingCode
@BleedingCode 4 ай бұрын
Yes it does. I use docker containers for most of my NodeJS Apps 👍.
@uditsoni24
@uditsoni24 2 жыл бұрын
Hi John, I have implemented Nodejs clustering logic in my application. in my machine there are 16 Logical cores, 8 physical cores, that means there will be 16 servers of my application, what would be the best, in this case, should I set thread pool size to 1 or 16? If we set it to 16 it's then each node server will have 16 thread pool size as I am having 16 servers and each server will have 16 thread pool size so there are gonna be 256 threads. As you said it should be equal to the Logical CPU, Could you please tell me what should be the number of thread pool if there is clustering implemented?
@BleedingCode
@BleedingCode 2 жыл бұрын
Hi Udit. Great question and you are absolutely right. Because you are running 16 nodejs runtimes, it's going to count to 256 threadpools, which won't make any sense. Have you seen my video on managing multiple threads using 1 runtime? Is this an option for you?
@habutf
@habutf Жыл бұрын
Pretty sur @BleedingCodes CPU also has more logical cores than he states in the video.
@habutf
@habutf Жыл бұрын
Ah... it comes a minute later...
@TheSiddhaartha
@TheSiddhaartha 2 жыл бұрын
If I have to do simple writes to a NoSQL DB like Cassandra/MongoDB, then is Node js faster than Java? The number of requests per second are around 1.4 million. Thanks!
@BleedingCode
@BleedingCode 2 жыл бұрын
Hi Siddharth. Thanks for the comment. Unfortunately this is not a simple answer and depends greatly on infrastructure, optimization of the statements/queries, size of the tables/collections and indexes, and a number of other factors 😳
@TheSiddhaartha
@TheSiddhaartha 2 жыл бұрын
@@BleedingCode do you think I can start with node js? I am not using nosql anymore as it's relational data. So going to decide on AWS Kinesis or similar solution.
@BleedingCode
@BleedingCode 2 жыл бұрын
@@TheSiddhaartha Yes you can absolutely. NodeJS is powerful enough to do the job, but again...many many factors at play :)
@rage128
@rage128 2 жыл бұрын
i download your code exacly but there is no effect on avarage request count by changing UV_THREADPOOL_SIZE ; what im doing wrong ?
@BleedingCode
@BleedingCode 2 жыл бұрын
Hmm. Seems to be the same issue to what you're having with the worker threads? Again, can you confirm your machine specs?
@anujgupta7152
@anujgupta7152 2 жыл бұрын
Will pm2 do the same stuff?
@BleedingCode
@BleedingCode 2 жыл бұрын
Hi Anuj. Sorry for the late reply. Honestly I'm not too sure, because PM2 manages multiple instances of your Node application using either clustering or child processes. So in theory your primary Node instance is where you declare the libuv Threadpool size. Sorry, I don't use PM2 that much as I primarily run my apps in Kubernetes.
@lakshmankashyap4690
@lakshmankashyap4690 2 жыл бұрын
If I using pm2 on server so is it necessary to implement workthread in high performance task in nodejs
@BleedingCode
@BleedingCode 2 жыл бұрын
Hi there. It depends on how you've set up clustering using PM2. If you already have clustering running for your runtime, then leave it be.
@lakshmankashyap4690
@lakshmankashyap4690 2 жыл бұрын
@@BleedingCode thanks i think I found with the help of your video after testing with autocannon and work pool then realize if pm2 start -i max then cluster run so worker-poll and pm2cluster , nodejs with cluster master salve are same right?
@BleedingCode
@BleedingCode 2 жыл бұрын
@@lakshmankashyap4690 Cluster and Child Processes are actually the same...cluster just uses child processes differently. WorkerPool is different, as it uses Worker Threads and doesn't spawn child processes. This is what makes Worker Threads awesome.
@Dev_pig
@Dev_pig 2 жыл бұрын
What if i run the application on k8s as a container?
@BleedingCode
@BleedingCode 2 жыл бұрын
It will work just fine. Remember, the container runs against the underlying resources that have been assigned to K8S.
@Dev_pig
@Dev_pig 2 жыл бұрын
@@BleedingCode thanks, however on k8s we have min cpu and max cpu config. For example, min cpu 1 core, max cpu 6 core, then what is the os.cpu.length ..
@BleedingCode
@BleedingCode 2 жыл бұрын
@@Dev_pig Hi there. I can't really answer that off the bat, but could you not add a console.log to your app to see what it's returning in terms of logical CPUs?
@Dev_pig
@Dev_pig 2 жыл бұрын
@@BleedingCode I found the answer. on k8s container, min cpu (requests.cpu) is used for choosing right node, and max cpu (limits.cpu) is what the application uses. So, if I use 8 as limits.cpu config, then application can use 8 cores from the beginning Thank u for letting me know many things :)
@BleedingCode
@BleedingCode Жыл бұрын
@@Dev_pig Brilliant...good catch 👏
@ngneerin
@ngneerin 2 жыл бұрын
Font size too small
@BleedingCode
@BleedingCode 2 жыл бұрын
Sorry about that. Didn't realize it at the time, but my future videos will support mobile much better, which will resolve the font size 👍
Managing Multiple Threads In Node JS 🧵 | OPTIMIZING NODE JS
9:32
Bleeding Code (By John Jardin)
Рет қаралды 23 М.
How To Determine Application Baseline Performance In Node JS 🔬 | OPTIMIZING NODE JS
18:41
KINDNESS ALWAYS COME BACK
00:59
dednahype
Рет қаралды 136 МЛН
Nutella bro sis family Challenge 😋
00:31
Mr. Clabik
Рет қаралды 12 МЛН
That's how money comes into our family
00:14
Mamasoboliha
Рет қаралды 9 МЛН
Mastering Memory Management in Node.js: Tips and Tricks for Better Performance
16:06
Software Developer Diaries
Рет қаралды 28 М.
PROOF JavaScript is a Multi-Threaded language
8:21
Beyond Fireship
Рет қаралды 267 М.
Don't Use Websockets (Until You Try This…)
6:46
Code With Ryan
Рет қаралды 288 М.
Why should not use EventEmitter for Asynchronous Operations
19:11
Adventures in Nodeland - Matteo Collina
Рет қаралды 4,1 М.
Node's Event Loop From the Inside Out by Sam Roberts, IBM
23:02
Node.js performance analytics and best practices | Sebastian Springer
1:04:00
International JavaScript Conference
Рет қаралды 8 М.
I tried 8 different Postgres ORMs
9:46
Beyond Fireship
Рет қаралды 391 М.
Tag her 🤭💞 #miniphone #smartphone #iphone #samsung #fyp
0:11
Pockify™
Рет қаралды 35 МЛН
Хотела заскамить на Айфон!😱📱(@gertieinar)
0:21
Взрывная История
Рет қаралды 6 МЛН
Красиво, но телефон жаль
0:32
Бесполезные Новости
Рет қаралды 350 М.