싱글 프로세스로 동작하는 노드가 CPU 코어를 모두 사용할 수 있게 해주는 모듈
코어가 8개인 서버가 있어도, 노드는 보통 1개의 코어를 사용함.
(스레드 풀 제외)
cluster 모듈을 설정하면, 코어 하나당 노드 프로세스 하나가 돌아가게 할 수 있음.
근데 프로세스는 메모리를 공유하지 않으므로, 세션을 메모리에 저장하는 경우 문제가 발생할 수 있음.
const cluster = require("cluster");
const http = require("http");
const numCPUs = require("os").cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on("exit", (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died`);
});
} else {
http
.createServer((req, res) => {
res.writeHead(200);
res.end("hello world\n");
})
.listen(8000);
console.log(`Worker ${process.pid} started`);
}


node의 내장 http 모듈로서 서버를 구성할 수 있음.
그러나 이 경우 라우팅 같은 걸 if문으로 다 처리해야 함.
그래더 express 같은 외부 모듈을 쓰면 좀더 편리하게 처리 가능.