4

I am very new to Node.js and express. I am currently learning it by building my own services. I recently read about clusters. I understood what clusters do. What I am not able to understand is how to make use of clusters in a production application.

One way I can think of is to use the Master process to just sit in front and route the incoming request to the next available child process in a round robin fashion. I am not sure if this is how it is designed to be used. I would like to know how should clusters be used in a typical web application.

Thanks.

2 Answers 2

2

The node.js cluster modules is used with node.js any time you want to spread out the request processing across multiple node.js processes. This is most often used when you wish to increase your ability to handle more requests/second and you have multiple CPU cores in your server. By default, a single instance of node.js will not fully utilize multiple cores because the core Javascript you run in your server is single threaded (uses one core). Node.js itself does use threads for some things internally, but that's still unlikely to fully utilize a mult-core system. Setting up a clustered node.js process for each CPU core will allow you to better maximize the available compute resources.

Clustering also provides you with some additional fault tolerance. If one cluster process goes down, you can still have other live clusters serving requests while the disabled cluster restarts.

The cluster module for node.js has a couple different scheduling algorithms - the round robin you mention is one. You can read more about that here: Cluster Round-Robin Load Balancing.

Because each cluster is a separate process, there is no automatic shared data among the different cluster processes. As such, clustering is simplest to implement either where there is no shared data or where the shared data is already in a place that it can be accessed by multiple processes (such as in a database).

Keep in mind that a single node.js process (if written to properly use async I/O and not heavily compute bound) can server many requests itself at once. Clustering is when you want to expand scalability beyond what one instance can deliver.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks.Would you suggest the Master to be built in such a way that, when it gets a request, it will redirect it to an available child process and child process actually takes care of fetching data from DB and doing some processing on it and returning it back to the UI?
@Photon - I'd suggest you read how the node.js cluster module works. A usual model is to just let it round robin requests and the receiving cluster processes the entire request. As far as the receiving process is concerned, it just gets an incoming request and does its normal processing on it.
@jfriend00 does the cluster utilizes same event loop?
@NaveenKerati - Yes, it uses the same event loop. Incoming requests arrive as incoming TCP requests using the normal TCP events and event loop. They are then load balanced to the clusters using interprocess communication to the clustered processes which also run via the event loop. All processes are normal node.js apps using the regular event loop.
0

I have created a poc on cluster in nodejs and added some details in the below blogs. Once go through it. It may provide some clearance.

https://jksnu.blogspot.com/2022/02/cluster-in-node-js-application.html

https://jksnu.blogspot.com/2022/02/cluster-management-in-node-js.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.