Skip to content

Commit 7da6f22

Browse files
committed
Update README.md
1 parent 053cf82 commit 7da6f22

File tree

1 file changed

+60
-35
lines changed

1 file changed

+60
-35
lines changed

README.md

Lines changed: 60 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,66 @@ Cluster supports two types of load distribution:
13831383
<b><a href="#table-of-contents">↥ back to top</a></b>
13841384
</div>
13851385

1386+
## Q. How does the cluster module work in Node.js?
1387+
1388+
The cluster module provides a way of creating child processes that runs simultaneously and share the same server port.
1389+
1390+
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.
1391+
1392+
**Example:**
1393+
1394+
```js
1395+
/**
1396+
* Cluster Module
1397+
*/
1398+
const cluster = require('cluster');
1399+
1400+
if (cluster.isWorker) {
1401+
console.log('I am a worker');
1402+
} else {
1403+
console.log('I am a master');
1404+
cluster.fork();
1405+
cluster.fork();
1406+
}
1407+
```
1408+
1409+
**Output:**
1410+
1411+
```js
1412+
I am a master
1413+
I am a worker
1414+
I am a worker
1415+
```
1416+
1417+
<div align="right">
1418+
<b><a href="#table-of-contents">↥ back to top</a></b>
1419+
</div>
1420+
1421+
## Q. Explain cluster methods supported by Node.JS?
1422+
1423+
|Method |Description |
1424+
|---------------|-----------------------|
1425+
|disconnect() |Disconnects all workers|
1426+
|exitedAfterDisconnect |Returns true if a worker was exited after disconnect, or the kill method|
1427+
|fork() |Creates a new worker, from a master|
1428+
|id |A unique id for a worker|
1429+
|isConnected |Returns true if the worker is connected to its master, otherwise false|
1430+
|isDead |Returns true if the worker\'s process is dead, otherwise false|
1431+
|isMaster |Returns true if the current process is master, otherwise false|
1432+
|isWorker |Returns true if the current process is worker, otherwise false|
1433+
|kill() |Kills the current worker|
1434+
|process |Returns the global Child Process|
1435+
|schedulingPolicy|Sets or gets the schedulingPolicy|
1436+
|send() |sends a message to a master or a worker|
1437+
|settings |Returns an object containing the cluster\'s settings|
1438+
|setupMaster() |Changes the settings of a cluster|
1439+
|worker |Returns the current worker object|
1440+
|workers |Returns all workers of a master|
1441+
1442+
<div align="right">
1443+
<b><a href="#table-of-contents">↥ back to top</a></b>
1444+
</div>
1445+
13861446
## Q. How to make use of all CPUs in Node.JS?
13871447

13881448
A single instance of Node.js runs in a single thread. To take advantage of multi-core systems, the user will sometimes want to launch a **cluster** of Node.js processes to handle the load. The cluster module allows easy creation of child processes that all share server ports.
@@ -1501,41 +1561,6 @@ Please note - before pid. This converts a pid to a group of pids for process kil
15011561
<b><a href="#table-of-contents">↥ back to top</a></b>
15021562
</div>
15031563

1504-
## Q. How does the cluster module work in Node.js?
1505-
1506-
The cluster module provides a way of creating child processes that runs simultaneously and share the same server port.
1507-
1508-
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.
1509-
1510-
**Example:**
1511-
1512-
```js
1513-
/**
1514-
* Cluster Module
1515-
*/
1516-
const cluster = require('cluster');
1517-
1518-
if (cluster.isWorker) {
1519-
console.log('I am a worker');
1520-
} else {
1521-
console.log('I am a master');
1522-
cluster.fork();
1523-
cluster.fork();
1524-
}
1525-
```
1526-
1527-
**Output:**
1528-
1529-
```js
1530-
I am a master
1531-
I am a worker
1532-
I am a worker
1533-
```
1534-
1535-
<div align="right">
1536-
<b><a href="#table-of-contents">↥ back to top</a></b>
1537-
</div>
1538-
15391564
#### Q. What is load balancer and how it works?
15401565
#### Q. How to improve Node.js performance?
15411566
#### Q. How to manage Node.js clusters?

0 commit comments

Comments
 (0)