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

  • @AshrafulFerdousFariv
    @AshrafulFerdousFariv2 ай бұрын

    from version 18.14.0, it is advised to use os.availableParallelism() instead of os.cpus().length

  • @SoftwareDeveloperDiaries

    @SoftwareDeveloperDiaries

    2 ай бұрын

    Thanks for the hint!

  • @maximus4510
    @maximus451011 ай бұрын

    GOSH, I love this channel! And how your deliver the concept clearly and precisely. Thanks!!

  • @SoftwareDeveloperDiaries

    @SoftwareDeveloperDiaries

    11 ай бұрын

    Super happy to hear that you enjoy the content, my friend! ☺

  • @mowmiadrian8608
    @mowmiadrian860811 ай бұрын

    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

    @SoftwareDeveloperDiaries

    11 ай бұрын

    Thanks for joining the channel, my friend! Happy to hear you like it! :)

  • @AViStudioMoldova
    @AViStudioMoldova7 күн бұрын

    Very clean and good explanation! You're good! Keep going! Thanks!

  • @PaweGradziel
    @PaweGradziel8 ай бұрын

    Looks really great! Thanks for the video!

  • @SoftwareDeveloperDiaries

    @SoftwareDeveloperDiaries

    8 ай бұрын

    Thanks mate! 😉

  • @DudnikAndrey
    @DudnikAndrey Жыл бұрын

    Thank you! Very clear explanation

  • @SoftwareDeveloperDiaries

    @SoftwareDeveloperDiaries

    Жыл бұрын

    Always welcome!

  • @chafroudiyed9301
    @chafroudiyed93019 ай бұрын

    it is the best video of advanced nodejs i ever seen , keep going

  • @SoftwareDeveloperDiaries

    @SoftwareDeveloperDiaries

    9 ай бұрын

    Will do, my friend!

  • @AbdulHannan-or8ql
    @AbdulHannan-or8ql3 ай бұрын

    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

  • @wasankds777
    @wasankds7774 ай бұрын

    Thanks a lot, This is what I looking for.

  • @SoftwareDeveloperDiaries

    @SoftwareDeveloperDiaries

    4 ай бұрын

    Glad I could help!

  • @code_react
    @code_react Жыл бұрын

    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

    @SoftwareDeveloperDiaries

    Жыл бұрын

    Thanks for such nice words, my friend!

  • @progressnwimuelekara2167
    @progressnwimuelekara21679 ай бұрын

    Great content man. Great channel

  • @SoftwareDeveloperDiaries

    @SoftwareDeveloperDiaries

    9 ай бұрын

    Thanks mate!

  • @shinmini99
    @shinmini99 Жыл бұрын

    omg, bless you.

  • @anton1492
    @anton1492 Жыл бұрын

    your videos are awesome man! i usually don't write comments but your channel is so undersubscribed!

  • @SoftwareDeveloperDiaries

    @SoftwareDeveloperDiaries

    Жыл бұрын

    I appreciate that, my friend :)

  • @enogratechnologies1896
    @enogratechnologies18963 ай бұрын

    Great video

  • @alphacentauri8035
    @alphacentauri8035 Жыл бұрын

    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

    @SoftwareDeveloperDiaries

    Жыл бұрын

    Really happy to hear that! Keep it up mate!

  • @renzcarlosalanga6077

    @renzcarlosalanga6077

    9 ай бұрын

    why? clustering means exactly what it means in the dictionary LOL what's hard about that

  • @alphacentauri8035

    @alphacentauri8035

    9 ай бұрын

    @@renzcarlosalanga6077 So what you're doing here? Just check the dictionary.

  • @fendipratama4792
    @fendipratama479211 ай бұрын

    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

  • @remember387
    @remember3876 ай бұрын

    Thank you , very nice - you love me !

  • @hashcode01
    @hashcode014 ай бұрын

    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.

  • @sanjayms-so9wq
    @sanjayms-so9wq25 күн бұрын

    helpful

  • @MelroyvandenBerg
    @MelroyvandenBerg8 ай бұрын

    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

    @MelroyvandenBerg

    8 ай бұрын

    also when running this cluster setup you can't share memory, it will just spawn new processes of NodeJS.

  • @SoftwareDeveloperDiaries

    @SoftwareDeveloperDiaries

    8 ай бұрын

    Great point!

  • @tryzack
    @tryzack3 күн бұрын

    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...

  • @ranadenish
    @ranadenish15 күн бұрын

    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?

  • @saurabhtalele1537
    @saurabhtalele1537Ай бұрын

    Thanks nice.... I just want to ask in one core there are multiple processes....or processes is equal to core. ??? Thanks

  • @SoftwareDeveloperDiaries

    @SoftwareDeveloperDiaries

    Ай бұрын

    By default - no, everything will run on a single core. But with the cluster mode - yes.

  • @mrlectus
    @mrlectus2 ай бұрын

    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

  • @prokhorov_sergey_igorevich
    @prokhorov_sergey_igorevich Жыл бұрын

    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?

  • @vaibhavm1986
    @vaibhavm1986 Жыл бұрын

    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

    @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

    @vaibhavm1986

    Жыл бұрын

    Thanks 🙂

  • @labidyramzi4840
    @labidyramzi48407 ай бұрын

    Question: Can we achieve Node app horizontal scalability(running node on multiple CPU) with only docker and kuburnetes ?

  • @SoftwareDeveloperDiaries

    @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.

  • @internetexplorer7880
    @internetexplorer78807 күн бұрын

    Wait i thought pm2 spawns only 1 if you use that command

  • @carlosflores4179
    @carlosflores417910 ай бұрын

    How does pm2 know that it should run in cluster mode instead of fork mode?

  • @SoftwareDeveloperDiaries

    @SoftwareDeveloperDiaries

    10 ай бұрын

    PM2 uses a configuration file called "ecosystem.config.js" (or "pm2.config.js") to specify exactly that

  • @HelloWorld-ow1dh
    @HelloWorld-ow1dh2 ай бұрын

    i'm getting about ~250 errors even after use of cluster , before it was around ~600, how are you achieving 0 ?

  • @user-cm7vu8ke3b
    @user-cm7vu8ke3b Жыл бұрын

    Hey! What service do you use to create such a good diagrams?

  • @SoftwareDeveloperDiaries

    @SoftwareDeveloperDiaries

    Жыл бұрын

    Excalidraw :)

  • @TuanPham-ph6lr
    @TuanPham-ph6lr5 ай бұрын

    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

    @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.

  • @aka.theos.
    @aka.theos.7 ай бұрын

    Is there any reason why to use this instead of docker and a load balancer ?

  • @SoftwareDeveloperDiaries

    @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.

    @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

    @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

  • @hakanangin
    @hakanangin Жыл бұрын

    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

    @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

    @mjprakash1780

    Жыл бұрын

    @@SoftwareDeveloperDiaries facing same issue

  • @korkut31

    @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

    @storylineteam

    Жыл бұрын

    Run the loadtest command in a new terminal window outside of vscode

  • @qureshiowais6951
    @qureshiowais6951 Жыл бұрын

    How Do we make rest API with cluster?

  • @SoftwareDeveloperDiaries

    @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

    @qureshiowais6951

    10 ай бұрын

    @@SoftwareDeveloperDiaries please create few video with lates teach and practice thanks

  • @marwanFX483
    @marwanFX483 Жыл бұрын

    does this create race condition problem ?

  • @SoftwareDeveloperDiaries

    @SoftwareDeveloperDiaries

    Жыл бұрын

    Totally. It all depends on the use case and code that you've written.

  • @mateuszfijak5293
    @mateuszfijak5293 Жыл бұрын

    how can I use __dirname in typescript?

  • @themaridv2000

    @themaridv2000

    2 күн бұрын

    It comes by default

  • @cloudpg0788
    @cloudpg07883 ай бұрын

    what does this even mean ???

  • @internetexplorer7880

    @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

Келесі