Ecosystem File vs CLI-Only Management for Deploying a Stateful Node.js App Under PM2
0 reputation · 12 Apr 2026, 20:35 UTC
0 reputation · 12 Apr 2026, 20:35 UTC
I'm deciding how to standardize PM2 deployments for a Node.js service that runs in cluster mode and holds some in-memory session state. The two documented approaches are starting processes directly with pm2 start app.js and persisting them with pm2 save plus pm2 startup, versus declaring everything in an ecosystem.config.js file under version control.
The CLI route is faster for prototyping, but I worry about configuration drift between staging and production when flags like -i max, watch settings, and environment variables live only in shell history. The ecosystem file makes cluster mode and per-environment variables declarative and reviewable, but adds a layer of indirection for quick local experiments.
There is also a behavioral tension I can't resolve from the docs alone: for a stateful app, pm2 reload promises zero-downtime staggered worker restarts in cluster mode, while pm2 restart hard-kills processes. It's unclear how in-flight connections are drained during a reload.
pm2 reload wait for existing connections to close before replacing a worker, or must connection draining be handled in application code?35275 reputation · 13 Apr 2026, 07:40 UTC
ecosystem.config.js file is more than a maintainability choice; it is an operational necessity. While CLI flags are useful for prototyping, they fail to provide the declarative state required to manage stateful lifecycles across environments.
instances: 'max' or specific counts in a file ensures that the pm2 save command restores the exact topology after a server reboot. CLI flags are lost unless manually recorded in shell history.watch settings to be managed in one code, preventing 'configuration drift' where staging might have different memory limits or restart policies than production.pm2 reload and pm2 restart is resolved by how PM2 handles signals:
SIGINT to a worker, waits for it to exit, and then starts a new one. However, PM2 does not natively wait for in-flight connections to close. If your application code does not explicitly handle SIGINT to stop accepting new requests and finish processing existing ones before calling process.exit(), PM2 will eventually kill the process after the kill_timeout.pm2 reload replaces a worker, any session stored in that specific worker's memory is lost.Diagnostic Question: Is your application currently using a local object to store sessions, or do you have an external data store like Redis configured?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.