RE:NODE
ჰოსტინგი

ბაზები6 წუთის საკითხავი

Schema migrations without downtime

Adding is safe, removing is safe later, and renaming is what breaks things. The expand-and-contract pattern, in the order it actually works.

ეს სტატია ჯერ ინგლისურადაა. ვთარგმნით.


During any deploy there is a window where old and new code are both running against one database. Every safe migration follows from that fact, and every unsafe one ignores it.

Expand, migrate, contract

  1. Expand: add the new column or table. Old code ignores it; new code can use it.
  2. Deploy the code that writes to both the old and the new shape.
  3. Backfill the existing rows, in batches, while everything keeps running.
  4. Deploy the code that reads only the new shape.
  5. Contract: drop the old column, in a later release, once nothing refers to it.

The operations to be careful with

  • Renaming a column: never in one step. It is add, backfill, switch, drop.
  • Adding a NOT NULL column without a default, which rewrites the table.
  • Creating an index on a large table without the concurrent option, which locks writes.
Five boring deploys are cheaper than one interesting one.