MongoDB Migration Patterns

MongoDB Migration Patterns

Migration Tool Selection

Tool From → To Downtime Best for
Atlas Live Migration Self-managed → Atlas Minimal (minutes) MongoDB 4.4+ → Atlas
mongosync MongoDB → MongoDB Near-zero Cluster-to-cluster sync
Relational Migrator RDBMS → MongoDB Depends on strategy Oracle/MySQL/PostgreSQL/SQL Server/DB2
mongodump/mongorestore MongoDB → MongoDB Full (offline) Small datasets, cold migrations
mongoexport/mongoimport Any → MongoDB Full (offline) JSON/CSV interchange

Atlas Live Migration (Self-Managed → Atlas)

Process

  1. Pre-migration check: Atlas validates source cluster compatibility
  2. Initial sync: Atlas pulls all documents from source
  3. Oplog tailing: Atlas continuously applies changes from source oplog during sync
  4. Cutover: When lag < 30 seconds, initiate cutover — stop writes, confirm lag = 0, switch connection strings

Requirements

Atlas CLI Migration

atlas liveMigrations create --clusterName targetCluster --projectId <id> \
  --migrationHosts source.example.com:27017 --ssl --caFile /path/to/ca.pem

atlas liveMigrations cutover --clusterName targetCluster --projectId <id>

mongosync — Cluster-to-Cluster Sync

mongosync is MongoDB’s cluster-to-cluster synchronization tool.

# Start mongosync
./mongosync \
  --cluster0 "mongodb+srv://user:[email protected]" \
  --cluster1 "mongodb+srv://user:[email protected]"

# Initialize sync
curl -X POST http://localhost:27182/api/v1/start \
  -H "Content-Type: application/json" \
  -d '{"source": "cluster0", "destination": "cluster1"}'

# Monitor progress
curl http://localhost:27182/api/v1/progress

# Commit (finalize)
curl -X POST http://localhost:27182/api/v1/commit

mongosync Limitations

Cutover Procedure

  1. Monitor lagTimeSeconds until < 10 seconds
  2. Stop writes to source cluster
  3. Wait until lagTimeSeconds: 0 and state: COMMITTED
  4. Call commit API
  5. Update application connection strings to destination

Relational Migrator

MongoDB Relational Migrator is a free GUI tool for migrating RDBMS schemas and data to MongoDB.

Supported Source Databases

Oracle 11g+, MySQL 5.7+, PostgreSQL 11+, SQL Server 2016+, DB2 11.5+, Sybase/ASE 16.0+, YugabyteDB

Migration Strategy Options

Pre-Migration Analysis

Key checks:

Snapshot vs CDC Migration

Zero-Downtime Migration Runbook

Phase 1: Pre-Migration (Days Before)

  1. Source assessment: document count, storage size, index count, write rate
  2. Schema analysis: run Relational Migrator pre-migration advisor
  3. Network validation: confirm Atlas can reach source
  4. Pilot migration: migrate subset of non-critical collections

Phase 2: Initial Sync

  1. Start mongosync or Atlas Live Migration
  2. Monitor initial sync progress
  3. Monitor source cluster performance (migration reads impact production)
  4. Validate document counts periodically

Phase 3: Cutover

  1. Announce maintenance window
  2. Drain writes (stop scheduled jobs, maintenance tasks)
  3. Confirm sync lag < 10 seconds
  4. Stop application writes (brief read-only or maintenance page)
  5. Confirm sync lag = 0
  6. Update connection strings in application config/secrets
  7. Restart applications pointing to Atlas
  8. Resume writes
  9. Verify: check application health, error rates, latency

Phase 4: Validation

// Document count comparison
db.orders.countDocuments()  // Compare source vs destination

// Sample document validation
db.orders.aggregate([
  { $sample: { size: 100 } },
  { $project: { _id: 1, orderId: 1, amount: 1, status: 1 } }
])

Common Migration Anti-Patterns

References