Performance: Forking workers per CPU core
MEDIUM IMPACT
This pattern affects server-side request handling speed and responsiveness by utilizing multiple CPU cores to handle concurrent tasks.
import cluster from 'cluster'; import http from 'http'; import os from 'os'; if (cluster.isPrimary) { const cpuCount = os.cpus().length; for (let i = 0; i < cpuCount; i++) { cluster.fork(); } } else { http.createServer((req, res) => { res.writeHead(200); res.end('Hello World'); }).listen(8000); }
import cluster from 'cluster'; import http from 'http'; if (cluster.isMaster) { // Fork only one worker cluster.fork(); } else { http.createServer((req, res) => { res.writeHead(200); res.end('Hello World'); }).listen(8000); }
| Pattern | CPU Utilization | Request Concurrency | Response Delay | Verdict |
|---|---|---|---|---|
| Single worker process | Low (uses 1 core) | Low (serial requests) | High (requests queue) | [X] Bad |
| One worker per CPU core | High (uses all cores) | High (parallel requests) | Low (fast responses) | [OK] Good |