Posted on Updated on - Arjun

Express js optimize performance with clusters

You can increase the performance of your Express.js application by using clusters. Cluster is a native core library from node.js which allows you to implement a network of multiple processes. Usually an node js process will only use one core process meaning we are not using all available CPU power, using clusters we can spin up multiple processes.

In node.js you can create N-1 process to work with parallel processing, where the master process works on balancing the load among the other child salve process. Child processes communicate with master process via IPC(Inter Process Communication).

express js cluster

A cluster basically runs several instances of one single application, usually one instance in each core of the CPU, which distributes the overall task among many instances and sharing the load. Cluster allows multiple process to listen on the same master port, which means master process receives the request and distribute it to child processes. Clusters adds the additional bandwidth to the application.

We can get following benefits with clustering, better performance, lower cost, Greater reliability, easy maintenance.

Start node server in cluster mode

Save below code in cluster.js file and update your server file(server.js).

Sample express server

You can run your cluster server and normal server with node cluster.js , node server.js respectively.
My server output –

Thanks it.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

Arjun
I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.
DMCA.com Protection Status