❮ Built-in Modules
Example
Run the code three times, the first time is as a master, then as workers:
var cluster = require('cluster');
if (cluster.isWorker) {
console.log('I am a worker');
} else {
console.log('I am a master');
cluster.fork();
cluster.fork();
}
Run example »
Definition and Usage
The cluster module provides a way of creating child processes that runs simultaneously and share the same server port.
Node.js runs single threaded programming, which is very memory efficient, but to take advantage of computers multi-core systems, the Cluster module allows you to easily create child processes that each runs on their own single thread, to handle the load.
Syntax
The syntax for including the cluster module in your application:
var cluster = require('cluster');
Cluster Properties and Methods
Method | Description |
disconnect() | Disconnects all workers |
exitedAfterDisconnect | Returns true if a worker was exited after disconnect, or the kill method |
fork() | Creates a new worker, from a master |
id | A unique id for a worker |
isConnected | Returns true if the worker is connected to its master, otherwise false |
isDead | Returns true if the worker's process is dead, otherwise false |
isMaster | Returns true if the current process is master, otherwise false |
isWorker | Returns true if the current process is worker, otherwise false |
kill() | Kills the current worker |
process | Returns the global Child Process |
schedulingPolicy | Sets or gets the schedulingPolicy |
send() | sends a message to a master or a worker |
settings | Returns an object containing the cluster's settings |
setupMaster() | Changes the settings of a cluster |
worker | Returns the current worker object |
workers | Returns all workers of a master |
❮ Built-in Modules
W3schools Pathfinder
Track your progress - it's free!
FAQs
Worker threads operate at thread level, providing a way to run JavaScript code in parallel within a single process. Clusters operate at process level, allowing you to create multiple Node. js processes (workers) to handle incoming network requests.
How does a node cluster work? ›
Clustering in Node. js involves creating multiple worker processes that share the incoming workload. Each worker process runs in its own event loop, utilizing the available CPU cores. The master process manages the worker processes, distributes incoming requests, and handles process failures.
How to create a cluster in NodeJS? ›
js comes bundled up with a cluster module. The cluster module permits the creation of child processes, that are copies of your program operating concurrently on the same server port. Each child process possesses an event loop, V8 instance and memory. There is a parent process routing traffic to these child processes.
What is the difference between worker node and cluster? ›
Node is a worker computer in Kubernetes that can be either virtual or real, depending on the cluster. The control plane manages each node and the container operates logically in a pod, but it also requires a container runtime. A cluster is a collection of several nodes in a Kubernetes system.
When to use worker threads in NodeJS? ›
Use worker threads when:
- You're running CPU-intensive tasks. If your tasks are CPU-intensive, worker threads are a good choice.
- Your tasks require shared memory and efficient communication between threads. Worker threads have built-in support for shared memory and a messaging system for communication.
How does the cluster module work? ›
The Node. js cluster module is a built-in module that allows you to create a cluster of child processes. Each child process runs on a separate core of the CPU, allowing you to take full advantage of multi-core processors. The cluster module provides several methods for creating and managing child processes.
What is the primary purpose of cluster module in node JS? ›
The cluster module provides a way of creating child processes that runs simultaneously and share the same server port.
When to use nodejs cluster? ›
To summarize, in Node, we can use the cluster module to do load balancing of requests as they come into our node HTTP servers. And the cluster module uses the round-robin approach to determine which process will handle those incoming requests, the requests, which are the load being balanced across your server.
How many nodes can be there in a cluster? ›
A cluster is a set of nodes (physical or virtual machines) running Kubernetes agents, managed by the control plane. Kubernetes v1. 30 supports clusters with up to 5,000 nodes.
Can we create single node cluster? ›
You can create a single node cluster by selecting "Single Node (1 master, 0 workers)" on the Cluster type section of the Set up cluster panel on the Dataproc Create a cluster page.
To increase the size of a cluster's node pools, run the gcloud container clusters resize command: See more code actions. Replace the following: CLUSTER_NAME : the name of the cluster to resize.
How do I create two node clusters? ›
Creating a two node cluster is the same as creating any other cluster. You add a one node as the configuration coordinator and the other node as the other cluster node. Incremental configuration synchronization is not supported in a two-node cluster. Only full synchronization is supported.
How many worker nodes in a cluster? ›
The total number of nodes required for a cluster varies, depending on the organization's needs. However, as a basic and general guideline, have at least a dozen worker nodes and two master nodes for any cluster where availability is a priority.
Is a cluster node a server? ›
Clusters are groups of servers that are managed together and participate in workload management. A cluster can contain nodes or individual application servers. A node is usually a physical computer system with a distinct host IP address that is running one or more application servers.
What is the relationship between a node and a cluster? ›
Essentially, the nodes share resources with each other and act as a single powerful machine — the cluster. Because the nodes are workers (or in the train analogy, the separate train cars), the cluster master oversees activity on each node, much like a train conductor monitors what's happening inside each train car.
What are worker threads? ›
Worker threads are a means to execute different tasks in multiple parallel contexts of execution in a concurrent manner, which can take advantage of multiprocessor and multithreaded environments as well as to keep UI Thread in Application responsive by delegating or offloading work which need not be handled in UI Main ...
What is the difference between main thread and worker thread? ›
Any code that updates the UI or interacts with the user should be run on the main thread. Worker threads are used for background tasks that should not block the main thread, such as network requests, database operations, and image processing.
What is the difference between worker thread and normal thread? ›
By default, a . NET program is started with a single thread, often called the primary thread. However, it can create additional threads to execute code in parallel or concurrently with the primary thread. These threads are often called worker threads.
How does the cluster module work what's the difference between it and a load balancer? ›
The main difference between web server clustering and load balancing is that web server clustering creates a single logical system from multiple web servers, while web server load balancing distributes the requests among multiple independent web servers.