We use cluster module to scale our application across multiple CPU cores by creating multiple Node.js processes that share the same server port.
- Want to utilize all CPU cores on your machine.
- Your app is I/O-heavy (e.g., APIs, HTTP requests).
- You want automatic restarts of crashed workers.
- The master (primary) process forks child processes (workers).
- All workers share the same port.
- The OS load-balances the incoming connections across them.
- Workers donβt share memory.
- Itβs not true multithreading β it's multi-processing.
- Not suitable for CPU-heavy tasks β use worker_threads instead.
- Node.js
- Express
- Node.js cluster module
# Clone the repo
git clone https://github.com/rupachowrasia/nodejs-scaling-cluster.git
# Move into the project directory
cd nodejs-scaling-cluster
# Install dependencies
npm install
# Run the app
npm run start