Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Using PM2 to manage NodeJS cluster (3/4)
Antonio Santiago
Antonio Santiago

Posted on

     

Using PM2 to manage NodeJS cluster (3/4)

The cluster module allows us to create worker processes to improve our NodeJS applications performance. This is specially important in web applications, where a master process receives all the requests and load balances them among the worker processes.

But all this power comes with the cost that must be the application who manages all the complexity associated with process managements: what happens if a worker process exists unexpectedly, how exit gracefully the worker processes, what if you need to restart all your workers, etc.

In this post we presentPM2 tool. although it is a general process manager, that means it can manage any kind of process like python, ruby, ... and not only NodeJS processes, the tool is specially designed to manage NodeJS applications that want to work with the cluster module.


More on this series:

  1. Understanding the NodeJS cluster module
  2. Using cluster module with HTTP servers
  3. Using PM2 to manage a NodeJS cluster
  4. Graceful shutdown NodeJS HTTP server when using PM2

Introducing PM2

As said previously, PM2 is a general process manager, that is, a program that controls the execution of other process (like a python program that check if you have new emails) and does things like: check your process is running, re-execute your process if for some reason it exits unexpectedly, log its output, etc.

The most important thing for us is PM2 simplifies the execution of NodeJS applications to run as a cluster. Yes, you write your application without worrying about cluster module and is PM2 who creates a given number of worker processes to run your application.

The hard part of cluster module

Lets see an example where we create a very basic HTTP server using the cluster module. The master process will spawn as many workers as CPUs and will take care if any of the workers exists to spawn a new worker.

constcluster=require('cluster');consthttp=require('http');constnumCPUs=require('os').cpus().length;if(cluster.isMaster){masterProcess();}else{childProcess();}functionmasterProcess(){console.log(`Master${process.pid} is running`);for(leti=0;i<numCPUs;i++){console.log(`Forking process number${i}...`);cluster.fork();}cluster.on('exit',(worker,code,signal)=>{console.log(`Worker${worker.process.pid} died`);console.log(`Forking a new process...`);cluster.fork();});}functionchildProcess(){console.log(`Worker${process.pid} started...`);http.createServer((req,res)=>{res.writeHead(200);res.end('Hello World');process.exit(1);}).listen(3000);}
Enter fullscreen modeExit fullscreen mode

The worker process is a very simple HTTP server listening on port 3000 and programmed to return aHello World and exit (to simulate a failure).

If we run the program with$ node app.js the output will show something like:

$node app.jsMaster 2398 is runningForking process number 0...Forking process number 1...Worker 2399 started...Worker 2400 started...
Enter fullscreen modeExit fullscreen mode

If we go to browser at URLhttp://localhost:3000 we will get aHello World and in the console see something like:

Worker 2400 diedForking a new process...Worker 2401 started...
Enter fullscreen modeExit fullscreen mode

That's very nice, now lets go to see how PM2 can simplify our application.

The PM2 way

Before continue, you need to instal PM2 on your system. Typically it is installed as a global module with$ npm install pm2 -g or$ yarn global add pm2.

When using PM2 we can forget the part of the code related with the master process, that will responsibility of PM2, so our very basic HTTP server can be rewritten as:

consthttp=require('http');console.log(`Worker${process.pid} started...`);http.createServer((req,res)=>{res.writeHead(200);res.end('Hello World');process.exit(1);}).listen(3000);
Enter fullscreen modeExit fullscreen mode

Now run PM2 with$ pm2 start app.js -i 3 and you will see an output similar to:

Note the option-i that is used to indicate the number of instances to create. The idea is that number be the same as your number of CPU cores. If you don't know them you can set-i 0 to leave PM2 detect it automatically.

$pm2 start app.js-i 3[PM2] Starting /Users/blablabla/some-project/app.jsincluster_mode(3 instances)[PM2] Done.| Name      | mode    | status | ↺ | cpu | memory    ||----------|---------|--------|---|-----|-----------|| app       | cluster | online | 0 | 23% | 27.1 MB   || app       | cluster | online | 0 | 26% | 27.3 MB   || app       | cluster | online | 0 | 14% | 25.1 MB   |
Enter fullscreen modeExit fullscreen mode

We can see the application logs running$ pm2 log. Now when accessing the thehttp://localhost:3000 URL we will see logs similar to:

PM2        | App name:appid:0 disconnectedPM2        | App[app] withid[0] and pid[1299], exited with code[1] via signal[SIGINT]PM2        | Starting execution sequencein-cluster mode-forapp name:appid:0PM2        | App name:appid:0 online0|app      | Worker 1489 started...
Enter fullscreen modeExit fullscreen mode

We can see how PM2 process detects one of our workers has exit and automatically starts a new instance.

Conclusions

Although the NodeJS cluster module is a powerful mechanism to improve performance it comes at the cost of complexity required to manage all the situations an application can found: what happens if a worker exists, how can we reload the application cluster without down time, etc.

PM2 is a process manager specially designed to work with NodeJS clusters. It allow to cluster an application, restart or reload, without the required code complexity in addition to offer tools to see log outputs, monitorization, etc.

References

Node.js clustering made easy with PM2

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
omancoding profile image
OmanCoding
www.twitter.com/omancoding
  • Location
    Muscat, Oman
  • Work
    Software Engineer
  • Joined

If there is a node js module for handling REST requests, will running two identical instances of it using PM2 make the performance (throughput) better - (note it is in fork mode since each slightly has different configuration)?

CollapseExpand
 
leoplaw profile image
leoplaw
  • Joined

Do you know how to cluster with PM2 programmatically?
Can you point to any examples please?

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software engineer as profession and hobby @joppyme co-founder former @letgo and @trovit Creator of the stupid initiative #deployFridayFest 🤯 Talk with me at #ThePonycorns 🦄 discord.gg/98qwsBNAfr
  • Joined

More fromAntonio Santiago

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp