Scrivio Interview library
ScenarioAdvanced6 min read · updated 12 November 2026

Should migrations run at application startup, in a Kubernetes init container, or as a separate pipeline step? Discuss the trade-offs for a service with 30 instances.

For a service with 30 instances, I would usually run migrations as a separate pipeline step that runs once before the deploy. Migrating at startup makes all 30 instances try to migrate; the tool's lock keeps them from clashing, but slow migrations delay startup and health checks, and an init container has the same per-pod problems plus DDL credentials in every pod. A separate job has clear logs and failure handling, lets the application run with a user that cannot change the schema, and, like every option, still requires migrations compatible with the running version.

Why this question is asked

Where migrations run is one of those decisions that seems unimportant until the first incident. A service starts with Flyway running at startup because that is the default, everything is fine with two instances and small tables, and then one day a slow migration makes 30 pods fail their health checks in a loop. Interviewers ask this scenario because the trade-offs are real and the right answer depends on scale, so they want to hear the reasoning. In this answer, we will look at each of the three options for a 30-instance service, compare them on startup, failure handling and security, and finish with the rule that applies to all of them.

1Option 1: Migrate at Application Startup

This is Spring Boot's default. With Flyway or Liquibase on the classpath, each instance runs the migration as part of startup, before JPA and before the application accepts traffic. With 30 instances rolling out, all 30 will try. The tools handle this safely: Flyway takes a database lock on PostgreSQL and Liquibase uses its DATABASECHANGELOGLOCK table, so one instance migrates while the others wait, and then each finds nothing left to do.

The problems appear when a migration is slow. Suppose it takes 4 minutes. The first pod spends 4 minutes in startup, and the others wait on the lock. If the startup probe allows only 2 minutes, Kubernetes kills the migrating pod halfway through; with Liquibase that can leave a stale lock, with any tool it can leave a non-transactional change half done. The rollout stalls, and the logs are spread across 30 pods, so finding which one actually ran the migration is itself a task.

Step throughStartup migration with a slow change
Pod 1DatabasePods 2 to 30Kubernetes
take migration lock, start 4-minute change
take migration lock? wait
startup probe failed after 2 minutes: restart
lock released or left stale
one of them retries the migration
probes failing: rollout stalls

6 messages in order. Play them, or step through them one at a time.

The lock prevents clashes, but probes and restarts interfere with slow migrations.

2Option 2: A Kubernetes Init Container

An init container runs before the application container in each pod, so we can run the migration tool there and start the application only when it succeeds. This separates migration from application startup and from the application's health probes, which helps. But it is still per pod: 30 pods start 30 init containers, which again rely on the tool's lock to serialise, and a slow migration delays every pod's start.

There is also a security cost. Every pod must carry credentials for a database user allowed to run DDL, because any of them may be the one that migrates. That means the high-privilege credentials are mounted 30 times, in every pod of every release, even though they are needed for a few seconds once per deploy.

3Option 3: A Separate Pipeline Step

The third option runs the migration once, as its own step in the deployment pipeline, before the new application version rolls out: a Kubernetes Job, a pipeline stage running the Flyway or Liquibase command line, or a one-off container from the same image with a migrate command.

YAML
# Pipeline, simplified
- name: migrate
run: |
kubectl apply -f k8s/migrate-job-v2026.11.05.yaml # runs flyway migrate once
kubectl wait --for=condition=complete job/orders-migrate-v2026.11.05 --timeout=30m
- name: deploy
needs: migrate # only if the migration succeeded
run: kubectl set image deployment/orders app=orders:2026.11.05
# application.properties for the service itself
spring.flyway.enabled=false # or keep validation only, with a read-only check

Notice the order: the deploy step depends on the migration step. If the migration fails, the deploy never starts, and the old version keeps running against a schema that is still compatible with it. The migration's logs are in one place, its timeout can be 30 minutes without affecting any health check, and failure handling is a normal pipeline failure with a clear owner.

4Fewer Privileges at Runtime

A strong benefit of the separate step is security. Only the migration job needs a database user that can create, alter and drop tables, and its credentials can live only in the pipeline. The 30 application instances can run with a user that can read and write data but not change the schema. If an application pod is compromised, or a bug issues unexpected SQL, it cannot drop a table. With migration at startup or in an init container, every running pod holds DDL rights all the time.

Where to run migrations for 30 instances
ConcernAt startupInit containerPipeline job
Runs per deployup to 30 attempts, one wins the lockup to 30 attemptsexactly once
Slow migrationdelays startup, probes may kill itdelays every podown timeout, no probes
Logs and failuresspread across podsspread across podsone job, one log
DDL credentialsin every running podin every podonly in the pipeline
Setup effortnone (default)smalla pipeline step and a job
For 30 instances the separate job wins on almost every concern except setup effort.

5The Rule That Applies Whatever We Choose

None of these options removes the need for compatible migrations. With a separate step, the migration runs while the old version is still serving traffic, and during the rollout old and new versions share the new schema. With startup migration, the first new pod migrates while 29 old pods are running. So in every case each migration must work with both the current and the previous application version, using expand and contract for anything destructive.

For a small service with two instances and quick migrations, migrating at startup is acceptable and simple. As instance counts, table sizes and security requirements grow, the separate step becomes the better default, which is why I would choose it here.

6Follow-up questions

Does the application still need Flyway on its classpath with a separate job?

Not necessarily. Many teams disable it with spring.flyway.enabled=false. Some keep it in validate-only mode, so an instance refuses to start if the schema is behind what it expects, which protects against deploying code before its migration. Validation needs only read access to the history table.

What happens if the migration succeeds but the deploy fails?

The old version keeps running against the new schema, which is fine because the migration was backward compatible. We fix the deploy and roll it out again; the migration job will find nothing pending. This is exactly the situation expand and contract is designed for.

How do you stop two pipelines from migrating at the same time?

The migration tool's lock already prevents concurrent runs on one database, and the pipeline can serialise deploys per service and environment. Naming each job after the release version also makes a second run of the same release obvious in the logs.

7Conclusion

In this answer, we compared three places to run migrations for a 30-instance service. Startup migration is simple but makes every instance try, ties slow migrations to startup and health probes, and spreads logs across pods. Init containers separate migration from the probes but keep the per-pod problems and put DDL credentials in every pod. A separate pipeline step runs once, with clear logs, its own timeout and failure handling, and lets the application run with fewer privileges, while every option still requires migrations compatible with the running version.

What to say in the interviewAbout 90 seconds aloud

Open with

For 30 instances I would run migrations as a separate pipeline step before the rollout. If they run at startup, all 30 instances try to migrate. The tools lock, Flyway with a database lock and Liquibase with its lock table, so they do not clash, but a slow migration stretches startup, the health probes may kill the pod mid-migration, Liquibase can be left with a stale lock, and the logs are scattered over 30 pods.

Then, if they want more

An init container decouples it from the application's probes, but it is still per pod, it still delays every pod, and every pod needs credentials for a user with DDL rights.

A separate job, a Kubernetes Job or a pipeline stage, runs exactly once with its own long timeout, has one log, and if it fails the deploy simply does not start, so the old version keeps running. It also lets me give the application a database user without DDL rights and keep the migration credentials only in the pipeline.

In the application I would disable Flyway or keep it validating, so a pod refuses to start if its migration is missing. Whichever option, the migration runs while the previous version is serving traffic, so every migration must be backward compatible, using expand and contract for destructive changes.

For a tiny service with quick migrations, startup migration is fine, but not at this scale.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Should migrations run at application startup, in a Kubernetes init container, or as a...": https://scrivio-interview-library.netlify.app/databases/schema-migration/67215ef8d2/