Can PM2's reload command achieve zero-downtime migration for a small stateful Node.js application?
0 reputation · 31 Mar 2025, 13:21 UTC
0 reputation · 31 Mar 2025, 13:21 UTC
Goal: Migrate a small Node.js application to PM2 Cluster Mode to enable zero‑downtime reloads while maintaining service availability. Constraint: The application stores user session data in local memory, so a reload that replaces instances may lose that state unless handled. Uncertainty: It is unclear whether PM2’s reload mechanism can preserve in‑memory session data across sequential restarts without external storage or code changes. How can session state be preserved across PM2 reloads without introducing an external store?
No. PM2's reload command provides zero-downtime availability—the listening port stays open and requests continue to be served—but it cannot preserve in-memory session state across worker replacements. Each reloaded worker starts with a fresh JavaScript heap; any sessions stored in local memory are discarded when that worker exits.
pm2 reload restarts workers sequentially (one at a time by default).SIGINT by default).--wait-ready is used, PM2 waits for the new worker to call process.send('ready') before stopping the old one.This sequence guarantees that the service stays up. It does not migrate JavaScript objects, closures, or module-level variables from the old worker to the new one.
Map, object, or array inside the worker process lives only in that process's heap.sticky option or an external load balancer), the session disappears the moment its owning worker is reloaded.To keep sessions across reloads without a network-backed store (Redis, database), you must add application-level code that:
This is a code and architecture change, not a PM2 configuration flag.
Is losing in-memory sessions during a reload acceptable for your use case? If yes, pm2 reload alone gives you zero-downtime availability. If no, you must implement session persistence as described above.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 31 Mar 2025, 14:47 UTC
The key distinction here is between availability and state preservation. PM2's reload achieves zero-downtime in the sense that the TCP socket remains open and new requests are always accepted—but it does not migrate JavaScript objects, closures, or in-process data structures to the replacement worker.
If your application relies on in-memory sessions, a reload will terminate workers one at a time. Each terminated worker loses its session data permanently. Even with cluster mode distributing sessions across workers, only the subset held by the reloading worker disappears, but that still means user sessions are lost.
You can observe this behavior directly: deploy a simple counter in memory, trigger pm2 reload, and confirm the counter resets. This demonstrates that no state handoff occurs between old and new workers.