Working with Data Migrations
1. Understanding Schema Migration Strategies
| Strategy | Detail |
|---|---|
| Expand / contract | Add new → migrate → remove old |
| Big-bang | Stop-the-world; only for tiny windows |
| Online (gh-ost) | Shadow table, apply binlog, atomic swap |
| Versioned tools | Flyway, Liquibase, Alembic, Atlas |
| Forward-only | Never edit applied migration |
2. Implementing Dual-Write Pattern
| Property | Detail |
|---|---|
| Definition | Write to both old + new stores during migration |
| Risk | Partial failure → divergence |
| Mitigation | Outbox + CDC; or write to one, sync to other |
| Read | Compare or shadow-read to verify parity |
3. Implementing Incremental Migration
| Step | Detail |
|---|---|
| 1 | Migrate small slice (per tenant / region) |
| 2 | Validate, monitor SLOs |
| 3 | Expand slice; iterate |
| 4 | Cleanup old once 100% migrated |
4. Implementing Data Backfilling
Example: Idempotent batch backfill (Java)
long lastId = readCheckpoint();
while (true) {
List<Row> batch = jdbc.query(
"SELECT id, payload FROM src WHERE id > ? ORDER BY id LIMIT 1000", lastId);
if (batch.isEmpty()) break;
for (Row r : batch) {
jdbc.update("INSERT INTO dst(id, payload) VALUES(?, ?) "
+ "ON CONFLICT (id) DO UPDATE SET payload = EXCLUDED.payload",
r.id, transform(r.payload));
}
lastId = batch.get(batch.size() - 1).id;
writeCheckpoint(lastId);
rateLimiter.acquire(batch.size());
}
| Practice | Detail |
|---|---|
| Idempotent | Re-run safely (UPSERT) |
| Checkpoint | Resume after crash |
| Rate-limited | Don't saturate prod DB |
| Off-peak | Run nights/weekends |
5. Implementing Zero-Downtime Migrations
| Phase | Detail |
|---|---|
| Add new (compat) | New column nullable |
| Dual-write | Write both |
| Backfill | Fill historical rows |
| Dual-read + verify | Compare; pick new if matches |
| Switch reads | New is source of truth |
| Stop dual-write | Drop old |
6. Handling Data Transformation
| Pattern | Detail |
|---|---|
| ETL / ELT | Spark, dbt, Airflow |
| CDC stream | Debezium → Kafka → transform → sink |
| Schema evolution | Schema Registry compat checks |
| Idempotent transform | Pure function of input |
7. Implementing Migration Rollback
| Approach | Detail |
|---|---|
| Reversible step | Each migration has up + down |
| Compat phases | Old schema still works during cutover |
| Snapshot before | PITR / logical backup |
| Forward-fix | Often safer than reversing destructive ops |
8. Implementing Data Validation During Migration
| Check | Detail |
|---|---|
| Row count | SELECT COUNT(*) parity |
| Checksum / hash | Per-partition aggregate |
| Random sample diff | Spot-check N rows |
| Shadow reads | Compare prod responses live |
9. Understanding Blue-Green Data Migration
| Property | Detail |
|---|---|
| Two DBs | Old (blue) + new (green) |
| Sync | CDC keeps green current |
| Cutover | Stop writes briefly → flip → resume |
| Rollback | Flip back to blue |
10. Implementing Parallel Run Strategy
| Step | Detail |
|---|---|
| Both systems live | Old + new compute results |
| Compare | Diff outputs; investigate mismatches |
| Confidence | After N days of parity, retire old |
| Use | Critical financial / billing migrations |