Production Rollout: Data and Index Sync
Use this guide before moving a monSQLize service into production or before deploying a version that changes data shape, sync targets, Model index declarations, or collection indexes.
monSQLize provides the database runtime building blocks for this path:
- Change Stream sync for asynchronous CDC after writes reach MongoDB.
- Model index preflight through
ensureIndexes()andensureModelIndexes(). - Collection index APIs such as
listIndexes(),createIndex(),createIndexes(), anddropIndex(). - Data tasks through
msq.dataTasksandmonsqlize data-taskfor reviewed index sync, filtered data sync, field transforms, snapshots, and verification.
It does not replace MongoDB native import/export, a full backup/restore policy, or an exactly-once data pipeline. Treat dataTasks as bounded release tasks: explicit filter, dry-run, snapshot, controlled run, and verification.
When to Use This Page
Use this page when you need any of these release steps:
Production Release Sequence
- Create a database backup or restore point.
- Deploy code with
autoIndex: falsefor production services. - Run the index dry-run against the target environment.
- Resolve index conflicts before creating missing indexes.
- Run
msq.dataTasks.dryRun()/monsqlize data-task dry-run, then executerunwith production confirmation if the release needs historical data changes. - Enable Change Stream sync only for ongoing CDC after prerequisites are ready.
- Check counts, sample records, sync stats, slow queries, and error logs.
- Switch traffic only after the release gates are green.
Index Sync
For Model-declared indexes, disable automatic index creation in production and use explicit preflight.
Automatic indexing also preflights with listIndexes(), skips existing indexes, and creates only missing indexes. Production services should still prefer autoIndex: false because startup-time asynchronous index creation is not a release gate: you still need a deliberate dry-run, conflict review, duplicate-data checks for unique indexes, a maintenance window for heavy builds, and an operator-visible rollback plan.
ensureModelIndexes() and ModelInstance.ensureIndexes() compare declared indexes with existing database indexes. Dry-run mode only reports existing, missing, and conflicts. Execution mode creates missing indexes only. It does not drop, rename, or rebuild conflicting indexes.
For non-Model collections, use collection index APIs directly:
Production index checklist:
- Run dry-run first when using Model indexes.
- Review unique index changes against existing duplicate data.
- Avoid creating heavy indexes during peak traffic.
- Record the current index list before destructive changes.
- Use
dropIndex()only when rollback and query impact are understood. - Recheck slow-query logs and
explain()output after traffic moves.
Data Migration Sync
Use dataTasks for release-scoped historical data changes. A task should be bounded by filter, match records by stable business keys, snapshot affected target documents before writes, and verify the result before traffic moves.
CLI form:
Task recommendations:
- Use a stable
filterand setallowFullCollection: trueonly after an explicit review. - Use business
matchByfields for cross-endpoint sync. Source_idmatching is blocked by default. - Keep target writes idempotent; reruns should update or skip already synced documents.
- Review the snapshot path and checksum before continuing a production run.
- Use MongoDB native tooling or an application-owned job for full database copy, backup, restore, or very large batch pipelines.
Change Stream Sync After Backfill
Change Stream sync is for ongoing CDC, backup targets, projections, cache invalidation callbacks, and other asynchronous target updates. It is not a substitute for the first full historical backfill.
Operational boundaries:
- MongoDB must be a replica set for Change Streams.
- Sync is at-least-once, not exactly-once.
- Built-in MongoDB targets are idempotent for replacement/upsert style writes.
- Custom
applytargets should deduplicate by change event_id. - Monitor
getSyncStats().isRunning,errorCount,lastError, target errors, and token save errors. - If a token is lost, do not assume the historical gap is covered; run a repair or full comparison job.
Traffic Switch Checklist
Before switching traffic:
npm run release:preflighthas passed for package release readiness.- Target database has the expected collections and indexes.
- Model index dry-run reports no conflicts.
- Missing indexes have been created or intentionally deferred.
- Migration/backfill counts match the expected scope.
- Sync stats are healthy and no target is stopped.
- Slow-query logs do not show new index misses.
- Rollback path and backup restore point are documented.
After switching traffic:
- Watch sync stats and application error logs.
- Compare source and target counts for migrated collections.
- Sample critical records across old and new read paths.
- Run representative queries with
explain()when latency changes. - Keep the old rollback point until the release window closes.