/docs/vps/atomic-cutover

Zero-Downtime Cutover

The strategy

Every nextdeploy ship follows a health-check-then-flip pattern. The new release is built and started on a shadow port. Only after the health endpoint returns 200 does Caddy flip traffic. The old process keeps running until confirmed healthy โ€” if the check fails, the flip never happens.

Cutover sequence

1. Upload new bundle to VPS
2. Extract to /releases/v{n} (parallel to current /releases/v{n-1})
3. Start new Node.js process on :3002 (shadow port)
4. Poll GET /api/health on :3002 (max 30s, 1s interval)
   โ€ข returns { status: "ok", database: "ok" } โ”€โ”€โ–บ proceed
   โ€ข returns non-200 or timeout         โ”€โ”€โ–บ abort, cleanup :3002
5. Atomically update Caddy upstream:  :3000 โ”€โ”€โ–บ :3002
6. Drain :3000 (60s graceful shutdown)
7. Rename /releases/current symlink to v{n}
8. Keep v{n-1} for one-command rollback

Health probe

The GET /api/health route in this template performs a real database liveness check (SELECT 1) so the probe catches both application start failures and broken DB connections before any traffic hits the new release.

// src/app/api/health/route.ts
export async function GET() {
  await db.execute(sql`select 1`);           // real DB check
  return Response.json({ status: "ok" });   // 200 โ”€โ”€โ–บ flip traffic
}

Rollback

Because the previous release directory and process are kept for 24 hours, rolling back is a single command that completes in under 2 seconds โ€” faster than a fresh deploy.

nextdeploy rollback  # re-runs step 5 in reverse