Google is playing a joke on me. I spent a month fiddling with the first topic, with the promise of all cycles. I just solved the problem and RUclips immediately suggested your video...
An amazing oportunity to use os.cpus()!! + import os from "os"; ... - const THREAD_COUNT = os.cpus().length; and reduce as well! - const total = thread_results.reduce((x, y) => x + y); Thanks for the video! :3
Technically, node does not use threads for network operations. It uses different mechanism called async io. But the idea is similar - main thread is also not blocked.
Question: What if I will be using "docker + kubernetes" to horizontally scale my node app and at the same time i have a file upload + file processing intensive task in my app should I still use "worker threads" to manage the intensive tasks or "docker+kubernetes" will solve the problem anyway ? Thank you for this amazing explanation !
I has a question that when we use Multithreading with "worker threads" or real applications of it. I have saw many Nodes courses and they just introduce about it, not talk about the real application
awesome video but I have a question, can you elaborate on the difference between background jobs and worker threads. I have a situation in which I need to call db recursively then call third-party API which takes some time to respond then refactor data then filter specific data and again call third-party API and then bulk insert into db + I need to perform this action inside cron jobs which get triggered after every 3 hours so what should I need to use???? and I'm using ec2 which has only 1 core so I think I can't divide it if my solution is worker thread
Thank you! :) Sounds like you need a queueing mechanism for such a bulky operation. Take a look at RabbitMQ or Apache Kafka and how you can integrate them with your existing API.
Though, one thing I could not digest, 20M loops by one thread Vs 4 threads counting 5 M loops simultaneously / parallelly :-> 1. addition of their count will be 5M + 5M + 5M + 5M = 20 M loops for sure as mathematically it is correct. But, As these 4 threads are working parallelly time taken by them is almost equal which is of counting only 5M loops. Diagrammatically, 1 Thread ---------------- 20 M loops ---------------------> 12 Sec (let's say) 1/4 Thread --------5M loops ------------> 3 Sec (let's say) 2/4 Thread --------5M loops ------------> 3 Sec 3/4 Thread --------5M loops ------------> 3 Sec 4/4 Thread --------5M loops ------------> 3 Sec Here, we simply reduced our task to count form 20 M to 5 M, hence time taken describes to count 5M only not 20M.
Are they counting like 1st Thread => 1M ---> 5M , 2nd Thread => 6M ---> 10 M , 3rd Thread => 11M ----> 15 M, 4th Thread => 16M ---> 20M .... ??? No , all threads are counting form 1 to 5M and parallelly hence time taken describes to count 5M only not 20M
The machine will have to launch a fair amount of worker threads and get noticeably slow, before it throws an error that it can't launch a new thread. But this is probably the sequence that you'd see.
That's a great question! In a Kubernetes environment, each pod typically runs a single container, and the number of CPUs available to a pod depends on the resources allocated to it. If a pod has only one CPU allocated, then indeed, multithreading might not provide significant benefits since there's only one CPU core available for processing.
I have a question, If I cluster node js app on 8 cors and if I use thread on each computation in my code, So assumption is my same code is running on 8 clusters and all have threads in them mean to say the api I wrote in my single code is running on 8 different cors, does it improve performance and if it improve performance? Can you tell me pros and cons of it?
Hi , one question is like I have scenario in which have to do file operation many times we are using some third party library to club or combine small doc files to one big final pdf , but that has to be in particular order , there are 20 sections , each section is tree containing further sections and so on and so forth ,each section contains docx file that has to be merged, so can I divide workers to club 20/ thread-count and then finally club the result of all of them to final file, only thing is I would have to find thread count , programmatically do you agree? is there any other way you can recommend
Hi, I did not understand the idea of 7 threads , kindly correct me If this is wrong from what I know is UV_THREADPOOL_SIZE = 4 , by default and these are the only threads used by libuv for some of the operations! i.e except CPU intensive tasks
Correct. When we refer to a "thread", we actually mean a "thread pool". With UV_THREADPOOL_SIZE you define the size of the pool, which by default consists of 4 "child threads". You can still increase the size of the pool up to 1024.
my cloud setup is having 7 nodes each node has 2 vcpu , virtual CPU I am not sure how many threads each instance has , I printed cpus length using os module it printed 2 , so only 2 threads ? do you suggest any other way or to know about it
@@vaibhavm1986 It can't, the UV_THREADPOOL_SIZE can't be bigger than the number of total logical threads on your CPU. If you have an Octa-Core CPU each of them have 2 threads, then the logical threads at your disposal will be 8 * 2. It can't be more. To automate the code, you can use - const os = require("os") process.env.UV_THREADPOOL_SIZE = os.cpus().length Also, the threads used in the worker pool are not real threads. They are circumventive solution for multithreading. Possibly very close to the Co-routines of Kotlin language. Or GoRoutines from Go lang. In kotlin, you can have thousands of coroutines with multiple tasks. If you are looking for multithreading, you can use cluster module from node.js, which spawns many worker instances on each cpu thread, but yeah they are still memory intensive and mixing worker_thread with clustering is bad idea.
Great video! but most likely you work in a company that uses containerized environment (unless you live under a rock) and if thats the case - dont bother, just put 1.25 cpu requests and add more replicas! its safer and much more scalable, also in containers environment you cant trust the core count since it works on CPU time
I have 3.5 years of Experience as a Node js Developer. But I feel I knew nothing. Thank you so much man. This is mind blowing. Love every of it
This is gold! I have been binge watching your channel. I wish I had discovered your channel much before.
You can’t imagine how much this comment means to me!
The most intuitive video of multithreading nodejs out there
This is the best video I have found that teaches how to use multithreading in Node
Happy to hear that!
Facts
Awesome content, Thanks for the creating video Multi Threading. Explanation was too smooth, and especially loved the optimization part.
Glad you liked it!
Great video! Excited for this whole series!
Best video I have ever seen on node multithreading👍
Wow, thanks!
Google is playing a joke on me. I spent a month fiddling with the first topic, with the promise of all cycles. I just solved the problem and RUclips immediately suggested your video...
Great content, i hope you to do more videos with advanced concepts and proyects with nodejs, best regards from Argentina
Gracias amigo! More to come 😉
An amazing oportunity to use os.cpus()!!
+ import os from "os";
...
- const THREAD_COUNT = os.cpus().length;
and reduce as well!
- const total = thread_results.reduce((x, y) => x + y);
Thanks for the video! :3
const { cpus } = require("os");
const THREAD_COUNT = cpus().length;
maybe better option
This man is too good.
Wow This is amazing. very well explained as well. I am gonna use it where ever possible. Thank you for making this video. loved it. 😍😍
This playlist is awesome! Thank you!
An excellent tutorial on Multithreading in Node using Worker Threads. Thank you very much.
{2024-01-12}, {2024-01-15}, {2024-01-19}.{2024-11-20}
Really nice guide! Thanks 🙌🏻
Нема за що, glad you enjoyed it 👍
Deym, I thought i'm on my limits for optimizing my api, Thank you so much for this
Super helpful. Thank you so much!
Happy to hear, buddy!
You solved a great problem. Thanks a lot
thank you for sharing this was so helpful
Sure thing 🙂
bzhy :d
great work , explanation at its best..!
Thanks mate!
Please do more stuffs like this💥
Awesome tutorial
great job man!
Excellent explanation, thank you
Glad it was helpful!
Very well done!
Thank you!
Thank you brother
great content!
God, it's super helpful. Please make more videos about APIs optimization and security for Nodejs, pleaseeeeeeeeeeeeeeeeeeeeeeeeeee
Thanks a million! 😊
as a fresher, mern stack developer..should i learn this playlist?
Imagine combining multithreading with clustering? That would be a bomb 💣
Hmmm interesting!!
hello brother. Can we use this to handle 2-3 different tasks at the same time? I subscribed your channel ;)
Yes, you totally can :) Thanks for the subscription!
It's very cool, but how to understand how many threads will you need? Just check it manually?
okay, understood, thanks for nice tip
Technically, node does not use threads for network operations. It uses different mechanism called async io. But the idea is similar - main thread is also not blocked.
Hello. why do you need the number of cores? If I understand it correctly, the new loop is located in the same process but on a different thread.
Sir Which theme you are using? It's cool for the eyes.
Hey mate, it's called "Palenight (Mild Contrast)"
I think while creating worker threads , new main thread is not created. Correct me if I am wrong.
Question: What if I will be using "docker + kubernetes" to horizontally scale my node app and at the same time i have a file upload + file processing intensive task in my app should I still use "worker threads" to manage the intensive tasks or "docker+kubernetes" will solve the problem anyway ?
Thank you for this amazing explanation !
why nt use both
Great video
Thanks!
Great bro
I has a question that when we use Multithreading with "worker threads" or real applications of it. I have saw many Nodes courses and they just introduce about it, not talk about the real application
awesome video
but I have a question, can you elaborate on the difference between background jobs and worker threads. I have a situation in which I need to call db recursively then call third-party API which takes some time to respond then refactor data then filter specific data and again call third-party API and then bulk insert into db
+ I need to perform this action inside cron jobs which get triggered after every 3 hours
so what should I need to use???? and I'm using ec2 which has only 1 core so I think I can't divide it if my solution is worker thread
Thank you! :)
Sounds like you need a queueing mechanism for such a bulky operation. Take a look at RabbitMQ or Apache Kafka and how you can integrate them with your existing API.
@@SoftwareDeveloperDiaries okay 🤩 thanks again sir
perfect!
Though, one thing I could not digest,
20M loops by one thread Vs 4 threads counting 5 M loops simultaneously / parallelly :->
1. addition of their count will be 5M + 5M + 5M + 5M = 20 M loops for sure as mathematically it is correct. But,
As these 4 threads are working parallelly time taken by them is almost equal which is of counting only 5M loops.
Diagrammatically,
1 Thread ---------------- 20 M loops ---------------------> 12 Sec (let's say)
1/4 Thread --------5M loops ------------> 3 Sec (let's say)
2/4 Thread --------5M loops ------------> 3 Sec
3/4 Thread --------5M loops ------------> 3 Sec
4/4 Thread --------5M loops ------------> 3 Sec
Here, we simply reduced our task to count form 20 M to 5 M, hence time taken describes to count 5M only not 20M.
Are they counting like
1st Thread => 1M ---> 5M , 2nd Thread => 6M ---> 10 M , 3rd Thread => 11M ----> 15 M, 4th Thread => 16M ---> 20M .... ??? No , all threads are counting form 1 to 5M and parallelly hence time taken describes to count 5M only not 20M
and what will happen when you run out of all your worker threads? in case of multiple requests to blocking endpoint by different clients
The machine will have to launch a fair amount of worker threads and get noticeably slow, before it throws an error that it can't launch a new thread. But this is probably the sequence that you'd see.
looks like a case that should be handled)
If our app is inside of pods of kubernetes is not possible the Multithreading if has not have more 1 CPU for pod?
That's a great question! In a Kubernetes environment, each pod typically runs a single container, and the number of CPUs available to a pod depends on the resources allocated to it. If a pod has only one CPU allocated, then indeed, multithreading might not provide significant benefits since there's only one CPU core available for processing.
I have a question,
If I cluster node js app on 8 cors and if I use thread on each computation in my code,
So assumption is my same code is running on 8 clusters and all have threads in them mean to say the api I wrote in my single code is running on 8 different cors, does it improve performance and if it improve performance? Can you tell me pros and cons of it?
Hi , one question is like I have scenario in which have to do file operation many times we are using some third party library to club or combine small doc files to one big final pdf , but that has to be in particular order , there are 20 sections , each section is tree containing further sections and so on and so forth ,each section contains docx file that has to be merged, so can I divide workers to club 20/ thread-count and then finally club the result of all of them to final file, only thing is I would have to find thread count , programmatically
do you agree? is there any other way you can recommend
Maybe try worker threads? I have a video on it too. If that doesn't help, you can try asking ChatGPT, I'm pretty sure it knows everything :d
@@SoftwareDeveloperDiaries thanks
thanks bro,
What is the difference between workers and fork or child process
Processes take much more resources to spawn while threads are lighter and are handled by the same process
Hi,
I did not understand the idea of 7 threads , kindly correct me If this is wrong from what I know is UV_THREADPOOL_SIZE = 4 , by default and these are the only threads used by libuv for some of the operations! i.e except CPU intensive tasks
Correct. When we refer to a "thread", we actually mean a "thread pool". With UV_THREADPOOL_SIZE you define the size of the pool, which by default consists of 4 "child threads". You can still increase the size of the pool up to 1024.
@@SoftwareDeveloperDiaries is it practically possible to have 1024 threads ?
my cloud setup is having 7 nodes each node has 2 vcpu , virtual CPU
I am not sure how many threads each instance has , I printed cpus length using os module it printed 2 , so only 2 threads ? do you suggest any other way or to know about it
@@vaibhavm1986 It can't, the UV_THREADPOOL_SIZE can't be bigger than the number of total logical threads on your CPU. If you have an Octa-Core CPU each of them have 2 threads, then the logical threads at your disposal will be 8 * 2. It can't be more. To automate the code, you can use -
const os = require("os")
process.env.UV_THREADPOOL_SIZE = os.cpus().length
Also, the threads used in the worker pool are not real threads. They are circumventive solution for multithreading. Possibly very close to the Co-routines of Kotlin language. Or GoRoutines from Go lang. In kotlin, you can have thousands of coroutines with multiple tasks.
If you are looking for multithreading, you can use cluster module from node.js, which spawns many worker instances on each cpu thread, but yeah they are still memory intensive and mixing worker_thread with clustering is bad idea.
subscribed.
Happy to have you here, mate!
great for single good server, bad for container. This way was just make container suffer bcose the resources already taken up by the io operation.
What do you suggest then
0:19 - wait, I'm not ready yet!
Ooops 😁
Thanks for this video! All Node haters - in your face! :D
Hell yeah hahaha
where is the source code link?
Great video!
but most likely you work in a company that uses containerized environment (unless you live under a rock) and if thats the case - dont bother, just put 1.25 cpu requests and add more replicas!
its safer and much more scalable, also in containers environment you cant trust the core count since it works on CPU time
How download code example?
Hey, there's none. I was hoping you could reproduce it by watching the video
How hard is it to write that
ACCHA KAM 👍 - HANCOCK
Just learn my friends, and you will get where you want
Audio is slightly off-sync with the video. Great vid tho!
Jesus loves you, man. Thanks for this incredible video.
My pleasure! 😊
Hey Use cluster module instead of this much better approach
I will check it, thanks
fuck bro 😶🌫
Great video, but you should have used a reduce to sum your results. I'm triggered haha
Check this out, it's better to use good old for loops when working with a lot of data :)
leanylabs.com/blog/js-forEach-map-reduce-vs-for-for_of/
@SoftwareDeveloperDiaries I meant to sum the result of each thread.
Total = thread_results[0] + thread_results[1]....
@@Flonfl0n oh i see ^^
@@SoftwareDeveloperDiaries hehehe