How to scale NodeJs applications using the cluster module.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay guys so welcome back again so in this video let's see that how do we scale our node.js applications so for those of you who don't know that node.js processes are single threaded that is whenever you start your application it runs on a single thread that is inside a single process and if you have a multi-core cpu then it won't be utilizing the full power of that cpu it would only be using the single core of that cpu so to scale our application we would be using the cluster module and even if you don't have multi-core cpu if you already have a single core cpu then also i would suggest you to start your applications using the cluster module because it would provide you with zero downtime that is if in any case any of your instances go down then the second instance would be up there running so before diving into the code let's see the results first so here at the top we have the results for multiple instances of our application and here at the bottom we have the results for single instance so the target url is the same url here that is localhost port 3000 and then the number of request is thousand request with a concurrency level of 100 and we see that the total time taken to complete the request is 28.91 seconds in a single instance application that is running on a single thread and the request per second that a server could handle is only 35 and if we look at the multi-instance application then we see that the requests are same the concurrency level is same but the total time this time taken to complete the request is only about 9.29 milliseconds and the total number of requests per second that our server could handle is 108. that is about three times faster so now let's do some coding and let's see that how we can achieve this result and after completing the application we would be again doing the load testing so the result would be in front of your eyes so let's go back to vs code and here i've created a new npm project and if you look at the package.json file we have only two dependencies that is the express framework and the nodemon dependency and we have a single start script here that says notebond app.js so now let me close this package.json file and here i am using one extension called riskclient so this is the extension which i am going to use to make a request to our server so if you don't know how to use risk line you can go and watch my video that is linked above and it's in the description also so it is super simple to use you simply install this extension and you simply create a file called rest.http and here we can define a request like this since we are making a get request so we type in the get keyword here and then the url to which we want to make a get request so now let's go to our app.js file so here i have initialized a very basic express application and it only has an index route here as you can see here and then inside the index route i am making some long running task that is going on after sending back a response of 4k here and then we are listening on port 3000 and then we simply have a lock statement here so now if i simply do npm start then we see that the server is started on localhost port 3000 and if we go to rest.http and if we make a request here we see that we get backup response that says okay so now let's close this and let's go to our wrap.js file so now here firstly to use the cluster module what we need to do we need to require the cluster module and we do not need to install any third party package this is all built inside node.js so what we can do we can simply say const cluster equal to require cluster like this and then cost os equal to require os that is the operating system package and why i'm using this os package it is because to get the number of cpus or the number of cores present inside our processor so we can say cons num cpu equal to os dot cpus dot length it will give us the number of codes present inside our cpu and now instead of listening to port 3000 what we can you do we can use the cluster module to check whether the process is a master process if it is a master process then we simply fork a new worker process and then we will start listening inside the worker process and the master process would not be listening for any request but only the worker processes would be listening on some port so now firstly what we need to do we need to check if it is a master process if it is a master process then we create a new worker process as the number of cpus present inside our system so we can do it like this so if cluster is master if it is a master process then we need to do something otherwise we need to do something else so if it is a master process then we need to create a new worker process so for that we need to create as many workers as the number of cpus present inside our processor so we can do it using the for loop here so for let i equal to zero i is less than number of cpus and then i plus plus simply like this and now what we need to do inside this for loop we simply need to use one method of the cluster module and that is the fork mod fork method so we can say cluster.4 to create a new worker process and basically this cluster.4 uses the child processes dot fork method and if you want to learn more about child processes then you can watch the video that is linked here at the top so now coming back here so whenever we fork here then it will create a new worker process so we know that if we are in this else block then we are sure that it is a worker process because here we are making an if check that is if it is a master process then we are doing this thing here otherwise we are doing this thing here so we are sure that we are inside a worker process and now what the worker will do worker will simply listen on this port 3000 so let's copy this line and we can simply paste it here and the beauty of these workers is this that they all share the same port we do not have to create different ports for different workers and now what more we can lock out here we can simply log out the process pid so we can simply pass in dollar process dot pid like this just to make sure that we are getting different pids for each of the workers so now the work is already done all or almost done so now if we save this application the application restarted because of node mod and we see here that our server started on these four processes with these pids that is five six five seven five eight and five 9. and now if we make a get request here we see that we are getting back our response but now let's see that which worker is sending this response back so let's go to our app.js file and let me minimize this thing here so here whenever we are sending back our response let me add the process pid so to see that which worker is sending back us a response so we can simply use the dollar thing here that is dollar process dot pid like this and now we can save our app.js file the application again restarted and the ids of the workers this time are these that is 7475767 so now if we make a request here we see that we get a response from seven five that is two eight zero seven five and if we make again a request we get back our response from seven seven again seven six again seven four and again seven five so basically this cluster module uses the round robin approach that is firstly the first request would be handled by seven five the second would be handled by seven seven then seven six then seven four and then again by seven five so this is how this cluster module works so now let's take it one step further so let's go back to our app.js file and what i want to do after sending back the response i want to kill this pro processor that is whichever worker is responding to a request i want to kill that worker so what we can do we can simply use this cluster thing here that is cluster dot worker that is the current worker dot kill so now let's save this and the application would restart and now here inside this if block here we can listen for an event on this cluster here so we can say cluster dot on exit that is if any of the worker died so here we have the worker then the code and the signal like this and now here what we can simply make a log statement that is this worker died so we can simply make a log statement so console.log and here we can use the backtext so we can say worker and here we can log in the id of the worker and we can simply say died that is the worker with this process id has been killed so we can simply get the workers process id like this that is worker dot process dot pid like this so now let's save this application the application again restarted on these processes that is with these ids here so now let's go to our rest.http and as soon as we make a request we will see that one of the workers will die so let's make a request so we see that we get a backup response from 108 and that zero eight worker has died so if we know that we started these three for these four workers here and now only three are left because one zero eight has been killed so if we again make a request 106 has been killed if we again make a request 107 has been killed and now we are left only with one worker so if we again make a request we see that all the four worker processes have been killed so if we again make a request we won't be getting back any response because there are no processes left to handle our request so in this case what we can do we can go to our wrap.js file again and whenever we see that a worker has been killed what we can do we can simply fork another worker by doing cluster dot 4 simply like this and now as soon as a worker is killed or it has crashed for any reason then a new instance of that worker would be initialized and we always have our four instances up and running so this is why i was telling you that even if you don't have multi-core cpu if you only have a single core cpu then instead of using num cpu here you can provide in a number here that is two or three and then you will have that many instances of your application running so that your application should have zero downtime so now let's save this application and this time let's see what happens so let's go to our rest restaurant here so let me make a request here we see that 117 died but we forked another worker here that is one to zero if we may again make a request we see one one five died but we again forged a new worker with this process id that is one two one and if we keep on making request here our worker will die but a new worker will get initialized and our application would always have four instances so now this is about this thing here so now let me comment this thing out from here that is cluster dot worker dot kill and now let's jump into load testing so here we have the four instances up and running so firstly let's make a load test of this application with four instances so let's go here at this line here and for this what i've done i am using this package here that is load test that is provided by npm so you can simply install it globally that is npm install ifng load test it is super simple to use or if you prefer to use apache benchmark that is fine also so here let me make a load test so load test hyphen n that is the number of requests would be thousand and the number and the concurrency level would be hundred by providing an hyphen c and then the url so http local host port 3000 so now let's press enter and let's see the time taken to complete the request since we are running four instances so it might take a moment or so so let's wait and cross our fingers so we see here that this time the number of requests were thousand the concurrency level was 100 and the total time taken is about 10.49 seconds and the request per second that are handled is 95. so now let's see the load test of a single instance so let's go here and before doing so we need to change this code back to be used as a single instance so we can comment this thing out from here and we can start our application on a single port here that is on port 3000 so let me save this and now we have a in single instance application and now let's go back to our terminal here and let me again make a load test using the same values that is the thousand request with a concurrency level of 100 so if we press enter here and this time it might take 30 seconds so i'll be simply fast forwarding this so now here we see that again the total number of requests for thousand concurrency level of 100 the total time taken is 31 seconds about 31 seconds and the request per second is only 32 so the result is in front of your eyes so it depends on you whether you want to use the cluster module or not so i'd leave it to you so as that is all about this video so if you have liked the video do hit the like button and if you haven't subscribed to the channel do subscribe to the channel so thank you bye tata take care and have a good day and have a great day
Info
Channel: yoursTRULY
Views: 7,063
Rating: 4.977591 out of 5
Keywords: scale nodejs application, scale nodejs, scale node to 1 million users, how to scale a nodejs application, nodejs cluster, nodejs cluster express, nodejs cluster tutorial, nodejs cluster example, nodejs cluster vs pm2, nodejs cluster module, nodejs cluster performance, nodejs cluster load balancing, nodejs cluster round robin, nodejs cluster vs kubernetes, yourstruly
Id: 9RLeLngtQ3A
Channel Id: undefined
Length: 13min 36sec (816 seconds)
Published: Wed Jan 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.