Most deploys that take a site down for a few minutes aren't code problems. They're schema changes: a column renamed while the old version of the app is still running, or an ALTER TABLE that locks a busy table for longer than anyone expected.
The fix is a pattern called expand and contract (also known as parallel change). Instead of changing the schema in one step, you split the change into small steps. Each step works with both the old and the new version of your code. Here's how it works, with PostgreSQL examples.
Why one-step migrations break
During a rolling deploy, old and new app instances run side by side for a few minutes. Say you ship this as a single migration:
ALTER TABLE users RENAME COLUMN name TO full_name ;
Enter fullscreen mode
Exit fullscreen mode
The new code works, but every old instance still querying name starts throwing errors until the deploy finishes.…