Three app servers run the same deploy script, and each tries to run database migrations at the same time. How do you ensure only one does?
AdvancedScenarioScenarios: deployments, user management and fleet operations
Answer
Situation read → flock only works on one host. You need a lock that all hosts share.
What I'd do →
- Use the database itself as the lock: Postgres advisory locks are held for the session.
Most migration tools (Flyway, Liquibase, Alembic with care, golang-migrate) already take such a lock.psql -v ON_ERROR_STOP=1 <<'SQL' SELECT pg_advisory_lock(424242); \! ./migrate up SELECT pg_advisory_unlock(424242); SQL - Or run migrations once, as a separate pipeline step or a Kubernetes Job, before rolling out app servers.
- Alternatives: a DynamoDB conditional write, Consul or etcd locks.
Why → Concurrent migrations can apply a change twice or interleave DDL, corrupting the schema.
Interview tip → The cleanest answer is "migrations are a separate, single step in the pipeline". Locks are a fallback.
More on Scenarios: deployments, user management and fleet operations
Write the core of a deploy script with release directories, an atomic symlink switch and rollback.IntermediateDeploy to 10 servers behind an AWS ALB one at a time, removing each from the target group during its deploy. Outline the script.AdvancedAfter a deploy, the health check fails intermittently. Your script must roll back automatically, but only if the failure persists. How?IntermediateImplement a blue/green switch on a single nginx host with a script.AdvancedOnboard 30 engineers: create accounts and install their SSH keys from their GitHub usernames, idempotently.IntermediateAn engineer leaves the company. Write the offboarding steps for Linux servers.Intermediate