Zero-Downtime Deploys and Migrations

Ship code and schema changes to a live system without outages using rolling, blue-green and expand-contract techniques.

Advanced · about 4 hours · difficulty 4/5

Learn first: Schema Migrations, Continuous Integration and Delivery, Vertical and Horizontal Scaling

At scale, "we will take the site down for maintenance on Sunday" stops being acceptable. Every deploy and schema change must happen while traffic flows.

Deployment strategies

  • Rolling: replace instances a few at a time. Old and new versions run side by side briefly, so they must be compatible.
  • Blue-green: run the new version as a full parallel environment and switch traffic at once, with instant rollback by switching back.
  • Canary: send a small percentage of traffic to the new version, watch error and latency metrics, then expand or roll back automatically.

All rely on health checks, graceful shutdown (drain in-flight requests on SIGTERM) and good metrics.

Expand and contract for schemas

Because two app versions overlap, never make a breaking schema change in one step. Renaming a column, for example:

  1. Expand: add the new column; deploy code that writes to both.
  2. Migrate: backfill existing rows in batches.
  3. Switch reads to the new column.
  4. Contract: stop writing the old column, then drop it in a later release.

Safe migration habits

  • Know which DDL takes heavy locks. Adding a column with a volatile default, adding a non-concurrent index or changing a type can block writes on large tables. Use CREATE INDEX CONCURRENTLY, add constraints as NOT VALID then validate, and set a lock_timeout.
  • Backfill in small batches with pauses, never one giant UPDATE.
  • Make every migration reversible or have a documented forward fix.

Feature flags

Deploying code is not the same as releasing a feature. Ship code dark behind a feature flag, enable it for staff, then a percentage of users. Remove flags once rolled out.

You are ready to move on when you can rename a column on a table under constant load with no errors, using expand and contract across multiple deploys.

Key concepts

  • Rolling deployments
  • Blue-green deployments
  • Canary releases
  • Expand and contract migrations
  • Lock-safe DDL
  • Batched backfills
  • Feature flags

Mini project

While a load test runs against your API, rename a column using expand and contract over three deploys and show zero failed requests in your metrics.

Check yourself

Why can you not drop an old column in the same deploy that stops using it during a rolling deployment?

Free resources