Data Migration Strategies
You'll learn how to choose the right approach—online migrations, background jobs, or dual-writing—to safely move data during schema evolution.
The Core Challenge
When your database schema evolves, you often need to transform existing data to match the new structure. The question isn't whether to migrate data, but how to do it without breaking your running application or locking users out for hours.
Three Primary Approaches
Online Migration (Synchronous)
Transform data during the migration script itself, before the deployment completes. The database is typically locked or read-only during this process.
Use when: Dataset is small, or you can afford brief downtime. Simple transformations like splitting a name field into first_name and last_name.
Background Jobs (Asynchronous)
Deploy the new schema first (using expand-contract), then process data in batches using a background worker that runs after deployment.
Use when: You have millions of rows. The migration can take hours or days. You need zero-downtime. Example: Computing and backfilling a new full_text_search column from existing text fields.
Dual-Writing
Your application code writes to both old and new columns/tables simultaneously during a transition period, allowing gradual data accumulation in the new structure.
Use when: You're fundamentally restructuring how data is stored (e.g., moving from one table to two). You need continuous uptime and can't afford even background job delays for new data.
Choosing Your Strategy
Small datasets + tolerable downtime = online migration
Large datasets + zero-downtime requirement = background jobs
Structural changes + continuous writes = dual-writing
Often you'll combine approaches: dual-write for new data while background jobs backfill historical records.
Key Takeaway: Match your data migration strategy to your dataset size, uptime requirements, and schema change complexity—online for simple/small, background jobs for large, and dual-writing for structural transformations.