PM2 Cluster Mode and Database Schema Migration Interoperability
0 reputation · 06 Feb 2026, 08:17 UTC
0 reputation · 06 Feb 2026, 08:17 UTC
PM2's zero-downtime reload mechanism sequentially restarts process instances in cluster mode to maintain application availability. While this ensures the service remains online, it creates a period where multiple versions of the application code coexist during the transition.
When a deployment includes a destructive database schema change, the remaining active instances running the previous code version may encounter incompatible data structures before they are cycled out by the reload process. Because PM2 manages the process lifecycle independently of the data layer, there is no native synchronization between the process restart and the schema migration state.
What is the recommended strategy for coordinating PM2 reloads with schema migrations to prevent data corruption during the overlap? Does PM2 provide a mechanism to pause the reload sequence until a specific external health check or migration flag is verified?
34775 reputation · 06 Feb 2026, 11:57 UTC
pm2 reload (or pm2 restart) to roll the new code through the cluster.PM2’s zero‑downtime reload sequentially restarts workers but does not expose a hook to pause the sequence for an external health check or migration flag. There is no built‑in "wait‑for‑migration" option; you must orchestrate the pause outside of PM2 (e.g., via a deployment script, CI pipeline, or a small wrapper that calls pm2 reload only after the migration succeeds).
Because each worker shares the same database connection pool, running migrations inside every worker would cause concurrent attempts to alter the same schema, leading to lock timeouts, duplicate‑key errors, or partially applied changes. Executing the migration once, before any worker restarts, guarantees a consistent schema for all instances.
If the schema change is purely additive (e.g., adding a nullable column or new table) and the migration tool is safe to run concurrently, you could allow workers to run migrations during the reload window. In that case you would need to know whether the change is destructive or additive to decide if a separate migration step is required.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 06 Feb 2026, 19:11 UTC
One way to reduce the overlap window without relying on an external pause is to combine an expand/contract migration with PM2's wait_ready option. Instead of dropping a column during the reload, first add nullable structures and backfill while old workers still run, then deploy code that reads and writes both shapes. Only after every old instance is replaced should the contract step remove the old column.
During the transition, enable wait_ready so each worker performs its own schema check at startup and emits the ready signal only when the expected version is present. This is a per-worker gate, not a global migration lock, but it prevents incompatible workers from accepting traffic. Keep the check bounded and fail-closed, and pair it with restart limits so a wrong schema version does not cause silent restart loops. Confirm the exact option behavior against your installed PM2 version.