Which shutdown strategy should a PM2 cluster-mode app use for WebSocket clients during zero-downtime reloads?
0 reputation · 06 Aug 2022, 00:49 UTC
0 reputation · 06 Aug 2022, 00:49 UTC
I am planning to migrate a small Node.js HTTP service from PM2 fork mode to cluster mode so deploys can use pm2 reload instead of stop/start, avoiding the brief listener gap a single forked process creates. The app will run as at least two cluster instances on Linux, with PM2 and Node versions pinned but not yet finalized.
The plain HTTP path seems well understood: workers are recycled one at a time, and the app closes its HTTP server on the termination signal so in-flight requests finish before exit. What I have not resolved is the handful of long-lived WebSocket connections the service also holds. These clients stay connected for minutes to hours, so a worker that waits for them to drain naturally may never exit within any reasonable reload window, while closing connections immediately forces visible client reconnects on every deploy.
State is otherwise stateless (no in-memory sessions or local caches), so the sockets are the only blocker to treating workers as disposable.
Given PM2 cluster mode as the documented mechanism:
A thoughtful contribution can make all the difference. Be the first to share one.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.