Improve NodeJs Application Performance via Cluster Mode

Improve NodeJs Application Performance via Cluster Mode

Every developer wants to have a high-performance app, particularly a backend service, which is consumed by different clients. There are two ways or more ways we can improve NodeJs Application Performance.

  1. Cluster mode
  2. Use Worker Thread

In This Article, We are going to focus on Cluster-Mode.

Cluster mode: is used to start up multiple copies of nodes that are all running your server inside them We cannot trick nodes into running with multiple threads.

By starting multiple copies of the node, we get multiple copies of the event loop.

When we start using clustering in our node application, we basically start up multiple node processes.

Clustering provides us the opportunity to have multiple instances of our application in one machine.

There is alway going to be one parent process called cluster manager. Cluster manager is responsible for managing the health status of individual instants of our application.

Cluster manager does not execute application code. Cluster manager can start instances, restart instances, send them data and do other administrative tasks.

Individual instance of our application are responsible for handling incoming request, fetching data from the database, execute cron job, handle authentication and serving up static assets.

Cluster module is standard nodeJs Module that allow us create cluster manager and fork multiple instance of our nodeJs Application.

The very first, we execute our node application, our cluster manager is setup. Every time after that, our worker instance is going to be produce.

The cluster module has a function (cluster.fork()), which excuse our nodeJs application the second time which then startup our worker instance.

Read More Here