Sitemap
JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Member-only story

Rails Zero-Downtime Database Migrations

14 min read5 days ago

--

Press enter or click to view image in full size
Rails Zero-Downtime Database Migrations

A production checklist for safe Rails migrations: compatibility, backfills, PostgreSQL locks, rollback limits, and deploy order.

Most Rails migrations that cause outages are not wrong. They are correct and badly timed.

rename_column works. change_column_null works. Both can still take your app down, because for a few seconds two versions of your code run against one database, and only one of them knows what you changed.

This is the order I use for Rails zero downtime migrations on PostgreSQL, tested on Rails 8.1 and PostgreSQL 15.

They fail in three ways, and each one needs a different fix.

Version skew. During a rolling deploy, old and new processes share a database. A migration that drops or renames a column breaks whichever side still expects the old shape.

Lock queues. A schema change waits for a lock, and everything behind it waits too. The migration itself may be instant. The queue it creates is not.

Long writes. A backfill, an index build, or a constraint check holds a transaction open or scans the table while traffic keeps arriving.

--

--

Ruby on Rails Consultant | 15 yrs | Free Rails audit | Built SaaS App called https://callscoutpro.com

See more from me

JavaScript in Plain English
JavaScript in Plain English

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.