-
Notifications
You must be signed in to change notification settings - Fork 14
/
clustering
18 lines (9 loc) · 839 Bytes
/
clustering
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Node processes run in a single thread limited by default to 512Mb on 32-bit machines and 1Gb on 64-bit.
In order to avoid processing power bottlenecks the cluster module can be used to split a single process into multiple ones (workers). Child processes share the same server ports with the master process.
Import the cluster module:
var cluster = require('cluster');
As a cluster module executes the same Node process multiple times, you need to find what portion of the code is executed by the children or master. Identify the master process:
if(cluster.isMaster) { ... };
You can then initiate the master process as it will initiate other worker processes:
cluster.fork();
Most used events contained by clusters are the online and exit emitted either when the worker process is forked or when the worker process is terminated.