Skip to content
tezvyn:

PM2 Cluster Mode: Scale Node.js Across All Cores

Source: pm2.keymetrics.ioMediumHow cards are made

PM2 Cluster Mode: Scale Node.js Across All Cores

PM2's cluster mode lets your Node.js app run on every CPU core, multiplying its capacity. It's essential for scaling networked apps on a single machine, but requires a stateless design—storing sessions in memory will break things as requests hit different…

Why it exists

Node.js is famously single-threaded. On a server with multiple CPU cores, a standard Node process uses only one, wasting the rest of the machine's power. PM2's cluster mode was created to easily utilize all available CPU power for a single application, dramatically increasing its performance and reliability without code changes.

The mental model

Think of your Node.js app as a single cashier. Cluster mode doesn't make that cashier faster; it opens new checkout lanes, one for each CPU core, with a clone of your app at each one. All clones serve different customers simultaneously, but they all use the same backend systems, like a shared database for state. This multiplies the number of customers you can serve at once.

How it works

Under the hood, cluster mode uses the native Node.js cluster module. When you run pm2 start app.js -i max, PM2 creates a primary process that forks multiple 'worker' processes, one for each CPU. This primary process listens on your app's port and distributes incoming connections to the workers, typically in a round-robin fashion. This allows multiple instances of your app to handle requests concurrently on the same port.

When to use it

Use cluster mode for any networked Node.js application, like an Express API or other HTTP/TCP server, running on a multi-core machine. It's the standard first step for vertical scaling on a single server. It also enables zero-downtime reloads using pm2 reload, which restarts workers one by one instead of killing the whole app at once.

When not to use it

Do not use cluster mode if your application is stateful. Each worker is an isolated process with its own memory. If you store user sessions or WebSocket connections in a worker's memory, a user's next request could be routed to a different worker that has no knowledge of that state, breaking the user experience. All shared state must be externalized to a database, Redis, or another shared service.

One canonical example

To scale an API server named api.js across all available CPU cores, the command is pm2 start api.js -i max. In a configuration file, you would set exec_mode: "cluster" and instances: "max". For a zero-downtime update, you would run pm2 reload api. This ensures continuous service availability by restarting each worker sequentially.

Interview question

What is the most crucial architectural consideration for a Node.js application to function correctly when deployed with PM2 Cluster Mode?

  • a.Implementing a custom inter-process communication (IPC) mechanism.
  • b.Designing the application to manage shared state in external services.Correct
  • c.Optimizing the application's single-threaded performance.
  • d.Ensuring all I/O operations are non-blocking.
Why?

The card explicitly states that applications must be stateless when using cluster mode, requiring all shared state (like sessions) to be externalized to avoid issues when requests are routed to different workers. While non-blocking I/O is a general Node.js best practice, it's not the specific architectural challenge introduced by cluster mode.

Just read this? Test yourself on what you have been reading.

Read the original → pm2.keymetrics.io

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on nodejs — each one lists the topics its interview covers.

See open roles