Categories
Node.js Best Practices

Node.js Best Practices — Process Managers

Spread the love

Like any kind of apps, JavaScript apps also have to be written well.

Otherwise, we run into all kinds of issues later on.

In this article, we’ll look at some best practices we should follow when writing Node apps.

Use a Process Manager

We should use a process manager to keep our Node app running even when it crashes.

A crash will bring down a Node app.

It’ll be offline until we restart it.

Therefore, we can’t just run it with node app.js or something similar.

A process manager helps our app maintain high availability.

It lets us gain insights into runtime performance and resource consumption.

Settings can be modified dynamically to improve performance.

We can also control clustering with StrongLoop PM and PM2.

StrongLoop PM has features that target production deployment.

We can build and package apps locally and deploy it securely to our production system.

Also, it automatically restarts our app if it crashes.

It also lets us manage clusters remotely.

CPU profiles and heap snapshots let us check for memory leaks and CPU usage.

And we can scale to multiple hosts with integrated control for the Nginx load balancer.

Use an Init System

An init system provides more reliability in that it ensures the apps start when the server restarts.

They can go down for many reasons so we should make sure our app starts again when it starts.

We can run our app in a process manager and install the process manager as a service with the init system.

The process manager would restart our app when the app crashes.

The init system will restart the process manager when the OS crashes.

We can also run our app directly in the init system.

This is simpler but we don’t get the additional privileges of using a process manager.

The 2 main init systems are systemd and Upstart.

Using Node’s Cluster Module

Node’s cluster module lets us create multiple instances of one app.

It enables a master process to spawn worker processes and distribute incoming connections among workers.

We can use StrongLoop PM to create a cluster without modifying application code.

StrongLoop PM automatically runs in a cluster with the number of workers equal to the number of CPU cores in a system.

We can manually change the number of worker processes in a cluster using the slc program without stopping the app.

For example, we can run an app with the given cluster size by writing:

slc ctl -C http://prod.example.com:8888 set-size my-app 8

We set the cluster size to 8 with the number 8 at the end.

PM2 also lets us create clusters without modifying our app’s code.

We must ensure that are app is stateless.

This means that no local data should be stored in the process like sessions, WebSocket connections, etc.

We can then enable cluster mode by running:

$ pm2 start app.js -i 4  
$ pm2 start app.js -i max

We run a cluster with 4 worker processes with the 4 at the end.

Or we can use max to all the CPUs and start that many worker processes.

To add more workers we can use the plus sign:

$ pm2 scale app +3  
$ pm2 scale app 2

We use scale to change the number of workers.

Conclusion

We can use process managers to create clusters, restart our app, and monitor hardware usage.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *