Tech Interview RoomStudy guides

CI/CD

Compare rolling, blue-green and canary deployments. How do you handle database migrations safely?

Advanced · 2 min read · Lesson 5 of 6 in CI/CD

Strategies

  • Rolling update (Kubernetes default): replace pods gradually (maxSurge, maxUnavailable). No extra capacity needed; old and new versions run together during the rollout; rollback is another rollout.
  • Blue-green: deploy the new version (green) alongside the old (blue) at full capacity, test it, then switch traffic at once (load balancer or DNS). Instant rollback by switching back; costs double capacity during the release.
  • Canary: send a small share of traffic (1–5%) to the new version, compare metrics (error rate, latency, business KPIs) against the baseline, then increase in steps (25%, 50%, 100%). Limits the blast radius. Tools: Argo Rollouts, Flagger, a service mesh, or cloud load balancer weights.
  • Feature flags: deploy code dark and enable it separately for a percentage of users; decouples deployment from release and gives instant kill switches.
  • Shadow traffic: mirror production requests to the new version without returning its responses.

Database migrations: expand and contract

Because old and new application versions run at the same time in every strategy above, each schema change must work with both:

  1. Expand: add the new column or table (nullable or with a default); deploy code that writes to both old and new and reads the old.
  2. Migrate: backfill existing data in batches.
  3. Switch reads to the new structure.
  4. Contract: after all old versions are gone, stop writing the old column and drop it in a later release. Never rename or drop a column in the same release that stops using it. Run migrations with Flyway or Liquibase as a separate step (or a Kubernetes Job) before the rollout, and make them online (create indexes CONCURRENTLY in PostgreSQL, avoid long table locks).

Follow-up questions interviewers ask

Try answering each one before you open it.

How does an automated canary rollback work?

Argo Rollouts or Flagger shift traffic in steps and, at each step, run an analysis that queries Prometheus or Datadog for the canary's metrics (for example error rate under 1% and p99 under 500 ms) and often compares them with the stable version. If a check fails, traffic is routed back to the stable version and the rollout is aborted, without a human in the loop.

How do you roll back if a migration has already run?

Prefer rolling forward: with expand/contract, the previous application version still works against the expanded schema, so you only roll back the code. Destructive migrations (drops) happen only in the contract phase, after the new version has been stable. Down-migrations are rarely safe on production data, so take backups or snapshots before risky migrations.