How to scale NodeJs applications using the cluster module.

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ส.ค. 2024
  • In this video you will learn how to #scale #nodejs apps using the built in #cluster module. Its super easy to use and it will make your applications much faster and in turn your applications will have #zero-downtime. It will also automatically handle #load-balancing
    Deploy apps: zeet.co/r/your...
    (One FREE project forever 😘)
    FREE $100 credit @Digital Ocean: m.do.co/c/3208...
    Subscribe: www.youtube.co...
    Support the channel:
    Paypal: www.paypal.me/...
    Patreon: / trulymittal
    ⭐ Kite is a free AI-powered coding assistant that will help you code faster and smarter. The Kite plugin integrates with all the top editors and IDEs to give you smart completions and documentation while you’re typing. I've been using Kite for 6 months and I love it! www.kite.com/g...
    ---------------
    Other useful Playlists
    ---------------
    #RestAPI (#NodeJS and MongoDB): • REST API using NodeJS ...
    #API Authentication using #JWT: • NodeJS API Authenticat...
    #Firebase: • Firebase | Build a Not...
    Docker: • Docker
    MongoDB: • Learn MongoDB in 50 mi...
    Html/Css/Js: • HTML / CSS / JS
    Android: • Android
    Challenges: • Challenges
    #yoursTRULY #tutorial #howto #nodejs #android

ความคิดเห็น • 166

  • @Mirzly
    @Mirzly 3 ปีที่แล้ว +25

    Where were you a year ago when I first heard of this stuff and went on to figure it out on my own 😢

    • @mafiacodes
      @mafiacodes  3 ปีที่แล้ว +1

      Subscribe like share

  • @shashwatsingh253
    @shashwatsingh253 ปีที่แล้ว +1

    Best TH-cam for NodeJS important topics.

  • @mdjahidulislam9205
    @mdjahidulislam9205 3 ปีที่แล้ว +6

    You are helping to do my app on production.
    Please make more videos like this so that we can learn real-life stuff.

  • @oneeminem2666
    @oneeminem2666 2 ปีที่แล้ว +4

    This is just what I was looking for thanks man. Please post more core concepts like these. Great Work

    • @sokoyaadedolapo5321
      @sokoyaadedolapo5321 11 หลายเดือนก่อน

      Same here too, I've been looking for this type of example didn't see any, what I found is how they work not how to use them.

  • @jibanjyotinayak9359
    @jibanjyotinayak9359 ปีที่แล้ว

    this is one of the awesome video on node cluter module on TH-cam yet...

  • @NirdeshPokhrel
    @NirdeshPokhrel 2 ปีที่แล้ว +6

    I only knew we can scale nodeJs server, but have never tried to do so.
    Thanks to you for making me(us) know scaling server in nodeJs. This is worth a subscribe.❤️☘️

  • @soft4win619
    @soft4win619 2 ปีที่แล้ว +2

    This is so cool, your explanation was superb, didn't knew a single thing about cluster, but was able to setup the whole thing with so much ease.

  • @sayed_faixal
    @sayed_faixal 3 ปีที่แล้ว +3

    Awesome 🔥
    As mentioned in the description, every line was explained in a detailed manner.

  • @bosluk8806
    @bosluk8806 ปีที่แล้ว +8

    A nice solution that you can use for small projects. However, if you are looking for a solution for more complex projects, instead of this, you should run multiple node instances and put a load balancer in front of them.

    • @mafiacodes
      @mafiacodes  ปีที่แล้ว +1

      Yes

    • @paulojo720
      @paulojo720 ปีที่แล้ว

      Pls how can I do that, is there any tutorial source you can provide to learn this 🙏, thanks

    • @StLouis-yg8jp
      @StLouis-yg8jp ปีที่แล้ว +1

      Why not have multiple node instances all using the cluster module? Wouldn't that be the most efficient?

    • @hongphuc5497
      @hongphuc5497 ปีที่แล้ว

      @@StLouis-yg8jp Because they would share the same memory on your EC2 (I mean the machine you run application), it's a drawback for ram intensive tasks.

  • @ramiworkstation
    @ramiworkstation หลายเดือนก่อน

    Beautiful. Thank you for sharing 🌷

  • @himanshulal97
    @himanshulal97 2 ปีที่แล้ว +1

    that 's the reason i subscribed to your channel , thanks a lot bro thanku thanku thanku

  • @Dipenparmar12
    @Dipenparmar12 ปีที่แล้ว +1

    Amazing real world project tutorial.

  • @OmpalSingh-pw7mc
    @OmpalSingh-pw7mc ปีที่แล้ว +1

    Great work 👏

  • @funnyclipz520
    @funnyclipz520 2 ปีที่แล้ว +4

    This is good for vertical scaling and only if your machine has more than one CPU thread.
    If your CPU is single threaded one can also look into kubernetes. After understanding kubernetes pods you can see how beautifully they solved this problem on an infra level.
    Also great video bro!, keep em coming....

    • @felixlabayen7861
      @felixlabayen7861 ปีที่แล้ว

      how kubernetes solves this in infra level?

  • @student7743
    @student7743 ปีที่แล้ว +2

    this video is awesome. but i stucked as you didn't tell where to use this loadtester?

  • @rahulnautiyal4225
    @rahulnautiyal4225 3 ปีที่แล้ว

    Great explanation sir , I am watching your videos from last year now you are doing too good . I am following you from my 3 different id's. Great work keep going on .

  • @pranavwani
    @pranavwani ปีที่แล้ว +1

    Thanks man for sharing important information!

  • @zakariasassi
    @zakariasassi ปีที่แล้ว +1

    Amazing video , thanks bro

  • @samusaw
    @samusaw ปีที่แล้ว +1

    very very great work

  • @missiontechdk6064
    @missiontechdk6064 2 ปีที่แล้ว

    outstanding explanation bro. Keep posting such quality content 🔥🔥

  • @greggord-hume3979
    @greggord-hume3979 2 ปีที่แล้ว +2

    I deploy to Heroku using a Hobby dyno and I have noticed my node project barely uses any CPU or RAM when stress testing. So i added more workers, because its only 1 cpu I had to hardcode a worker amount - i found 6 was good for my app.
    My requests per second and response times improved drastically - 6 fold you could say.
    I will now try cluster to see what happens but i think its the same thing... although its recommended to not do this and let Node handle the load, i cant help but notice adding workers and clusters utilizes more of the Servers resources and speeds up the response per second.

  • @nexus2056
    @nexus2056 10 หลายเดือนก่อน

    PHENOMENAL VIDEO BROTHER!! Mashallah!

  • @azeemshah9501
    @azeemshah9501 2 ปีที่แล้ว

    So good. It's a very good way to scale a simple application for starters.

  • @parasarora7565
    @parasarora7565 3 ปีที่แล้ว +1

    Nice tutorials, helping me alot. Keep uploading

    • @mafiacodes
      @mafiacodes  3 ปีที่แล้ว

      Subscribe like share

  • @thetechrr
    @thetechrr ปีที่แล้ว +1

    Thank you man, you saved the day.

  • @hardikmistry1661
    @hardikmistry1661 2 ปีที่แล้ว

    this was the great video sir 😎😎 i use compresion with cluster module and it give realy great result thans sir to make this awesome video. 👍🤩😍.

  • @gautiere-d9012
    @gautiere-d9012 2 ปีที่แล้ว +2

    Amazing tutorial, thank you

    • @mafiacodes
      @mafiacodes  2 ปีที่แล้ว +1

      Glad you enjoyed it!

  • @channaveerhakari7319
    @channaveerhakari7319 11 หลายเดือนก่อน

    You can use PM2 to run your application in Cluster mode and also it servers as your process manager ie if your application by default crashes then it will be restarted automatically

    • @nishantbhavasar5047
      @nishantbhavasar5047 10 หลายเดือนก่อน

      yes or it has inbuilt load balancer also so it's a good approach then clustor module

    • @vinaykumarkharwar177
      @vinaykumarkharwar177 7 หลายเดือนก่อน

      @@nishantbhavasar5047 using pm2 will start it in cluster mode also. You possibly will have to use load balancer and use pm2 to create different worker threads.

  • @mathearts9739
    @mathearts9739 3 ปีที่แล้ว +1

    Subscribed awesome explanation.

  • @vellaikaagitham8401
    @vellaikaagitham8401 ปีที่แล้ว

    Thanks for clear explanation

  • @mohitchauhan569
    @mohitchauhan569 2 ปีที่แล้ว +1

    very nice explain clustering

  • @jeevanbhattarai4133
    @jeevanbhattarai4133 2 ปีที่แล้ว +1

    great one

  • @AjaySharma-vz9ei
    @AjaySharma-vz9ei ปีที่แล้ว

    Superb 👐🏻👏🏻

  • @Superuser-r1y
    @Superuser-r1y 2 ปีที่แล้ว +1

    Great explanation bhai 👍❤️🔥

  • @TechBoldy
    @TechBoldy 3 ปีที่แล้ว +1

    Super awesome video sir 💜❤💙

    • @mafiacodes
      @mafiacodes  3 ปีที่แล้ว

      Subscribe like share

  • @theervu
    @theervu ปีที่แล้ว +1

    Thanks

  • @PuneetThakkar7
    @PuneetThakkar7 2 ปีที่แล้ว +1

    Excellent example

    • @mafiacodes
      @mafiacodes  2 ปีที่แล้ว

      subscribe like share

  • @rahulr9539
    @rahulr9539 2 ปีที่แล้ว +1

    I would suggest installing nodemon as dev dependency or global dependency

  • @trihargianto
    @trihargianto ปีที่แล้ว

    wow, such a great explanation 😃

  • @utsavojha2953
    @utsavojha2953 2 ปีที่แล้ว

    Thanks for this awesome video 👍🏻

  • @kevalcharla9797
    @kevalcharla9797 3 ปีที่แล้ว +1

    Amazing Bro!!

  • @kid_kulafu_1727
    @kid_kulafu_1727 3 ปีที่แล้ว

    Thank you man. I love you!

  • @moose43h
    @moose43h 2 ปีที่แล้ว +1

    This is amazing

  • @ciroamore2129
    @ciroamore2129 3 ปีที่แล้ว +1

    Truly Amazing :)!!!!

  • @abhijeetshikharvlog1444
    @abhijeetshikharvlog1444 2 ปีที่แล้ว

    Good explanation

  • @tejass817
    @tejass817 2 ปีที่แล้ว

    Very useful information.

  • @the_taskmasterr
    @the_taskmasterr 2 ปีที่แล้ว +1

    Hey brother...i like the way you explain things...one doubt in this lecture is that how to apply this cluster module when we are using controllers and routes style of architecture...Hope you will reply my question🤞

    • @the_taskmasterr
      @the_taskmasterr 2 ปีที่แล้ว

      @Not Top Voices thanks man i got it

  • @puspendumatilal6460
    @puspendumatilal6460 2 ปีที่แล้ว

    I wish I could have a love reaction button.

  • @nandhakumar4817
    @nandhakumar4817 3 ปีที่แล้ว

    Clear explanation thanks

  • @todaysmotivation9692
    @todaysmotivation9692 2 ปีที่แล้ว

    Your fluency is awsome, just make your speed a bit slow - it will be great

  • @saifulsync155
    @saifulsync155 3 หลายเดือนก่อน

    great video

  • @stylesg7818
    @stylesg7818 3 ปีที่แล้ว +1

    Interesting... Thank you

  • @syedshahzaibzafar2422
    @syedshahzaibzafar2422 2 ปีที่แล้ว

    great explanation.

  • @MRAMetharam
    @MRAMetharam 2 ปีที่แล้ว

    Although the content here is superb, something else caught my eye! What did you have to install to have your comments use a different font?

  • @RIAN8835
    @RIAN8835 ปีที่แล้ว +1

    you are awesome

  • @sanyamarora3011
    @sanyamarora3011 3 ปีที่แล้ว +1

    Hi truly, could you please make a tutorial on how to use Typescript in Node projects .
    P.S You are a great Teacher.

    • @mafiacodes
      @mafiacodes  3 ปีที่แล้ว

      Well I guess I will try sometime

  • @DTUSEM
    @DTUSEM 2 ปีที่แล้ว +1

    Should we need to do load balancing after this also?

  • @Javedboqo1
    @Javedboqo1 ปีที่แล้ว

    Nice tutorial
    Does clusters are still useful in recent Nodejs versions or is there any alternative of it?

  • @KallolMedhi
    @KallolMedhi ปีที่แล้ว

    Thanks for the amazong video but in my case I am getting the response from the same pid always, there is no round-robin happening

  • @MrRaj-qv7pg
    @MrRaj-qv7pg 2 ปีที่แล้ว

    Good job bro. What is your keyboard name ?

  • @firojsiddiki2750
    @firojsiddiki2750 3 ปีที่แล้ว

    it is really a super useful video

  • @castmonkeys7590
    @castmonkeys7590 2 ปีที่แล้ว

    very clean explanation, but wondering why we need to restart a worker after each request ?

  • @souravvashisht7153
    @souravvashisht7153 ปีที่แล้ว +1

    Which is better scaling with Cluster create instance upto n number of logiacal core Or using Docker to create multiple container and using nginx as reverse proxy ?

    • @mafiacodes
      @mafiacodes  ปีที่แล้ว +1

      Docker with nginx as reverse proxy

  • @yesbit
    @yesbit ปีที่แล้ว

    Just got this on my recommendation and I have a question, why kill the worker and fork another after each request ? Is there a performance benefit and what about the performance penalty in fork+kill code execution ?

  • @mohamad.elbohsaly
    @mohamad.elbohsaly ปีที่แล้ว

    How do you deal with cron job(s)? They get forked as well to nodes?

  • @DTUSEM
    @DTUSEM 2 ปีที่แล้ว +1

    Create content like this you will get more views

  • @cristianfonseca9404
    @cristianfonseca9404 2 ปีที่แล้ว +1

    Great video.! Just a quick question the performance will be the same if I just run in the vps pm2 start -i max ????

    • @mafiacodes
      @mafiacodes  2 ปีที่แล้ว

      Just that if u have 1 cpu only one instance and if cpu>1 then instances equal to cpus

  • @SaiKumar-bu5ji
    @SaiKumar-bu5ji 3 ปีที่แล้ว

    For what kind of applications clustering API is ideal choice

  • @sourikghosh2597
    @sourikghosh2597 3 ปีที่แล้ว

    what is the difference between forking and creating a new worker thread ??

  • @SaiKumar-tg6ct
    @SaiKumar-tg6ct 3 ปีที่แล้ว

    Good explanation,
    please try to Make videoes on nodejs interview questions

  • @kishorindhe4891
    @kishorindhe4891 ปีที่แล้ว

    Great ♥️♥️

  • @brucebane7401
    @brucebane7401 3 ปีที่แล้ว

    amazing !!!!!

  • @shahbankhan8844
    @shahbankhan8844 ปีที่แล้ว

    What kind of terminal ur using I tried multiple time in cmd but it's not working

  • @developer_01
    @developer_01 ปีที่แล้ว

    Hey what are you using to test the performance of your node application

  • @navaneethpk3535
    @navaneethpk3535 2 ปีที่แล้ว

    i am able to fork only one worker despite having 4 cores. Any idea why?

  • @parastouhashemi8620
    @parastouhashemi8620 2 ปีที่แล้ว +1

    Great

  • @linuxtubers7313
    @linuxtubers7313 3 ปีที่แล้ว

    Thanks. Which theme and font you're using?

  • @riteshthakur9250
    @riteshthakur9250 3 ปีที่แล้ว

    Amazing video but can we use this same idea on real time project like i already have a project so can i use this for fast performance in my project

  • @shahbankhan8844
    @shahbankhan8844 ปีที่แล้ว

    Please anyone tell me how to get that log test results

  • @technoinfoworldwide2329
    @technoinfoworldwide2329 2 ปีที่แล้ว

    i suppose to deploy my site in aws, should i need this node cluster module or aws load balancing is good?please share your thoughts which one is better

    • @mafiacodes
      @mafiacodes  2 ปีที่แล้ว

      If u wanna pay use aws else you can do it urself like this or even with nginx

  • @andreadellamaggiora898
    @andreadellamaggiora898 2 ปีที่แล้ว

    Hello, are you using a special font for coding? if yes which is the name?
    I really like it.
    Thank you

  • @knowhowitworks637
    @knowhowitworks637 ปีที่แล้ว

    how to get those benchmarks

  • @ajmaln73
    @ajmaln73 2 ปีที่แล้ว

    so how do i use this with express code

  • @DeepakJangra-up8ek
    @DeepakJangra-up8ek 3 ปีที่แล้ว +1

    os.cpus().length will return the number of cores not threads (I beleive). So I have a few doubts
    1. If I have 2 cores 4 threads cpu then will it be on full potential this way because it looks like it will create 2 proces only.
    2. If I have to host say 3 apps on a 2 threads processor so how should it done. Should I create 2 processes for all 3 apps resulting in 6 total proces?

    • @mafiacodes
      @mafiacodes  3 ปีที่แล้ว

      1. yes os.cpus().length gives u the number of cores, if I said something else its slip of my tounge.
      2. You should only have the number of instances of you app === number of cores of a CPU
      3. If you have a single core CPU then you should run 2 instances, so as to have Zero downtime, in the case one of the instances go down.
      Note: Regarding ur query about cores and threads, you can read this link and will provide you more insight about that and your question will be answered. www.quora.com/What-does-2-core-4-threads-mean-in-a-cpu-I-am-talking-about-this-cpu-ARK-Intel%C2%AE-Core%E2%84%A2-i5-520M-Processor-3M-Cache-2-40-GHz#:~:text=It%20means%20that%20the%20CPU,pipeline%20to%20process%20another%20thread.

    • @DeepakJangra-up8ek
      @DeepakJangra-up8ek 3 ปีที่แล้ว

      @@mafiacodes By this link you mean ... ?
      PM2 cluster mode with command pm2 start app.js -i max creates 8 apps on my 4 core 8 threads processor and according to you 4 is best. It got me little confused here :/
      and also
      Is your name really Truly (just asking) :|

    • @mafiacodes
      @mafiacodes  3 ปีที่แล้ว

      Bro I can’t really come up with an answer since I have an old quad core i5 4670k, and pm2 only does 4 coz of 4 threads. In ur system what do u get for os.cpus.length? Coz what I did was in documentation. And yes my name is Truly.

    • @DeepakJangra-up8ek
      @DeepakJangra-up8ek 3 ปีที่แล้ว

      @@mafiacodes OK. Now I am super duper confused on my i5 8250u (4 cores 8 threads) os.cpus().length gives 8.

    • @mafiacodes
      @mafiacodes  3 ปีที่แล้ว +2

      Ok solved, it’s because since it’s hyper threading. U have a 4 core cpu with 8 threads, that means that for each core the os provided u with 2 virtual cores and therefore ur getting 8, for more info read wiki: en.m.wikipedia.org/wiki/Hyper-threading
      In the end a gr8 question and I think it will help out others also.
      Solved 💯

  • @abhijeetshikharvlog1444
    @abhijeetshikharvlog1444 2 ปีที่แล้ว

    Please make video nodejs microservices

  • @Pralyth
    @Pralyth 2 ปีที่แล้ว

    I am getting the same results using 1 process as with 8. Both tests are finished in ~0.43 seconds and output ~2290 requests per second. Could this be due to it being such a simple program that it doesn't make a difference that I have multiple processes running on it? I have an Intel I5 8600 processor with 6 cores and ~4000mHz.

    • @mafiacodes
      @mafiacodes  2 ปีที่แล้ว

      Can u share your code

  • @be_b_50_ankitkumar_singh79
    @be_b_50_ankitkumar_singh79 3 ปีที่แล้ว

    Sir , how to do if we have routes , like I mean in my project I have a routes folder in which multiple JS files are there and each of it contains GET request, in such case how to perform this?

    • @rajendrah7316
      @rajendrah7316 ปีที่แล้ว

      I am also looking for the same brother, did you get any information on this ? Is yes, could you please share some resources to study please ?

  • @Omer-fr1ze
    @Omer-fr1ze 3 ปีที่แล้ว

    Hello, something happened to me different than video, I do not know why only same process response all the time for every requests.(Before adding zero downtime property) Do I miss something? Great video btw!

    • @Omer-fr1ze
      @Omer-fr1ze 3 ปีที่แล้ว +2

      It is because in node v4.x and newer versions round-robin scheduling is disabled by default on Windows for performance reasons.Simply just add this code " cluster.schedulingPolicy = cluster.SCHED_RR; " if someone has same problem.

    • @tahmidurrahman5119
      @tahmidurrahman5119 3 ปีที่แล้ว +2

      @@Omer-fr1ze thanks...it solved my issue

  • @mdabutalha3165
    @mdabutalha3165 2 ปีที่แล้ว

    Could you please make it using pm2 package?

  • @fuadcs22
    @fuadcs22 2 ปีที่แล้ว

    Why do I have to kill the process?

  • @reactm
    @reactm 2 ปีที่แล้ว

    can you please tell your vs code theme and font family you are using ?

    • @mafiacodes
      @mafiacodes  2 ปีที่แล้ว

      Snazzy operator and Operator mono
      Subscribe like share

  • @ankurgoel1638
    @ankurgoel1638 3 ปีที่แล้ว

    cluster module vs child process
    i am confused between them,
    cluster module uses cores of cpu
    while child processes?
    could you make a difference video? please

    • @beefnuts2941
      @beefnuts2941 2 ปีที่แล้ว +1

      i am new to this but I think cluster is just for making copies of the current app, but child processes could run a different app

  • @mdjahidulislam9205
    @mdjahidulislam9205 3 ปีที่แล้ว

    Should i use pm2 or node cluster? Are they same?

    • @mafiacodes
      @mafiacodes  3 ปีที่แล้ว +1

      Pm2 internally uses node cluster

  • @rezaghifary3653
    @rezaghifary3653 3 ปีที่แล้ว

    thx for the tuto my friend, although i wanna ask what is benefits for killing the worker and fork a new cluster? why cant we just leave the workers without killing it eact time its called.

    • @mafiacodes
      @mafiacodes  3 ปีที่แล้ว

      Killing workers is only for demonstration, and you don’t have to do it manually, only if it fails for some reason then a new worker will be spawned.

    • @rezaghifary3653
      @rezaghifary3653 3 ปีที่แล้ว

      @@mafiacodes thats what i thought. thx

  • @arqamrafay
    @arqamrafay ปีที่แล้ว

    At 0:45 Result : how to identify the result kindly guide , thanks in advance

  • @saikumar-eh6df
    @saikumar-eh6df 3 ปีที่แล้ว

    What is race condition in which cases it occurs, I have heard that using cluster module may cause for race condition.

    • @mafiacodes
      @mafiacodes  3 ปีที่แล้ว

      I don’t think so, because it uses round robin approach

    • @saikumar-eh6df
      @saikumar-eh6df 3 ปีที่แล้ว

      @@mafiacodes Thanks

  • @usama_khalid
    @usama_khalid 3 ปีที่แล้ว

    great video bro... when we use pm2 in cluster mode then we need redis to keep data same on every instance. Do we need redis etc when deploying node app using cluster module (fork).?

    • @mafiacodes
      @mafiacodes  3 ปีที่แล้ว +2

      U need redis if u want to communicate between processes like for socket.io else u won’t need it.

  • @sourikghosh2597
    @sourikghosh2597 3 ปีที่แล้ว

    Just asking , will these make our app statefull ???

    • @mafiacodes
      @mafiacodes  3 ปีที่แล้ว +1

      All instances have their own state, if u want to use single state for all of them though not necessary use Redis pub sub.

  • @symbianizemember6097
    @symbianizemember6097 3 ปีที่แล้ว

    Is it advisable to kill the worker in every process? In my case in every router.

    • @mafiacodes
      @mafiacodes  3 ปีที่แล้ว

      Nooo, that’s only for demonstration

    • @symbianizemember6097
      @symbianizemember6097 3 ปีที่แล้ว

      @@mafiacodes Okay, thank you :D

    • @symbianizemember6097
      @symbianizemember6097 3 ปีที่แล้ว

      @@mafiacodes I'm curious about how to use the cluster in a socket io application? Is it a good idea to use both of them together?

    • @mafiacodes
      @mafiacodes  3 ปีที่แล้ว +1

      Yes of course

  • @isaiasgarciatrueba
    @isaiasgarciatrueba 3 ปีที่แล้ว +1

    Primer Comentario, First comment.