Skip to content
tezvyn:
Menu

What does PM2 do for Node.js applications?

Source: interviewEasyHow cards are made

Summary

production process lifecycle and automatic recovery.

Key points

PM2 restarts crashed processes and manages multiple workers.

Key points

solves (1) app crash requiring manual restart, (2) single-process bottleneck.

Process supervision

When a Node.js app crashes, it exits immediately. Users see a connection error. Without PM2, someone must SSH to the server and restart the process manually, which is slow and error-prone. PM2 automatically watches the process and restarts it on exit, minimizing downtime. Combined with health checks, a crash is an invisible blip, not an outage.

Cluster mode for concurrency

A single Node.js process can only use one CPU core. Modern servers have 8, 16, or more cores sitting idle. PM2 cluster mode forks multiple worker processes, automatically distributing traffic. This solves the single-threaded bottleneck without code changes. The same application handles 8x more throughput.

Key problem 1

MANUAL RESTARTS

Without PM2, a crash requires manual intervention. PM2 monitors the process in the background and restarts it automatically, keeping the application available. This is especially important for long-running servers where someone might not notice a crash immediately.

Key problem 2

UNDERUTILIZED HARDWARE

A server with 8 cores but a single Node.js process wastes 7 cores. PM2 cluster mode forks 8 workers, each with its own event loop. Traffic is distributed via a round-robin load balancer within PM2, using all cores efficiently. This increases throughput without code changes.

Graceful restarts

PM2 can gracefully restart workers one by one, draining connections before termination. This enables zero-downtime deployments. The master process forks a new worker, drains the old one, and removes it, repeat for each worker.

Monitoring and logging

PM2 provides a dashboard (pm2 monit) showing CPU, memory, and restart counts. Logs are centralized. This visibility helps catch resource leaks or problems early. Without PM2, you rely on system tools like top, which is less integrated.

Interview question

Your Node.js app crashes due to an unhandled error. With PM2 running, what happens next?

  • a.PM2 automatically restarts the process, minimizing downtime.Correct
  • b.PM2 logs the error but doesn't restart; only a human operator can fix it.
  • c.The server refuses to accept new connections until the process is restarted.
  • d.The process stays dead until you manually restart it with 'pm2 restart'.
Why?

PM2 monitors the process and restarts it automatically on crash. This is one of its main purposes: providing automatic recovery without human intervention. Manual restart commands are unnecessary. PM2 doesn't just log; it takes action. The app is unavailable briefly during restart, but the process returns automatically.

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

Read the original → pm2.keymetrics.io

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

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. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles