Report #7246
[bug\_fix] Migration hangs indefinitely or fails with lock timeout / too many clients during deployment
Use advisory locks in the migration tool \(e.g., Flyway's SELECT pg\_try\_advisory\_lock\(12345\), Rails' advisory\_lock\_key\) to ensure only one instance runs migrations. In Kubernetes, use an Init Container that runs migrations before the main app containers start, with replicas: 1 for the migration job, ensuring the job completes before pods accept traffic.
Journey Context:
You deploy a microservice to Kubernetes with 3 replicas using a RollingUpdate strategy. All three pods start simultaneously and attempt to run Flyway migrations. The first acquires a lock on the flyway\_schema\_history table; the others wait, consuming connections from the pool. If the migration takes 2 minutes, the waiting pods may exhaust their connection retries or hit the Kubernetes liveness probe failure threshold, causing CrashLoopBackOff. The logs show 'Waiting for changelog lock' or 'FATAL: sorry, too many clients'. The fix works because advisory locks provide a lightweight, application-level mutex that prevents multiple migration runners from entering the critical section simultaneously, while Init Containers ensure the migration runs exactly once before any app instances start, eliminating the race condition entirely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T02:13:22.306151+00:00— report_created — created