The shadow table strategy is the foundation that nearly every online migration tool is built on. The name comes from the idea that you're building a "shadow" or "ghost" copy of your table in the background — invisible to the application — and then swapping it in when it's ready.
The General Algorithm
Almost all online migration tools (like pt-online-schema-change and gh-ost) follow the same high-level pattern:
- Create Ghost: Create an empty copy of the table (the "shadow" or "ghost" table) with the new schema — for example, with the extra
preferred_languagecolumn already present. - Backfill: Slowly copy existing data from the original table to the ghost table in small chunks (e.g., 1,000 rows at a time). Chunking prevents the migration from saturating disk I/O and starving the application of database resources.
- Sync: While copying, capture any new writes (INSERTs, UPDATEs, DELETEs) happening on the original table and apply them to the ghost table to keep it current. This is the hardest part — and the part where the two main tools differ fundamentally.
- Cutover: Once the ghost table contains all the data and is fully in sync, perform an atomic
RENAMEto swap the tables: users→users_old_backup_users_gho→users- Cleanup: Drop the
users_old_backuptable.
The magic is in Step 3 (Sync). How do you capture new writes without locking the table? There are two main approaches, and choosing between them has significant operational consequences.