Data Migration: Strategies and Patterns for Moving Data

Plan data movement between systems with CDC, staged cutovers, validation gates, and recovery paths. Learn where schema migration is a separate concern.

published: reading time: 15 min read author: Geek Workbench updated: November 12, 2026
Quick Summary

Data movement copies records between systems; schema migration changes structures and application compatibility. CDC can reduce the write freeze needed for a data move, but the result depends on source and target behavior, lag, validation, and cutover controls. Use explicit reconciliation checks and decide in advance when rollback remains safe.

Data Migration: Strategies and Patterns for Moving Data

Moving production data can lose records, change values through type conversion, or interrupt service. CDC may shorten a write freeze, but no pattern guarantees zero downtime. The result depends on the databases, workload, tooling, and cutover plan.

This guide covers data movement, CDC patterns, validation gates, and rollback planning. A data migration transfers records between systems; a schema migration changes database structures and the compatibility of application versions. They can happen together, but have different failure modes. See Database Migration Strategies for schema rollout patterns.

Migration Strategies

Each strategy has a different rhythm and risk profile. The diagram below shows how they differ.

flowchart TD
    subgraph BigBang["Big Bang Migration"]
        A1[Stop writes to source] --> A2[Extract all data]
        A2 --> A3[Load to target]
        A3 --> A4[Validate]
        A4 --> A5[Restart on target]
    end

    subgraph Phased["Phased Migration"]
        B1[Extract batch 1] --> B2[Load batch 1]
        B2 --> B3[Validate batch 1]
        B3 --> B4[Extract batch 2]
        B4 --> B5[Load batch 2]
        B5 --> B6[Validate batch 2]
        B6 --> B7[... repeat]
    end

    subgraph CDC["Change Data Capture"]
        C1[Initial bulk load] --> C2[Enable CDC]
        C2 --> C3[Apply changes to target]
        C3 --> C4[Validate continuously]
        C4 --> C5[Cut over when aligned]
    end

    subgraph DualWrite["Dual-Write"]
        D1[Write to source + shadow] --> D2[Validate shadow = source]
        D2 --> D3[Read from target]
        D3 --> D4[Switch reads when stable]
    end

Strategy 1: Big Bang Migration

Migrate everything in a single batch window. You stop writes, migrate the data, validate, then restart. Simple in concept. The problem is the downtime window.

# Big bang migration
def big_bang_migrate(source_conn, target_conn):
    """Migrate all data at once"""
    # Extract all from source
    records = source_conn.extract_all('users')

    # Load all to target
    target_conn.load('users', records)

    # Validate
    assert source_conn.count('users') == target_conn.count('users')

Pros: Simple, quick Cons: High risk, long downtime window, no easy rollback

Strategy 2: Phased Migration

Migrate in phases, moving a subset of data at a time. This reduces risk per phase and lets you validate incrementally.

# Phased migration
def phased_migrate(source_conn, target_conn, batch_size=10000):
    """Migrate in phases by ID range"""
    min_id, max_id = source_conn.get_id_range('users')

    offset = min_id
    while offset <= max_id:
        records = source_conn.extract_range(
            'users',
            offset=offset,
            limit=batch_size
        )
        target_conn.load('users', records)
        offset += batch_size

            # Validate this batch using the same key range on each side.
        assert source_conn.count('users', f'id <= {offset}') == \
               target_conn.count('users', f'id <= {offset}')

Pros: Lower risk per phase, can validate incrementally Cons: More complex, longer total migration time

Strategy 3: Dual-Write with Shadow Table

Write to both systems during transition, validate, then switch reads.

class DualWrite:
    def __init__(self, primary_conn, shadow_conn):
        self.primary = primary_conn
        self.shadow = shadow_conn

    def write(self, table, record):
        # Write to both
        primary_id = self.primary.insert(table, record)
        shadow_id = self.shadow.insert(table, {**record, 'dual_write_id': primary_id})

        # Track correlation
        self.primary.update(table, primary_id, {'shadow_id': shadow_id})

        return primary_id

    def read(self, table, query):
        # Compare reads during validation period
        primary_result = self.primary.query(table, query)
        shadow_result = self.shadow.query(table, query)

        if primary_result != shadow_result:
            logger.warning(f"Data mismatch: {query}")

        return primary_result

Pros: Reads can be compared before routing users to the target Cons: Application changes and reconciliation are required; two writes can partially succeed

Strategy 4: Change Data Capture (CDC)

Capture ongoing changes from source and apply to target.

import json
from datetime import datetime

class CDCMigration:
    def __init__(self, source, target, checkpoint_table):
        self.source = source
        self.target = target
        self.checkpoint = checkpoint_table

    def get_last_checkpoint(self, stream):
        row = self.checkpoint.get(f'cdc_{stream}')
        if row:
            return json.loads(row['value'])
        return {'sequence': 0, 'timestamp': '1970-01-01'}

    def save_checkpoint(self, stream, sequence, timestamp):
        self.checkpoint.set(f'cdc_{stream}', json.dumps({
            'sequence': sequence,
            'timestamp': timestamp
        }))

    def capture_changes(self, stream):
        checkpoint = self.get_last_checkpoint(stream)

        changes = self.source.get_changes(
            stream,
            since_sequence=checkpoint['sequence'],
            since_timestamp=checkpoint['timestamp']
        )

        for change in changes:
            apply_change(change)
            self.save_checkpoint(stream, change.sequence, change.timestamp)

def apply_change(change):
    """Apply CDC change to target"""
    if change.operation == 'INSERT':
        target.insert(change.table, change.data)
    elif change.operation == 'UPDATE':
        target.update(change.table, change.key, change.data)
    elif change.operation == 'DELETE':
        target.delete(change.table, change.key)

CDC Implementation Patterns

AWS DMS (Database Migration Service)

The following boto3 example shows the API shape; use placeholders from a secure secret store for credentials, and check the AWS DMS API reference for the installed SDK and service configuration. Checked 2026-11-12.

# Configure DMS replication task
import boto3

dms = boto3.client('dms')

# Create endpoint for source
dms.create_endpoint(
    EndpointIdentifier='source-mysql',
    EndpointType='source',
    EngineName='mysql',
    MySQLSettings={
        'ServerName': 'source-db.example.com',
        'Port': 3306,
        'DatabaseName': 'production',
        'Username': 'dms_user',
        'Password': '<retrieve-from-secret-store>'
    }
)

# Create endpoint for target
dms.create_endpoint(
    EndpointIdentifier='target-postgres',
    EndpointType='target',
    EngineName='postgres',
    PostgresqlSettings={
        'ServerName': 'target-db.example.com',
        'Port': 5432,
        'DatabaseName': 'production',
        'Username': 'dms_user',
        'Password': '<retrieve-from-secret-store>'
    }
)

# Create replication instance
dms.create_replication_instance(
    ReplicationInstanceIdentifier='dms-instance',
    ReplicationInstanceClass='dms.t3.medium',
    AllocatedStorage=50,
    VpcSecurityGroupIds=['sg-123456'],
    AvailabilityZone='us-east-1a'
)

# Create migration task
dms.create_replication_task(
    ReplicationTaskIdentifier='migrate-users',
    SourceEndpointArn='arn:aws:dms:us-east-1:123456789:endpoint:SOURCE-ID',
    TargetEndpointArn='arn:aws:dms:us-east-1:123456789:endpoint:TARGET-ID',
    ReplicationInstanceArn='arn:aws:dms:...',
    MigrationType='full-load-and-cdc',
    TableMappings=json.dumps({
        'rules': [
            {
                'rule-type': 'selection',
                'rule-id': '1',
                'rule-name': 'migrate-users',
                'object-locator': {
                    'schema-name': 'production',
                    'table-name': 'users'
                },
                'rule-action': 'include'
            }
        ]
    })
)

Debezium for CDC

This configuration is illustrative, not a complete deployment file. Install the connector in Kafka Connect, provide secrets outside the file, and check the Debezium MySQL connector documentation for the version you deploy. Checked 2026-11-12.

# Debezium connector configuration for Kafka
debezium_config = {
    "name": "users-connector",
    "config": {
        "connector.class": "io.debezium.connector.mysql.MySqlConnector",
        "database.hostname": "mysql-source",
        "database.port": "3306",
        "database.user": "debezium",
        "database.password": "<retrieve-from-secret-store>",
        "database.server.id": "184054",
        "topic.prefix": "dbz",
        "database.include.list": "production",
        "table.include.list": "production.users,production.orders",
        "schema.history.internal.kafka.bootstrap.servers": "kafka:9092",
        "schema.history.internal.kafka.topic": "schema-changes",
        "include.schema.changes": "false",
        "transforms": "unwrap",
        "transforms.unwrap.type": "io.debezium.transforms.ExtractNewRecordState",
        "poll.interval.ms": "1000"
    }
}

# Consume CDC events
from kafka import KafkaConsumer

consumer = KafkaConsumer(
    'dbz.production.users',
    bootstrap_servers=['kafka:9092'],
    value_deserializer=lambda m: json.loads(m.decode('utf-8')),
    auto_offset_reset='earliest',
    enable_auto_commit=True
)

for message in consumer:
    change = message.value
    if change['op'] in ['c', 'u', 'd', 'r']:  # create, update, delete, read
        apply_to_target(change['payload'])

Google Cloud Datastream

The stream-creation command below follows the current gcloud CLI reference checked on 2026-11-12. Create the source and destination connection profiles first; the JSON files contain the matching configuration for the selected engines and destination. Validate the stream before starting replication. Consult the Datastream stream command reference and create-stream guide for supported options and prerequisites.

gcloud datastream streams create users-stream \
  --location=us-central1 \
  --display-name=users-stream \
  --source=mysql-source \
  --mysql-source-config=source_config.json \
  --destination=gcs-target \
  --gcs-destination-config=destination_config.json \
  --backfill-all

The names and JSON files are placeholders. The command creates a Datastream stream; it does not by itself define the application’s cutover or validation gates.

Migration Validation

Capture source counts and checksums before movement, then compare them with the target after the bulk load and again after CDC catches up. Partition large tables by stable keys or time ranges so a mismatch can be localized. Row counts alone do not detect changed values, and an aggregate checksum must use the same canonicalization and null rules on both systems. Treat the MD5 sample below as illustrative only; it is not a universal checksum recipe.

Record Count Validation

def validate_record_count(source, target, tables):
    """Validate record counts match"""
    results = {}
    for table in tables:
        source_count = source.count(table)
        target_count = target.count(table)
        results[table] = {
            'source': source_count,
            'target': target_count,
            'match': source_count == target_count
        }
    return results

# Example output
{
    'users': {'source': 1000000, 'target': 1000000, 'match': True},
    'orders': {'source': 5000000, 'target': 4999999, 'match': False}  # Problem!
}

Checksum Validation

The SQL function and concatenation used for checksums are database-specific; replace them with a tested, collision-resistant approach appropriate to the schema and engine. Avoid ambiguous concatenation by framing values and handling nulls consistently.

import hashlib

def compute_table_checksum(conn, table, columns=None):
    """Compute MD5 checksum of table data"""
    if columns is None:
        columns = conn.get_columns(table)

    query = f"""
        SELECT MD5(
            CONCAT_WAGG(
                COALESCE(CAST({columns[0]} AS CHAR), ''),
                {' , '.join([f"COALESCE(CAST({c} AS CHAR), '')" for c in columns[1:]])}
            )
        ) as checksum
    """

    result = conn.execute(query)
    return result[0]['checksum']

def validate_checksums(source, target, table):
    """Validate checksums match"""
    columns = source.get_columns(table)
    source_checksum = compute_table_checksum(source, table, columns)
    target_checksum = compute_table_checksum(target, table, columns)

    return {
        'source': source_checksum,
        'target': target_checksum,
        'match': source_checksum == target_checksum
    }

Sample Validation

A sample can reveal mismatches but cannot prove every row is correct. Combine it with full counts and checksums for critical data, plus targeted checks for truncation, nullability, decimal precision, time zones, and key uniqueness.

import random

def sample_validation(source, target, table, sample_size=1000):
    """Compare random sample of records"""
    source_records = source.sample(table, sample_size)
    target_records = target.sample(table, sample_size)

    # Build lookup for target
    target_lookup = {r['id']: r for r in target_records}

    mismatches = []
    for record in source_records:
        target_record = target_lookup.get(record['id'])
        if not target_record:
            mismatches.append({'id': record['id'], 'issue': 'missing in target'})
        elif record != target_record:
            # Find differences
            diffs = {k: {'source': record[k], 'target': target_record[k]}
                     for k in record if record[k] != target_record.get(k)}
            mismatches.append({'id': record['id'], 'issue': 'value_mismatch', 'diffs': diffs})

    return {
        'sampled': sample_size,
        'mismatches': len(mismatches),
        'match_rate': (sample_size - len(mismatches)) / sample_size,
        'samples': mismatches[:10]  # First 10 mismatches
    }

Data Type Mapping

Different databases have different data types. Map carefully.

Common Mappings

Source (MySQL) Target (PostgreSQL)
TINYINT SMALLINT
MEDIUMINT INTEGER
BIGINT BIGINT
VARCHAR(255) VARCHAR(255)
TEXT TEXT
DATETIME TIMESTAMP
TIMESTAMP TIMESTAMP
DECIMAL(10,2) NUMERIC(10,2)

Data Type Validation

def validate_types(source, target, table):
    """Validate data types are compatible"""
    source_types = source.get_column_types(table)
    target_types = target.get_column_types(table)

    mismatches = []
    for col in source_types:
        if col not in target_types:
            mismatches.append(f"Column {col} missing in target")
        elif not compatible_types(source_types[col], target_types[col]):
            mismatches.append(
                f"Type mismatch for {col}: "
                f"{source_types[col]} -> {target_types[col]}"
            )

    return {
        'source_types': source_types,
        'target_types': target_types,
        'mismatches': mismatches
    }

def compatible_types(source_type, target_type):
    """Check if types are compatible"""
    # Simplified check
    numeric = ['INT', 'BIGINT', 'SMALLINT', 'DECIMAL', 'NUMERIC', 'FLOAT', 'DOUBLE']
    string = ['VARCHAR', 'CHAR', 'TEXT']
    date = ['DATE', 'TIME', 'DATETIME', 'TIMESTAMP']

    for category in [numeric, string, date]:
        if any(t in source_type.upper() for t in category) and \
           any(t in target_type.upper() for t in category):
            return True

    return source_type.upper() == target_type.upper()

Handling Schema Changes

Schema changes are a separate workstream from copying records. If both are required, sequence compatible schema changes, backfills, validation, and application rollout explicitly. See Database Migration Strategies for expand-contract and application compatibility patterns.

Add Column Migration

def add_column_migration(source, target, table, column_def):
    """Add column to target with default value"""
    # 1. Add column with default (non-blocking)
    target.execute(f"""
        ALTER TABLE {table}
        ADD COLUMN {column_def['name']} {column_def['type']}
        DEFAULT {column_def['default']}
    """)

    # 2. Backfill existing records
    backfill_column(source, target, table, column_def)

    # 3. Add NOT NULL constraint if needed
    if column_def.get('not_null'):
        target.execute(f"""
            ALTER TABLE {table}
            ALTER COLUMN {column_def['name']} DROP DEFAULT
        """)

def backfill_column(source, target, table, column_def):
    """Backfill column with values from source"""
    batch_size = 10000
    offset = 0

    while True:
        records = source.fetch_range(
            table,
            offset=offset,
            limit=batch_size
        )

        if not records:
            break

        # Transform records
        for record in records:
            record[column_def['name']] = transform_value(
                record,
                column_def
            )

        target.insert(table, records)
        offset += batch_size

Cutover Planning

Pre-Cutover Checklist

Treat each item as a release gate with an owner and recorded result. Set the acceptable CDC lag, validation thresholds, and recovery deadline for the specific system before scheduling cutover.

## Migration Cutover Checklist

### Before the Cutover Window

- [ ] Final validation runs clean
- [ ] Rollback procedure documented and tested
- [ ] All stakeholders informed
- [ ] Monitoring dashboards ready

### Immediately Before Cutover

- [ ] Freeze application writes (if applicable)
- [ ] Run final sync
- [ ] Take application snapshots
- [ ] Verify backup integrity

### Cutover Day

- [ ] Stop writes to source
- [ ] Run final CDC sync
- [ ] Validate record counts
- [ ] Run checksum validation
- [ ] Point application to target
- [ ] Verify application works
- [ ] Monitor error rates

### Post-Cutover

- [ ] Monitor the agreed service and data-integrity signals for the observation window
- [ ] Keep the source available until the rollback deadline and recovery decision are recorded
- [ ] Do not decommission until retention, reconciliation, and recovery requirements are met

Rollback Procedure

Rollback is safe only while the source remains authoritative or target-side writes have been reconciled back to it. Before cutover, choose a deadline for the rollback decision and define the point after which recovery uses forward repair or restore instead. A connection-string switch alone does not undo writes made to the target.

def rollback_migration(source, target, application_config):
    """Rollback to source database"""
    # 1. Stop application
    stop_application()

    # 2. Point to source
    application_config.update({
        'database_host': 'source-db.example.com',
        'database_port': 3306
    })

    # 3. Restart application
    restart_application()

    # 4. Verify
    assert application_config.is_connected_to_source()

Production Failure Scenarios

Migrations fail in ways that are predictable if you know where to look. Four scenarios come up repeatedly in production.

Scenario 1: Silent Data Loss During Bulk Load

Bulk load completes with a record count match but some rows are silently dropped due to type coercion or truncation errors.

What happens: A VARCHAR(50) column in the source contains 60-character strings. The target has VARCHAR(50). The load does not error — it truncates. Your application keeps running, but some customer records now have truncated names and nobody notices until customers call support.

Mitigation:

def validate_no_truncation(source, target, table):
    """Check for truncation by comparing string lengths"""
    source_lengths = source.execute(f"""
        SELECT id, LENGTH(string_column) as len
        FROM {table} HAVING LENGTH(string_column) > 50
    """)
    if source_lengths:
        raise ValueError(f"Found {len(source_lengths)} rows that would truncate in target")

    # Also check for silent type coercion
    target_rows = target.execute(f"SELECT COUNT(*) FROM {table}")
    source_rows = source.execute(f"SELECT COUNT(*) FROM {table}")
    if target_rows[0]['count'] != source_rows[0]['count']:
        raise DataLossError(f"Row count mismatch: {source_rows} vs {target_rows}")

Scenario 2: CDC Lag Exceeding Cutover Window

CDC replication falls behind during peak traffic. The lag grows beyond the cutover window, making the migration take much longer than planned.

What happens: Your CDC migration looks healthy during low-traffic hours. During peak traffic, the source generates changes faster than the CDC pipeline can apply them. What should have been a 2-hour lag becomes 18 hours. Your cutover is delayed and the downtime window expands.

Mitigation:

def monitor_cdc_lag(cdc_client, stream_name, max_acceptable_lag_minutes=30):
    """Alert if CDC lag exceeds acceptable window"""
    lag = cdc_client.get_lag(stream_name)
    lag_minutes = lag / 60

    if lag_minutes > max_acceptable_lag_minutes:
        send_alert(
            f"CDC lag is {lag_minutes:.0f} minutes — "
            f"cutover window at risk. "
            f"Consider pausing source writes or scaling CDC."
        )
    return lag_minutes

Scenario 3: Application Cutover to Corrupt Target State

Application points to target before validation completes. The target has post-migration inconsistencies that were not caught.

What happens: Application switches to target database mid-validation. A batch of records had datetime timezone inconsistencies during migration — times shifted by hours. The application starts making decisions on bad data before the validation report finishes running.

Mitigation:

def safe_cutover(source, target, application_config, validation_queries):
    """Only cut over after all validation queries pass"""
    results = {}
    for name, query in validation_queries.items():
        source_result = source.execute(query)
        target_result = target.execute(query)

        match = compare_results(source_result, target_result)
        results[name] = {'match': match, 'source': source_result, 'target': target_result}

    failures = [k for k, v in results.items() if not v['match']]
    if failures:
        raise SafetyCheckFailed(
            f"Validation failed for: {failures}. "
            f"Cutover blocked. Review results before proceeding."
        )

    # All validations passed — safe to cut over
    application_config.update({'database_host': target_host})
    return results

Scenario 4: Rollback Fails Due to Source Write Lock

You attempt rollback after cutover, but source database rejects writes because the application has not fully released its connections.

What happens: After pointing the application to the target, you attempt to “rollback” by re-enabling writes on the source. But some application instances are still connected to the source, holding locks. Writes fail. The application experiences errors on both systems simultaneously.

Mitigation:

def rollback_with_connection_drain(source, application_config, drain_timeout_seconds=60):
    """Safely drain source connections before rollback"""
    # 1. Confirm all app instances have switched
    remaining_connections = source.get_active_connections()
    if remaining_connections > 0:
        print(f"Waiting for {remaining_connections} source connections to drain...")
        time.sleep(drain_timeout_seconds)
        remaining_connections = source.get_active_connections()

    if remaining_connections > 0:
        raise RollbackBlocked(
            f"{remaining_connections} connections still on source. "
            f"Force-killing may cause data loss. "
            f"Investigate connection pool settings."
        )

    # 2. Now safe to re-enable writes on source
    source.enable_writes()

Performance Optimization

Bulk Loading

# PostgreSQL COPY for fast bulk loads
import psycopg2
from io import StringIO

def bulk_load_postgres(target_conn, table, records):
    """Use COPY for fast bulk loading"""
    buffer = StringIO()

    # Write CSV header
    if records:
        buffer.write(','.join(records[0].keys()) + '\n')

        # Write data
        for record in records:
            values = [str(v).replace('\n', ' ') for v in record.values()]
            buffer.write(','.join(values) + '\n')

    buffer.seek(0)

    cursor = target_conn.cursor()
    cursor.copy_from(
        buffer,
        table,
        sep=',',
        null=''
    )
    target_conn.commit()

Parallel Loads

from concurrent.futures import ThreadPoolExecutor

def parallel_load(target, table, records, num_workers=4):
    """Load records in parallel"""
    batch_size = len(records) // num_workers
    batches = [
        records[i:i + batch_size]
        for i in range(0, len(records), batch_size)
    ]

    with ThreadPoolExecutor(max_workers=num_workers) as executor:
        futures = [
            executor.submit(target.load_batch, table, batch)
            for batch in batches
        ]

    # Wait for all to complete
    results = [f.result() for f in futures]
    return all(results)

Quick Recap

Key Takeaways:

  • Choose a data-movement strategy based on downtime tolerance, source/target capabilities, and recovery risk
  • CDC can reduce the write freeze when capture and apply lag stay within the cutover gate
  • Always validate record counts, checksums, and samples
  • Plan rollback procedures before starting migration
  • Test the full migration process in staging first

Strategy Selection:

  • Short write freeze: CDC with a rehearsed cutover gate; dual-write only with explicit partial-failure handling
  • Small dataset, low risk: Big bang with validation
  • Large dataset, risk-averse: Phased migration
  • Complex schema changes: Dual-write with shadow table

Critical Checks:

  • Record counts match between source and target
  • Checksums match (MD5 or similar)
  • Data types are compatible
  • No data corruption during transfer
  • Application works against target
  • Rollback procedure tested

For cloud-specific services, see AWS Data Services, GCP Data Services, and Azure Data Services. For schema rollout and application compatibility, see Database Migration Strategies.

Category

Related Posts

Serverless Data Processing: Building Elastic Pipelines

Build scalable data pipelines using serverless services. Learn how AWS Lambda, Azure Functions, and Cloud Functions integrate for cost-effective processing.

#data-engineering #serverless #lambda

AWS Data Services: Kinesis, S3, Glue, Athena, Redshift

Follow an order event from Kinesis Data Streams through S3 to Athena or Redshift, adding Glue when transformation or shared catalog metadata helps.

#data-engineering #aws #kinesis

Azure Data Pipelines with Event Hubs and Synapse

Follow live events and scheduled data through Azure processing, Data Lake Storage Gen2, and Synapse, with a clear choice between stream and batch paths.

#data-engineering #azure #data-factory