Node.js single-threaded chỉ dùng một CPU core — server 16-core lãng phí 15 cores.
- Cluster module: master process fork N worker processes (thường = số CPU cores:
os.cpus().length), mỗi worker là Node.js process độc lập, share cùng port. - Connection distribution: Linux dùng round-robin (master nhận connection rồi distribute), Windows dùng OS scheduling.
- IPC communication:
process.send(msg)từ worker,worker.on('message', handler)ở master — dùng để share state nhỏ như worker health. - Shared nothing architecture: workers không share memory, mỗi worker có heap riêng — session/cache phải dùng Redis thay vì in-memory.
- Sticky sessions: cần khi dùng WebSocket — load balancer phải route cùng client đến cùng worker (nginx
ip_hash). - PM2 cluster mode:
pm2 start app.js -i maxtự động quản lý cluster với zero-downtime reload (pm2 reload app) — reload từng worker một thay vì restart toàn bộ. - Khi nào dùng cluster vs reverse proxy: cluster cho vertical scaling trên một server, Nginx/HAProxy cho horizontal scaling nhiều servers.
Node.js is single-threaded and only uses one CPU core — a 16-core server wastes 15 cores.
- Cluster module: the master process forks N worker processes (typically equal to the number of CPU cores:
os.cpus().length), each worker is an independent Node.js process sharing the same port. - Connection distribution: Linux uses round-robin (master receives the connection then distributes), Windows uses OS scheduling.
- IPC communication:
process.send(msg)from the worker,worker.on('message', handler)on the master — used to share small state like worker health. - Shared nothing architecture: workers don't share memory; each has its own heap — sessions/cache must use Redis, not in-memory storage.
- Sticky sessions: required for WebSockets — the load balancer must route the same client to the same worker (nginx
ip_hash). - PM2 cluster mode:
pm2 start app.js -i maxautomatically manages the cluster with zero-downtime reload (pm2 reload app) — restarts workers one at a time instead of all at once. - When to use cluster vs reverse proxy: cluster for vertical scaling on one server, Nginx/HAProxy for horizontal scaling across multiple servers.