mongosync
researched 2026-05-28T18:41:22.077Z· 22 sources · 10 concepts · skill mongosync
mongosync is MongoDB's official utility for continuous, real-time replication between two MongoDB clusters. It performs a full initial sync followed by change-stream-based CDC (no Kafka, no Debezium)
mongosync — MongoDB's Native Live Migration Tool
- mongosync is MongoDB's official utility for continuous, real-time replication between two MongoDB clusters. It performs a full initial sync followed by change-stream-based CDC (no Kafka, no Debezium) and supports cutover with sub-minute downtime, reverse sync for rollback, and filtered namespace replication. mongosync powers Atlas Live Migration and Cluster-to-Cluster Sync. [source]
1. Architecture — Initial Sync + Ongoing CDC
- mongosync runs as a standalone Go binary outside of mongod/mongos. It opens connections to two clusters and moves data in two phases: [source]
- Initial sync - mongosync reads collection data from the source cluster in parallel workers, applies inserts to the destination cluster, builds indexes, and tracks progress per-collection. [source]
- Change Event Application (CEA) - once initial sync completes, mongosync tails the source via change streams, applies operations to the destination, and stays in lockstep until you commit or pause. mongosync does not read the oplog directly - it relies on the change-streams API. [source]
- Because CDC runs over change streams, mongosync's resumability depends on the source oplog window. If un-applied operations age out of the source oplog, the change stream returns ChangeStreamHistoryLost and mongosync fails - see Section 6. [source]
Topology notes
- Replica set → replica set: one mongosync instance. [source]
- Sharded → sharded: run one mongosync per shard on the source. mongosync replicates individual shards in parallel from source to destination. [source]
- Replica set → sharded and sharded → replica set: supported with a single mongosync for the all-to-one cases, with caveats; check the version-specific topology page. [source]
- Both clusters must run MongoDB 6.0 or later and share the same major version (mongosync also supports certain version-cross migrations - confirm against the version matrix for your mongosync release). [source]
Migration host sizing
- For a production sync, MongoDB recommends a dedicated migration host with at least 8 CPUs and 24 GB of RAM. The host needs network reachability to both clusters and enough disk for logs and the local progress state. [source]
CLI vs config file
Core connection options
- > Cluster role (source vs destination) is not set on the binary - it's decided by the call to the /api/v1/start endpoint. The same mongosync process can be reversed; see Section 8. [source]
REST API surface
- mongosync exposes an HTTP API on 127.0.0.1:27182 (default port). Key endpoints: [source]
`start` body — the essential fields
Basic filters
- includeNamespaces and excludeNamespaces are mutually exclusive arrays of filter objects. Each object has a database and optionally a collections array. With no filter mongosync performs a full cluster sync (every non-system database/collection). [source]
- This includes only sales.EMEA, sales.APAC, and every collection under marketing. [source]
Regex filters (mongosync 1.6+)
- Filter values can be regular expressions, so you can match many databases/collections at once: [source]
Namespace remapping
- namespaceRemap lets you rewrite the destination namespace, useful for tenant consolidation or rename-during-migration. The destination database and/or collection name can differ from the source. [source]
Filter immutability
- You cannot change a filter on a running sync. Stop mongosync, prepare the destination (drop any partially-synced collections), and start a new sync with the updated filter. There is no in-place filter edit. [source]
Items mongosync never replicates
- local, config, admin database internals (system collections). [source]
- User credentials and roles - you must recreate roles/users on the destination. [source]
- Any collection mongosync flags as "unsupported" for the version (e.g., certain time-series edge cases - check the FAQ for your release). [source]
Where state lives
- mongosync writes progress and resume tokens to the destination cluster (in a metadata collection mongosync owns). That is why resume after a process crash works even on a brand-new mongosync host: the durable checkpoint lives next to the data. [source]
Resume rules
- resume only works if mongosync is in PAUSED state (or the process was killed in RUNNING and you bring it back). [source]
- After resume, mongosync may take at least 2 minutes before re-entering RUNNING - it re-validates the resume token and reopens the change stream. [source]
- The resume token is a change-stream _data value. If the oplog has rolled past it, the change stream errors with ChangeStreamHistoryLost and the sync cannot resume - you must drop the destination and start over. [source]
When you cannot resume
5. Verification
- mongosync ships four verification methods. Pick based on cluster shape and downtime budget. [source]
5.1 Embedded verifier (default, replica sets)
5.2 Hash comparison (dbHash MD5)
5.3 Document counts
5.4 Migration Verifier (`mongodb-labs/migration-verifier`)
Oplog window exhaustion
- Symptom: mongosync exits with ChangeStreamHistoryLost or similar - the source oplog rolled past mongosync's resume point. [source]
- Initial sync taking too long on a high-write source. [source]
- Long pause while CDC is suspended. [source]
- Source oplog sized too small (default 5% of disk can be tiny on busy servers). [source]
- Pre-sync: increase oplogSizeMB (or replSetResizeOplog with minRetentionHours > expected sync duration). [source]
- During sync: scale up the mongosync host (more CPU/RAM) so CDC keeps up. [source]
- Post-failure: drop destination collections and restart - there is no recovery once the oplog window is gone. [source]
- Rule of thumb: set minRetentionHours to 2–3× the expected initial-sync duration plus any pause window. [source]
Network partitions
- mongosync retries transient network errors with exponential backoff. Sustained partitions cause mongosync to surface errors via /progress and eventually stop. Restart picks up from the last checkpoint iff the oplog window is still intact. [source]
Schema drift
- mongosync replicates DDL via change events - collection creates, drops, index builds. It does not repair manual drift on the destination. If someone writes directly to the destination while a sync runs, you've corrupted the migration; restart from scratch. enableUserWriteBlocking on the destination is the guardrail. [source]
Stalled progress
Cannot reverse / reverse refused
7. mongosync vs Atlas Live Migration vs Cluster-to-Cluster Sync
- All three use the same underlying mongosync engine. The difference is the operating envelope: [source]
Decision rules
- Need filtered sync, namespace remap, private link, or cross-version → standalone mongosync. [source]
- Smaller cluster going into Atlas with public network → Atlas Live Migration (one-click). [source]
- Long-lived ongoing replication between two clusters (DR, multi-region, active-passive) → Cluster-to-Cluster Sync (same binary, configured to never reach COMMITTED). [source]
Cutover process
- mongosync is RUNNING with lagTimeSeconds low (sub-second on healthy networks). [source]
- Quiesce application writes on the source. [source]
- Call POST /api/v1/commit. [source]
- mongosync moves to COMMITTING - drains remaining change events. [source]
- mongosync reaches COMMITTED - final state. The destination is now authoritative. [source]
- Flip application connection strings to the destination cluster. [source]
- Healthy production cutovers complete in under 60 seconds because mongosync already had CDC caught up before commit. [source]
Reverse sync — rollback
- If the destination misbehaves post-cutover, you can flip the direction: [source]
- reversible:true set at original start. [source]
- enableUserWriteBlocking:true set at original start. [source]
- Same major version on both clusters. [source]
- Same topology (replica set ↔ replica set, or sharded ↔ sharded). [source]
- The destination cluster's oplog has not rolled past the canWrite=true moment. [source]
- After reverse, the original destination becomes source, and writes on the new source flow back to the original source. Filtered Sync is not supported during reverse - reverse syncs the full cluster. [source]
State machine summary
- State transitions are mostly API-driven; COMMITTING→COMMITTED and REVERSING→RUNNING are automatic. [source]
loadLevel
One mongosync per shard
- For sharded sources, run one mongosync instance per shard for true parallel copy. Coordinate them so they share the destination cluster's URI. [source]
Index builds
Balancer
- Disable the balancer on the sharded destination (sh.stopBalancer() / balancerStop) before starting the migration. The balancer fighting with mongosync inflates the migration window and can cause chunk-move-vs-CDC races. [source]
Other tuning knobs
10. Security — TLS & x.509
- mongosync inherits MongoDB's standard auth. For production: [source]
TLS
- Use TLS for every connection - both cluster0 and cluster1 URIs should be mongodb+srv with TLS implied, or include tls=true. [source]
- The mongosync host needs to trust the CA bundle for both clusters. Use the CA file via the URI parameter tlsCAFile=. [source]
- mongod logs a warning if the presented certificate expires within 30 days - monitor cert expiry on the migration host as part of pre-flight checks. [source]
x.509 auth (recommended for self-managed source ↔ Atlas dest)
Required roles
- The mongosync user on each cluster needs broad read/write across the synced namespaces plus: [source]
- On source: read on every synced namespace, plus permission to open change streams. [source]
- On destination: readWrite on every synced namespace, plus index creation, plus the metadata collections mongosync writes to. [source]
- Atlas exposes a built-in role specifically for mongosync (Atlas Admin is sufficient but overpowered - use the documented minimal role set). [source]
Network security
Quick-reference checklist for a new mongosync migration
- Both clusters on MongoDB 6.0+, same major version. [source]
- Migration host with >= 8 CPU, >= 24 GB RAM, network access to both. [source]
- Source oplog window >= 2-3x expected initial sync duration. [source]
- Destination cluster sized to absorb both sync load and post-cutover production load. [source]
- TLS + x.509 (or strong password auth) wired up on both URIs. [source]
- Balancer disabled on sharded destination. [source]
- Users/roles recreated on destination (mongosync does NOT migrate them). [source]
- Config file (not CLI password) - secrets out of ps. [source]
- reversible: true and enableUserWriteBlocking: true if rollback is required. [source]
- Filters reviewed - includeNamespaces/excludeNamespaces (cannot be changed later). [source]
- loadLevel chosen for destination capacity. [source]
- Monitor /api/v1/progress for state + lagTimeSeconds. [source]
- Watch source oplog window vs sync ETA. [source]
- Run migration-verifier in parallel for sharded or high-mutation workloads. [source]
- Quiesce source writes. [source]
- POST /commit, wait for COMMITTED. [source]
- Verify counts/hashes on destination. [source]
- Flip app connection strings. [source]
- Keep mongosync available for reverse until you're confident in the destination. [source]
Sources
- Mongosync - MongoDB Docs (current) [source]
- mongosync Quickstart [source]
- mongosync Configuration Reference [source]
- mongosync Binary Reference [source]
- start API endpoint [source]
- resume API endpoint [source]
- mongosync States [source]
- Filtered Sync [source]
- Regular Expressions in Filters [source]
- Verify Data Transfer [source]
- Verify with Hash Comparison [source]
- Verify with Migration Verifier [source]
- migration-verifier (mongodb-labs) [source]
- oplog Sizing [source]
- Reverse Sync Direction [source]
- Finalize Cutover Process [source]
- Sync Sharded Clusters [source]
- Atlas Live Migration vs Mongosync [source]
- Mongosync product page [source]
- Mongosync FAQ [source]
- X.509 Client Authentication on Self-Managed MongoDB [source]
Children
- initial-sync-and-cdc (frontier)
- namespace-filtering (frontier)
- resume-and-checkpoint (frontier)
- verification-modes (frontier)
- reverse-sync-cutover (frontier)
- oplog-window-sizing (frontier)
- atlas-live-migration-comparison (frontier)
- mongosync-state-machine (frontier)
- loadlevel-tuning (frontier)
- tls-x509-auth (frontier)
Frontier under this node: atlas-live-migration-comparison, initial-sync-and-cdc, loadlevel-tuning, mongosync-state-machine, namespace-filtering, oplog-window-sizing, resume-and-checkpoint, reverse-sync-cutover, tls-x509-auth, verification-modes