Your migrations run in an init container and two pods ran them at the same time during a deploy, leaving the schema half-applied. How do you fix the approach?
IntermediateScenarioData and databases
Answer
Situation read → Init containers run once per pod, so with 3 replicas you get 3 concurrent migration runs. Migrations need to run exactly once per release.
What I'd do →
- Repair the schema first: check the migration tool's version table (
schema_migrations,flyway_schema_history), fix or roll back the partial change manually, and mark the correct state. - Move migrations to a single Kubernetes Job that runs before the Deployment updates: an Argo CD
PreSynchook or a Helmpre-upgradehook.
metadata:
annotations:
argocd.argoproj.io/hook: PreSync
argocd.argoproj.io/hook-delete-policy: BeforeHookCreation
spec:
backoffLimit: 0
- Keep a database lock as a second safety net (Flyway, Liquibase and most tools take an advisory lock).
- Make migrations transactional where the database allows it (PostgreSQL supports transactional DDL).
- Write migrations to be backward compatible so the old pods keep working while the Job runs.
Why → One Job per release gives exactly-once execution and a clear pass or fail gate before rollout.
Interview tip → Mention backoffLimit: 0; you don't want a failed migration retried automatically.
More on Data and databases
A release needs to rename a column that the running app reads and writes. How do you do it with zero downtime?IntermediateHow do you prove your database backups actually work?IntermediateDesign backups for an RDS PostgreSQL database with RPO of 5 minutes and RTO of 1 hour, including a region failure.AdvancedUpgrade RDS PostgreSQL from 13 to 17 with minimal downtime. How?AdvancedYou need to add an index and a column to a 500-million-row table that's written to constantly. How do you avoid locking it?AdvancedUsers see stale data right after saving because reads go to a replica with lag. How do you handle it?Intermediate