Mobile & DevOps

Zero-Downtime Deployment With PM2 and GitHub Actions

A deploy pipeline for Node.js and Next.js on a VM that swaps versions without dropping requests, using PM2 cluster reloads, release folders and GitHub Actions.

By Devang GandhiUpdated Nov 3, 20267 min read

A zero-downtime deployment (PM2 plus GitHub Actions) is the cheapest reliability upgrade a VM-hosted product can make. Without it, every deploy means a few seconds of 502 errors, failed form submissions and dropped realtime connections. Teams start deploying late at night to hide it, and then they stop deploying often.

PM2 can swap versions without dropping requests, but only if the app and the pipeline cooperate. This post covers the pieces that make it actually work: cluster mode, ready signals, graceful shutdown, release folders, migration discipline and rollback.

TL;DR

Run the app in PM2 cluster mode with at least two instances. Signal readiness explicitly and shut down gracefully on SIGINT. Build each release into its own folder, switch a current symlink, then pm2 reload. Keep migrations backward compatible, health-check after the reload, and keep previous releases for instant rollback. Trigger it all from a GitHub Actions job.

key takeaways

  • pm2 reload only gives zero downtime when the app runs in cluster mode with at least two instances and shuts down gracefully.
  • Building Next.js in place overwrites files the running server needs, so build into a fresh release folder and switch a symlink.
  • Database migrations must be backward compatible for one release, because old and new code run side by side during a reload.
  • Every deploy needs a health check and a one-command rollback, or zero downtime only holds until the first bad release.

How pm2 reload works, and when it does not

In cluster mode, PM2 runs several instances of your app behind Node's cluster module, sharing one port. On pm2 reload, it starts a new instance, waits until it is ready, then stops one old instance, and repeats until all are replaced.

Setup pm2 restart pm2 reload
Fork mode, 1 instance Downtime Same as restart, downtime
Cluster mode, 1 instance Downtime Short gap possible
Cluster mode, 2+ instances Downtime Zero downtime if app is ready-aware
Cron or queue worker Fine, use fork mode Not needed

The PM2 cluster mode documentation explains the mechanics. The catch is the word "ready". By default PM2 considers an instance ready when it starts listening, which may be before database connections and caches are warm.

Make the app reload-friendly

Signal readiness explicitly

Set wait_ready: true and a listen_timeout in the ecosystem file. In the app, call process.send('ready') only after the server is listening and the database is connected:

await mongoose.connect(process.env.MONGO_URI);
server.listen(PORT, () => process.send && process.send('ready'));

Shut down gracefully

PM2 sends SIGINT to the old instance, then kills it after kill_timeout (default 1600 ms). Handle the signal:

  1. Stop accepting new connections with server.close().
  2. Let in-flight requests finish.
  3. Close database pools and flush logs.
  4. Exit with code 0.

Raise kill_timeout to cover your slowest normal request, such as a report export. If it is too short, PM2 cuts requests off mid-response.

Keep workers out of the cluster

Cron jobs and queue consumers should run as separate fork-mode apps with one instance. Two copies of a reminder cron sends every customer two reminders.

Build into release folders

Running git pull && npm run build in the live directory is the most common reason "zero downtime" deploys still break. Next.js rewrites the .next folder during the build, so the running server can fail to find chunks, and users on open tabs get errors when they navigate.

A release-folder layout fixes this:

/srv/app/
  releases/2026-11-03-1402-a1b2c3/
  releases/2026-11-02-1730-d4e5f6/
  shared/.env
  current -> releases/2026-11-03-1402-a1b2c3

Each deploy:

  1. Creates a new folder under releases/ named with time and commit SHA.
  2. Checks out the commit, links shared/.env, runs npm ci and npm run build there.
  3. Runs pending migrations.
  4. Atomically switches current with ln -sfn.
  5. Runs pm2 reload ecosystem.config.js --update-env, with the ecosystem cwd pointing at current.
  6. Hits a /health endpoint, and rolls back if it fails.
  7. Deletes all but the last five releases.

Because the build happens away from the running code, a failed build never touches production.

Migrations and realtime connections

Backward-compatible migrations

During a reload, old and new instances serve traffic at the same time for a few seconds. A migration that renames a column or field breaks the old instances immediately. Use expand and contract:

  • Release 1: add the new field, write to both, read the old.
  • Release 2: backfill, then read the new field.
  • Release 3: stop writing the old field and remove it.

It feels slower, but each release is safe to roll back.

Socket.IO in cluster mode

WebSocket clients are attached to a specific instance. When that instance reloads, they disconnect. Plan for it:

  • Use sticky sessions or force the WebSocket transport so polling requests do not bounce between instances.
  • Add an adapter (Redis is common) so an event emitted on one instance reaches clients on others.
  • Make the frontend reconnect automatically and refetch state after reconnecting.

The GitHub Actions job

The workflow is short. Run tests on a hosted runner, then deploy either by SSHing into the VM with a restricted deploy key or by running the job on a self-hosted runner on the server itself. Add concurrency: production-deploy so two pushes cannot run overlapping deploys, and use a GitHub environment with required reviewers if production deploys need approval. The deploy step calls one script on the server that does the seven steps above, so the same script works when run by hand in an emergency.

How I built this in practice

On Antoc, the real-estate CRM I work on at Passdn Technologies, the Next.js client and Node/Express API run on GCP VMs in asia-south1 under PM2 with Nginx in front. With 100+ real estate firms using the product during the working day, there is no quiet window where an outage goes unnoticed, so deploys have to be invisible.

That shapes the architecture. Socket.IO powers realtime updates, so clients must handle reconnects cleanly. WhatsApp nurture drips and reminders run as scheduled jobs, which stay single-instance so a reload never duplicates messages. Webhook receivers for Meta lead ads and Google Ads must keep answering during a deploy, or leads are missed. Sentry on both client and server makes a bad release visible within minutes, which is when a fast rollback matters. GitHub Actions with self-hosted runners is a pattern I use across these GCP setups.

Common mistakes

  • Using pm2 restart in the deploy script, which kills every instance at once.
  • Running a single cluster instance and expecting reload to keep the site up.
  • Building Next.js in the live folder while the server is serving it.
  • Leaving kill_timeout at the default and cutting off slow requests.
  • Shipping a field rename in one release while old instances still read the old name.
  • Having no health check, so a broken release is discovered by customers.

keep reading

FAQ

Does pm2 reload really give zero downtime?+

Yes, for cluster-mode apps with two or more instances. PM2 restarts instances one at a time and waits for each new one to be ready before stopping the next old one, so there is always a process serving traffic.

What is the difference between pm2 restart and pm2 reload?+

restart kills and restarts all instances at once, causing a brief outage. reload replaces instances one by one in cluster mode, keeping the app available throughout.

Can I do zero-downtime deploys with Socket.IO and PM2?+

Yes, but cluster mode needs sticky sessions or the WebSocket transport only, plus an adapter such as Redis so events reach clients connected to other instances. Clients will reconnect during a reload, so handle reconnection on the frontend.

How do I roll back a bad deploy on PM2?+

Keep the last few release folders on the server. Rolling back is pointing the current symlink at the previous release and running pm2 reload, which takes seconds.

Devang Gandhi

AI & full stack developer at Passdn, building CRMs, SaaS, AI agents and Flutter apps for clients in the US, UK, Australia, UAE and India. More about me

Read next

More in Mobile & DevOps