How to use Multithreading with "worker threads" in Node.js?

  Рет қаралды 42,498

Software Developer Diaries

Software Developer Diaries

Күн бұрын

JavaScript is single-threaded, but parallelism and multithreading are possible within Node.js with the help of worker threads. In this video, we're going to explore how one can make an Express.js server faster with worker threads when the main thread is blocked by heavy computations.
📚 Resources:
Stanley Ulili's article on Digital Ocean: www.digitalocean.com/communit...
======⚡⚡⚡======
Consider supporting the channel by clicking the "Thanks" button ❤️
👉 Get a 20% discount on Brilliant.org (to learn Science, Technology, Engineering, and Math): brilliant.sjv.io/kjVO2z
======⚡⚡⚡======
🙌 Become my Patreon and get exclusive perks: / softdevdiaries
💼 Follow me on LinkedIn and drop me a message if you'd like: / gusgadirov
💻 Also, let's connect on GitHub: github.com/gusgad
🕒 Timestamps:
(0:00) Node.js multithreading overview
(3:33) How Node.js worker threads can help
(09:02) Optimizing worker threads
And don't forget to subscribe for more videos like this 😊

Пікірлер: 90
@codingispower1816
@codingispower1816 4 ай бұрын
Great video! Excited for this whole series!
@leeshey99
@leeshey99 Ай бұрын
The most intuitive video of multithreading nodejs out there
@guytonedhai
@guytonedhai 11 ай бұрын
This is gold! I have been binge watching your channel. I wish I had discovered your channel much before.
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries 11 ай бұрын
You can’t imagine how much this comment means to me!
@jamshaidsabir5460
@jamshaidsabir5460 2 ай бұрын
You solved a great problem. Thanks a lot
@Dev-Siri
@Dev-Siri Жыл бұрын
This is the best video I have found that teaches how to use multithreading in Node
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries Жыл бұрын
Happy to hear that!
@ezetenduwilliams7322
@ezetenduwilliams7322 8 ай бұрын
Facts
@vitto_pincharrata
@vitto_pincharrata 10 ай бұрын
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
@OleksandrDanylchenko2k
@OleksandrDanylchenko2k Жыл бұрын
Really nice guide! Thanks 🙌🏻
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries Жыл бұрын
Нема за що, glad you enjoyed it 👍
@Infinite_Mainak
@Infinite_Mainak 9 ай бұрын
Best video I have ever seen on node multithreading👍
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries 9 ай бұрын
Wow, thanks!
@arkadiptadas4148
@arkadiptadas4148 Жыл бұрын
Super helpful. Thank you so much!
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries Жыл бұрын
Happy to hear, buddy!
@raviagheda5564
@raviagheda5564 Ай бұрын
Awesome content, Thanks for the creating video Multi Threading. Explanation was too smooth, and especially loved the optimization part.
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries Ай бұрын
Glad you liked it!
@vicTor-ft8xv
@vicTor-ft8xv 7 ай бұрын
Thank you brother
@54peace
@54peace Жыл бұрын
Just finished watching and subscribed. thanks for this amazing tutorial! I actually learnt something really important for my job. So thanks again! and Please upload more tutorial like this. can we also have socket pool tutorials?
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries Жыл бұрын
Hey, happy to have you on board! And thanks for the feedback, I'll look into socket pools :)
@frontend_ko
@frontend_ko Жыл бұрын
okay, understood, thanks for nice tip
@alirezamosavi6185
@alirezamosavi6185 8 ай бұрын
Great bro
@ahmedsoran4710
@ahmedsoran4710 Жыл бұрын
thank you for sharing this was so helpful
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries Жыл бұрын
Sure thing 🙂
@angryman9333
@angryman9333 Жыл бұрын
bzhy :d
@carlosdelgado5632
@carlosdelgado5632 Жыл бұрын
Excellent explanation, thank you
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries Жыл бұрын
Glad it was helpful!
@caotichcuc
@caotichcuc 15 сағат бұрын
God, it's super helpful. Please make more videos about APIs optimization and security for Nodejs, pleaseeeeeeeeeeeeeeeeeeeeeeeeeee
@gregoax5474
@gregoax5474 2 ай бұрын
great content!
@Jberv
@Jberv Жыл бұрын
Deym, I thought i'm on my limits for optimizing my api, Thank you so much for this
@Abdulkalam-he7wo
@Abdulkalam-he7wo Жыл бұрын
great work , explanation at its best..!
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries Жыл бұрын
Thanks mate!
@Abdulkalam-he7wo
@Abdulkalam-he7wo Жыл бұрын
Please do more stuffs like this💥
@pabloroque8268
@pabloroque8268 Ай бұрын
Great content, i hope you to do more videos with advanced concepts and proyects with nodejs, best regards from Argentina
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries Ай бұрын
Gracias amigo! More to come 😉
@acluster3411
@acluster3411 11 ай бұрын
Very well done!
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries 11 ай бұрын
Thank you!
@verdynandreyhans9592
@verdynandreyhans9592 10 ай бұрын
thanks bro,
@labidyramzi4840
@labidyramzi4840 6 ай бұрын
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 !
@ronnykgalema
@ronnykgalema 8 ай бұрын
Imagine combining multithreading with clustering? That would be a bomb 💣
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries 8 ай бұрын
Hmmm interesting!!
@Pareshbpatel
@Pareshbpatel 4 ай бұрын
An excellent tutorial on Multithreading in Node using Worker Threads. Thank you very much. {2024-01-12}, {2024-01-15}, {2024-01-19}
@parassharma7041
@parassharma7041 2 күн бұрын
Great video
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries Күн бұрын
Thanks!
@firoj.siddiki
@firoj.siddiki 7 ай бұрын
subscribed.
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries 7 ай бұрын
Happy to have you here, mate!
@muharief3885
@muharief3885 Жыл бұрын
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.
@Aleks-fp1kq
@Aleks-fp1kq 2 ай бұрын
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.
@vaibhavm1986
@vaibhavm1986 Жыл бұрын
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
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries Жыл бұрын
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
@vaibhavm1986
@vaibhavm1986 Жыл бұрын
@@SoftwareDeveloperDiaries thanks
@ayeshazahid2421
@ayeshazahid2421 Жыл бұрын
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
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries Жыл бұрын
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.
@ayeshazahid2421
@ayeshazahid2421 Жыл бұрын
@@SoftwareDeveloperDiaries okay 🤩 thanks again sir
@user-gy9sv7fp3i
@user-gy9sv7fp3i Ай бұрын
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.
@ahmeterdonmez9195
@ahmeterdonmez9195 3 ай бұрын
hello brother. Can we use this to handle 2-3 different tasks at the same time? I subscribed your channel ;)
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries 3 ай бұрын
Yes, you totally can :) Thanks for the subscription!
@jsarabia_dev7371
@jsarabia_dev7371 28 күн бұрын
If our app is inside of pods of kubernetes is not possible the Multithreading if has not have more 1 CPU for pod?
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries 22 күн бұрын
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.
@Black-xq9ey
@Black-xq9ey 21 күн бұрын
Sir Which theme you are using? It's cool for the eyes.
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries 14 күн бұрын
Hey mate, it's called "Palenight (Mild Contrast)"
@reactjs1900
@reactjs1900 9 ай бұрын
What is the difference between workers and fork or child process
@jperusm
@jperusm 2 ай бұрын
Processes take much more resources to spawn while threads are lighter and are handled by the same process
@atmai1026
@atmai1026 Ай бұрын
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
@vb4190
@vb4190 11 ай бұрын
and what will happen when you run out of all your worker threads? in case of multiple requests to blocking endpoint by different clients
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries 11 ай бұрын
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.
@vb4190
@vb4190 11 ай бұрын
looks like a case that should be handled)
@chavdarajan7428
@chavdarajan7428 Ай бұрын
ACCHA KAM 👍 - HANCOCK
@vaibhavm1986
@vaibhavm1986 Жыл бұрын
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
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries Жыл бұрын
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.
@vaibhavm1986
@vaibhavm1986 Жыл бұрын
@@SoftwareDeveloperDiaries is it practically possible to have 1024 threads ?
@vaibhavm1986
@vaibhavm1986 Жыл бұрын
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
@vinaykumarkharwar177
@vinaykumarkharwar177 8 ай бұрын
@@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.
@user-nw3rz4yp4u
@user-nw3rz4yp4u 3 ай бұрын
I think while creating worker threads , new main thread is not created. Correct me if I am wrong.
@aki1840
@aki1840 Жыл бұрын
Thanks for this video! All Node haters - in your face! :D
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries Жыл бұрын
Hell yeah hahaha
@esp2644
@esp2644 Жыл бұрын
How download code example?
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries Жыл бұрын
Hey, there's none. I was hoping you could reproduce it by watching the video
@swagz6440
@swagz6440 Жыл бұрын
How hard is it to write that
@meandradethiago
@meandradethiago 10 ай бұрын
Jesus loves you, man. Thanks for this incredible video.
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries 10 ай бұрын
My pleasure! 😊
@innoventstack
@innoventstack Жыл бұрын
Hey Use cluster module instead of this much better approach
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries Жыл бұрын
I will check it, thanks
@Flonfl0n
@Flonfl0n Ай бұрын
Great video, but you should have used a reduce to sum your results. I'm triggered haha
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries Ай бұрын
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/
@Flonfl0n
@Flonfl0n Ай бұрын
@SoftwareDeveloperDiaries I meant to sum the result of each thread. Total = thread_results[0] + thread_results[1]....
@SoftwareDeveloperDiaries
@SoftwareDeveloperDiaries Ай бұрын
@@Flonfl0n oh i see ^^
@Flonfl0n
@Flonfl0n Ай бұрын
@@SoftwareDeveloperDiaries hehehe
Scaling your Node.js app using the "cluster" module
13:44
Software Developer Diaries
Рет қаралды 31 М.
ChatGPT’s Amazing New Model Feels Human (and it's Free)
25:02
Matt Wolfe
Рет қаралды 215 М.
Which one will take more 😉
00:27
Polar
Рет қаралды 83 МЛН
Learn Typescript with React | Quick Crash Course
16:59
developedbyed
Рет қаралды 87 М.
Mastering Memory Management in Node.js: Tips and Tricks for Better Performance
16:06
Software Developer Diaries
Рет қаралды 24 М.
PROOF JavaScript is a Multi-Threaded language
8:21
Beyond Fireship
Рет қаралды 257 М.
How To Handle Errors in Node.js and TypeScript
37:23
Mark Maksi
Рет қаралды 3,3 М.
I Stopped Using Express.js: Because Bun and Hono 🔥
10:23
Sam Meech-Ward
Рет қаралды 97 М.
How to handle Node.js errors like a Pro?
12:44
Software Developer Diaries
Рет қаралды 20 М.
When is NodeJS Single-Threaded and when is it Multi-Threaded?
18:42
Hussein Nasser
Рет қаралды 69 М.
What % of charge do you have on phone?🔋
0:11
Diana Belitskay
Рет қаралды 346 М.
How Neuralink Works 🧠
0:28
Zack D. Films
Рет қаралды 28 МЛН