Managing Database Schema Migrations in Production with Flyway and Liquibase
Fast recall
Quick revision
Core answer
Whether you treat schema migrations as version-controlled, automated, repeatable artifacts rather than ad-hoc SQL scripts run manually.
Running migrations as part of application startup in production without safeguards, or using destructive changes (DROP COLUMN) before deploying backward-compatible code.
Mention the expand-contract pattern, running Flyway/Liquibase in a separate pre-deployment job rather than application startup, and having a tested rollback strategy for each migration.
Interview-ready guide
Interview answer
Flyway and Liquibase version-control database schema changes the same way Git versions code — each migration is a numbered, immutable script that runs once and is recorded in a migrations table.
How Flyway works
- Flyway scans
classpath:db/migrationfor files namedV1__description.sql,V2__description.sql; it runs each unexecuted migration in order and records the checksum in theflyway_schema_historytable - On application startup (with Spring Boot auto-configuration), Flyway runs automatically before the application accepts traffic — which means the database is always at the schema version the application expects
The production migration risk
- The real danger is the window during a rolling deployment when old and new application versions are running simultaneously with the new schema — if V2 adds a NOT NULL column, the old version's INSERT statements fail because they don't include the new column
- Solution: the expand-contract pattern: V2 adds the column as nullable (old version's INSERTs work, new version writes null or the value); V3 (next release) fills in nulls and adds the NOT NULL constraint
Running migrations separately from the application
- For large tables, a migration that adds an index or rewrites rows can take minutes — running it during application startup blocks Spring Boot's initialization for those minutes, causing health check failures and deployment rollbacks
- Run migrations as a pre-deployment Kubernetes Job (Flyway's
flyway migratecommand) before deploying the new application version; the application starts with the schema already updated
Flyway vs Liquibase
- Flyway uses SQL or Java migration scripts — simple, explicit, and familiar to anyone who knows SQL; immutable after execution
- Liquibase uses XML/YAML/JSON changelogs — more database-agnostic (can generate SQL for different dialects), supports rollback scripts, and provides more granular change tracking; more configuration than Flyway
Managing Database Schema Migrations in Production with Flyway and Liquibase
Direct Answer
Use Flyway or Liquibase to version-control schema changes; run migrations as a pre-deployment step separate from app startup; use expand-contract for backward compatibility during rolling deployments.
Common Mistakes & Pitfalls
Running migrations as part of application startup in production without safeguards, or using destructive changes (DROP COLUMN) before deploying backward-compatible code.
Check your understanding
Answer these without looking back at the article.
- How do you roll back a Flyway migration?
- What is the difference between Flyway and Liquibase?
- How do you handle long-running schema changes (like adding an index to a large table) without locking?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.