Node.js Course
Node.js
/
Advanced

Clustering

Definition

A technique used to scale a Node.js application by spawning a pool of identical worker processes that share the same server port.

Explain Like I'm New

Node.js runs on a single core. If you buy a massive server with 32 CPU cores, Node will only use 1, leaving 31 cores doing absolutely nothing while your app crashes from too much traffic. Clustering tells Node to clone itself 32 times. One 'Master' clone acts as the boss, taking incoming network traffic and distributing it evenly to the 32 'Worker' clones. Now you are using 100% of your server's power.

Real World Example

Using the built-in `cluster` module or `PM2` to automatically scale an Express API across all available CPU cores on an AWS EC2 instance.

Common Use Cases

  • •Vertical scaling
  • •Maximizing CPU utilization
  • •Zero-downtime restarts

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • How many CPU cores does standard Node.js utilize by default?

intermediate

  • Do clustered worker processes share the same memory (RAM)?

Flash Cards

Question

How many cores by default?

Click to reveal answer
Answer

Only 1. The V8 JavaScript engine is strictly single-threaded.

Question

Do they share memory?

Click to reveal answer
Answer

No. Each worker is a completely separate Node process with its own V8 instance, Event Loop, and Memory space. If Worker 1 saves a user to `global.users = []`, Worker 2 will NOT see that user. This is why you must use external databases like Redis for shared state.