https://github.com/than-dev/increase-node-performance
Increasing nodejs performance with cluster and work threads. Explanations, use cases and benchmarks, enjoy it.
https://github.com/than-dev/increase-node-performance
backend benchmark clustering nodejs performance worker-threads
Last synced: 2 months ago
JSON representation
Increasing nodejs performance with cluster and work threads. Explanations, use cases and benchmarks, enjoy it.
- Host: GitHub
- URL: https://github.com/than-dev/increase-node-performance
- Owner: than-dev
- License: mit
- Created: 2021-09-16T21:38:34.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-01-03T00:28:52.000Z (over 3 years ago)
- Last Synced: 2025-02-05T14:48:53.817Z (4 months ago)
- Topics: backend, benchmark, clustering, nodejs, performance, worker-threads
- Language: JavaScript
- Homepage:
- Size: 556 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Enhance NodeJS Performance
[](/LICENSE)
## ➰ Two different ways to do it
- (Recommended) Node 'Cluster' mode
- (Experimental) Worker Threads
## 🐊 Start
First we need to understand the default application flux, that is:
```
Request ----> Node Server ----> Response
```It causes a block in all the event loop, this way, in request process much longs, we can lose performance and get worse the user experience.
## 🛠️ Cluster Module
The clustering process creates and manages some nodejs event loop instances, winning this way mor threads to execute our code, action that will increase our speed. This instances are monitored by the Cluster Manager
Follows a visual representation:
![]()
This basically call a lot of times the server index file, in the first execution, it creates the Cluster Manager and then it will create some new workers instances (child process). The command to create children process:
```
cluster.fork()
```
Command to verify if the process is the primary(first execution):
```
cluster.isPrimary(): Boolean
```
### Benefits
Using it you can break your server flux in most pieces, this way you will process more then one request at a time.
### Disadvantages and Not Usecases
If we increase the number of children process, will arrive a moment that our performance will decrease, because all machines have a limit of processing, because it, is recommendable we use this logic:```
const numCPUs = cpus().length;for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
```
### Conclusion
To use this feature we need to be wary before, you can fill free to do your tests and benchmarks using the file _cluster.js as base. Prefer to use it when you need big processing in the request.
## 🔨 Worker Threads
Just use it if you have a big business logic that can take much time.Our app communicate with the webworker through this flux:
Our app postMessage, and the webWorker stay listening an event 'onMessage', like the nodejs Event Emitter.
The applied example in the file _workers-threads.js is at nodejs documentation, see it to more details.
### 🛸 Author: Nathan Cotrim - MIT License