Docs cipi.yml Web panel API MCP Guides Compare GitHub Telegram channel
  1. Home
  2. Guides
  3. Zero-downtime Laravel deploys with safe migrations
Laravel

Zero-downtime Laravel deploys with safe migrations

How atomic releases work, how to write migrations that never break the live release, and how to take a database snapshot and roll back automatically when something goes wrong.

By Updated 2 min read

Zero downtime is two problems: switching code without a gap, and changing the database without breaking the code that's still live. Cipi solves the first. This guide shows how to handle the second.

Key ideas, in plain words#

TermMeaning
Atomic releaseThe new code goes live in one instant switch
Expand / contractFirst add (columns, tables) so old and new code both work; remove later
SnapshotA database dump taken right before migrations
Auto-rollbackCipi restores the previous release if the health check fails

How a release goes live#

Step 1 of 5Clone

The code is cloned into a brand new folder under releases/. Visitors still use the old one.

The old release keeps serving until the swap. The previous five releases stay on disk for an instant sudo cipi deploy shop --rollback.

The catch: migrations run before the swap#

For a few seconds the old code runs against the new schema. Migrations must be backward compatible with the release that's still live.

ChangeSafe way
Add a columnNullable or with a default. Safe in one deploy
Rename a columnAdd the new column, write to both, backfill, switch reads, drop the old one in a later deploy
Drop a columnFirst deploy code that no longer uses it, then drop it in the next deploy
Add an index on a big tableUse the engine's online method (ALGORITHM=INPLACE on MariaDB, CREATE INDEX CONCURRENTLY on PostgreSQL)
Make a column NOT NULLBackfill first, constrain later

This "expand, then contract" rhythm is what lets you deploy many times a day.

Take a snapshot first#

bash
sudo cipi deploy shop --snapshot             # dump first, continue if the dump fails
sudo cipi deploy shop --snapshot-required    # abort the deploy if the dump fails
sudo cipi app edit shop --predeploy-snapshot # every deploy, whatever triggered it

Dumps land in /var/log/cipi/backups/. To undo a bad migration:

bash
sudo cipi db restore shop /var/log/cipi/backups/shop_20261123_143012.sql.gz
sudo cipi deploy shop --rollback

Roll back automatically#

yaml
# cipi.yml
health:
  url: "https://shop.example.com/up"
  expect: 200
  rollback_on_unhealthy: true
deploy:
  predeploy_snapshot: true

After every deploy Cipi calls the health URL. If the new release doesn't answer as expected, the previous release goes back live and you get notified.

Know who deployed what#

Every deploy, from any trigger, is recorded in a hash-chained audit trail:

bash
sudo cipi deploy shop --audit
sudo cipi deploy shop --history

Troubleshooting#

ProblemFix
Old release errors during deployA migration removed something it uses: split into expand and contract
Deploy stuck on a migrationA table lock: check running queries, use online DDL
Rolled back code, data still newExpected: restore the snapshot if needed
No snapshot was taken--snapshot continues if the dump fails; use --snapshot-required

Questions

Does rollback undo migrations?

No. --rollback restores code. To undo data changes, restore the pre-deploy snapshot with cipi db restore.

What does "zero downtime" mean exactly?

The new release is built and migrated next to the live one, then a single symlink swap makes it live. Requests never hit a half-built release.

Can I rename a column safely?

In two deploys: add the new column and write to both, backfill, switch reads, then drop the old one in a later deploy.

What about long migrations on big tables?

Adding an index or a column with a default can lock a big table. On MariaDB use ALGORITHM=INSTANT/INPLACE where possible; on PostgreSQL create indexes CONCURRENTLY (outside a transaction).

Should I use maintenance mode?

Rarely. With expand-then-contract migrations the old release keeps working during the deploy, so there's nothing to hide.

What if a step fails?

Read the troubleshooting table above first. Then check the matching docs page, open a GitHub issue, or ask in the community. Most commands print the fix when they refuse something.

Do I need the web panel for this?

No. Every step works from the CLI. The optional web panel shows the same things in a browser.

Try it on your own server

Free and open source. One command on a fresh Ubuntu 24.04 or 26.04 VPS.

wget -O - https://cipi.sh/setup.sh | bash
Read the quickstart