Menu

Post image 1
Post image 2
1 / 2
0

Zero-Downtime Schema Changes (You Can Do This)

DEV Community·Tosh·28 days ago
#xReyHfww
Reading 0:00
15s threshold

You need to add a column to a table with 100 million rows. Old approach: Lock the table, add column, wait 30 minutes, production is down. New approach: Add column, backfill data, no downtime. The Pattern Step 1: Add column (new, nullable) ALTER TABLE users ADD COLUMN new_field VARCHAR ( 255 ) NULL ; Enter fullscreen mode Exit fullscreen mode Takes 2 seconds. Table briefly locked. No big deal. Step 2: Backfill in batches UPDATE users SET new_field = computed_value WHERE id >= 0 AND id < 10000 ; UPDATE users SET new_field = computed_value WHERE id >= 10000 AND id < 20000 ; -- ... repeat in batches Enter fullscreen mode Exit fullscreen mode Takes time but doesn't lock the whole table. Step 3: Add constraint (if needed) ALTER TABLE users MODIFY new_field VARCHAR ( 255 ) NOT NULL ; Enter fullscreen mode Exit fullscreen mode After backfill is done. Step 4: Update application Start writing to new_field. Step 5: Remove old code Once confident, stop reading old_field.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More