Scaling your Node.js app using the "cluster" module
Ғылым және технология
In this tutorial for the "cluster" module of Node.js, we dive deep into the topic of multi-processing, load-balancing, and improved scalability of your applications. We'll explore the benefits of using the "cluster" module, how to implement the "cluster" module, how to use "fork" and some best practices.
📚 Resources:
Check out Stanley Ulili's article that this video is based on:
www.digitalocean.com/communit...
Worker Threads: • How to use Multithread...
======⚡⚡⚡======
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
And don't forget to subscribe for more videos like this 😊
Пікірлер: 72
from version 18.14.0, it is advised to use os.availableParallelism() instead of os.cpus().length
@SoftwareDeveloperDiaries
2 ай бұрын
Thanks for the hint!
GOSH, I love this channel! And how your deliver the concept clearly and precisely. Thanks!!
@SoftwareDeveloperDiaries
11 ай бұрын
Super happy to hear that you enjoy the content, my friend! ☺
Bro, I love it when I find a gem like this. Thank you, I was looking for a quality this channel has to offer for very long time. I'm definitely going to subscribe!
@SoftwareDeveloperDiaries
11 ай бұрын
Thanks for joining the channel, my friend! Happy to hear you like it! :)
Very clean and good explanation! You're good! Keep going! Thanks!
Looks really great! Thanks for the video!
@SoftwareDeveloperDiaries
8 ай бұрын
Thanks mate! 😉
Thank you! Very clear explanation
@SoftwareDeveloperDiaries
Жыл бұрын
Always welcome!
it is the best video of advanced nodejs i ever seen , keep going
@SoftwareDeveloperDiaries
9 ай бұрын
Will do, my friend!
Hi! Your approach by identifying a problem and then solving it is blazingly great 😃 But please make a video on Difference between Cluster and Multi threads? As both utilizing all cpu cores I am looking forward to hear from you
Thanks a lot, This is what I looking for.
@SoftwareDeveloperDiaries
4 ай бұрын
Glad I could help!
Ok this is very awesome. I have been watching all of other videos on your channel and trust me your videos convinced me to subscribe to your channel. I can't wait to see more topics like this. And many many thanks for taking your time to make these videos.
@SoftwareDeveloperDiaries
Жыл бұрын
Thanks for such nice words, my friend!
Great content man. Great channel
@SoftwareDeveloperDiaries
9 ай бұрын
Thanks mate!
omg, bless you.
your videos are awesome man! i usually don't write comments but your channel is so undersubscribed!
@SoftwareDeveloperDiaries
Жыл бұрын
I appreciate that, my friend :)
Great video
My team lead explained me about clustering a few times and i didn't get shit, it sounded like a super complex thing when he talked about it. Your explanation was super easy to understand!! Thanks alot!
@SoftwareDeveloperDiaries
Жыл бұрын
Really happy to hear that! Keep it up mate!
@renzcarlosalanga6077
9 ай бұрын
why? clustering means exactly what it means in the dictionary LOL what's hard about that
@alphacentauri8035
9 ай бұрын
@@renzcarlosalanga6077 So what you're doing here? Just check the dictionary.
thank you for your sharing master, I have a question 'if I have 3 servers, running index.js from each server, how to execute index.js from different server?' any response will be highly appreciated
Thank you , very nice - you love me !
Thank you for the clear explanation. One thing to note is that a single instance still utilizes multithreading for asynchronous operations with libuv. Cluster is advantageous for synchronous operations, as shown in your example.
helpful
Keep in mind cluster is not core-pinning.. It just run on whatever core/thread it would like. Also you can still combine the nodejs cluster feature with workers, which in bigger calculations might help to un-block the main thread. Since nodejs is semi-multithreaded by default.
@MelroyvandenBerg
8 ай бұрын
also when running this cluster setup you can't share memory, it will just spawn new processes of NodeJS.
@SoftwareDeveloperDiaries
8 ай бұрын
Great point!
hi, i have a problem here, I've done everything like in the video but the workers wont work? the loadtest was arguably the same as the first time, so to check by myself i made another route without any load to check if i could make a get from that one while the heavy one was still loading and the answer was no, it waits for heavy get to complete before actually doing the main get, its like everything was going to the same worker instead of trying to use different workers, i have 12 cores so it should work...
Can we use cluster module for "SMPP Protocol" faster performance? SMPP is stateful connections, so I am not getting the full output using cluster, any hint?
Thanks nice.... I just want to ask in one core there are multiple processes....or processes is equal to core. ??? Thanks
@SoftwareDeveloperDiaries
Ай бұрын
By default - no, everything will run on a single core. But with the cluster mode - yes.
What is the difference between loadtest vs ab (apache bench) when i use loadtest The total errors was 500+ but with ab i didn't get any errors and i passed in the same metric as you did. This was an on code without cluster
If we have 8 cpu, the primary script use 1 cpu and remain 7 for the application? Conclusion, if we have 2 cpu - it's doesn't work?
Hi , Thanks I am looking something similar , just want to understand , making clustering this way , or having replica instances via docker , is there any difference between or better as compared to other what is the recommended way ?
@SoftwareDeveloperDiaries
Жыл бұрын
That’s a great question. In theory both approaches allow the so called “horizontal scaling”. When going with Node.js clusters, you’re limited to one machine your cluster is running on. So it’s not optimal for complex apps with very heavy loads, since this machine will be the single point of failure. Node.js cluster is a good idea if you have obvious parallel processing scenarios. However, if you have a complex application that requires multiple machines deployed across multiple environments for example, then Docker can be a better choice. At the end of the day, you can actually try combining them. Run a Node.js cluser on multiple Docker instances.
@vaibhavm1986
Жыл бұрын
Thanks 🙂
Question: Can we achieve Node app horizontal scalability(running node on multiple CPU) with only docker and kuburnetes ?
@SoftwareDeveloperDiaries
7 ай бұрын
In theory yes, but I wouldn't suggest combining them. If your application is relatively simple and you prioritize simplicity, Node.js cluster mode may be suitable. If you need advanced orchestration, infrastructure flexibility, and are planning for future growth, Kubernetes might be the better option. Ultimately, the decision depends on your specific use case, requirements, and the resources available to your team.
Wait i thought pm2 spawns only 1 if you use that command
How does pm2 know that it should run in cluster mode instead of fork mode?
@SoftwareDeveloperDiaries
10 ай бұрын
PM2 uses a configuration file called "ecosystem.config.js" (or "pm2.config.js") to specify exactly that
i'm getting about ~250 errors even after use of cluster , before it was around ~600, how are you achieving 0 ?
Hey! What service do you use to create such a good diagrams?
@SoftwareDeveloperDiaries
Жыл бұрын
Excalidraw :)
Execuse me, I am new with Nodejs so I have a question, the cpu count is the cpu of the server or cpu of user's computer ? Hope you response me soon.
@SoftwareDeveloperDiaries
5 ай бұрын
Hello there! The answer is: the CPU count on the machine that the Node.js app is running on. You’ll be most likely deploying it on the server at the end of the day, so it’s the one that really counts.
Is there any reason why to use this instead of docker and a load balancer ?
@SoftwareDeveloperDiaries
7 ай бұрын
Great question. I would use Node.js "cluster" mode when you have a simple application on a single server and want to scale it horizontally across CPU cores for improved performance. But choose Kubernetes with Docker and Load Balancing when dealing with complex, distributed applications that require container orchestration, horizontal scaling across multiple servers, and advanced features like automatic scaling, rolling updates, and service discovery. It's well-suited for cloud-native architectures.
@aka.theos.
7 ай бұрын
@@SoftwareDeveloperDiaries Your content is really good I hope you continue to cover advanced topics because not many channels cover them in details like you do
@roonywalsh8183
5 ай бұрын
@@SoftwareDeveloperDiaries would making a Nodejs cluster in each docker instance be a good idea. Wouldn't it lead to optimization of each server's usage
Hi, thanks you explanation. I take an error when runing loadtest. What is problem? Error is = node:events:491 throw er; // Unhandled 'error' event ^ Error: read ENOTCONN
@SoftwareDeveloperDiaries
Жыл бұрын
Is there anything else that the error message says? Have you made sure that in package.json you "type": "module" like in the video? I've googled the error and it seems like it's mostly caused by another process running on the same port.
@mjprakash1780
Жыл бұрын
@@SoftwareDeveloperDiaries facing same issue
@korkut31
Жыл бұрын
neden 2 Turk inglizce laf edersiniz, danisirsiniz ? neyse. node 16.19.1 versiyonu ile hata vermiyor. node 18.15.0 ile bende hata aldim .
@storylineteam
Жыл бұрын
Run the loadtest command in a new terminal window outside of vscode
How Do we make rest API with cluster?
@SoftwareDeveloperDiaries
Жыл бұрын
Same way you would make an ordinary Node.js API with Express, but in this case the entrypoint is primary.js, meaning it opens the port and serves the application.
@qureshiowais6951
10 ай бұрын
@@SoftwareDeveloperDiaries please create few video with lates teach and practice thanks
does this create race condition problem ?
@SoftwareDeveloperDiaries
Жыл бұрын
Totally. It all depends on the use case and code that you've written.
how can I use __dirname in typescript?
@themaridv2000
2 күн бұрын
It comes by default
what does this even mean ???
@internetexplorer7880
7 күн бұрын
Scenario: You go out for dinner in a new restaurant. 1st case: There's only one cook, so if the cook is busy making food for someone else you have to wait a long time for your order 2nd case: There are now multiple cooks, so if other people's food takes time, yours won't