MongoDB / Advanced: distributed and recoverable operations / Lesson 18
Operations, evolution and release reliability
Plan MongoDB changes as compatible, observable and recoverable transitions. Coordinate schema, indexes, feature compatibility, drivers and write ownership.
On this page
Lesson goals
- Design expand-and-contract document migrations.
- Evaluate version, topology and recovery prerequisites before cutover.
Pin assumptions instead of teaching a moving latest
Examples use MongoDB 8.0-compatible core query and mongosh syntax, with later or earlier feature boundaries labeled where relevant. Official current documentation and driver matrices can include newer releases, so select the server, driver, runtime and deployment feature set explicitly. Community, Enterprise and Atlas differ in management and some capabilities. Follow the published upgrade path and release notes for the exact versions. Binary version and featureCompatibilityVersion are distinct controls; changing one is not proof every application, index and rollback requirement is satisfied.
Inventory the actual application namespace
A release review should record active collection options, validators and indexes, alongside application read/write shapes. These inspection methods read the synthetic namespace without changing it. Names and metadata returned by a real deployment can be sensitive and require scoped privileges. An index appearing in a list does not alone prove it serves every query or that the rollout completed under acceptable load. Compare documented requirements with actual metadata and representative plans before declaring the destination ready. No administration command or service mutation is executed here.
db.getSiblingDB('quay_lab').getCollectionInfos({ name: 'shipments' });
db.getSiblingDB('quay_lab').shipments.getIndexes();
The expected result is metadata describing the visible training collection. No actual validator or index inventory is invented.
Expand and converge before removing old shapes
For a field rename, introduce the new representation while readers remain compatible with both versions, deploy new writes and backfill existing documents in bounded batches. Validate types and value agreement before switching authority. Then remove fallback behavior and old fields only after all consumers and rollback requirements permit it. Index builds and validator changes can affect resource use and accepted writes, so stage them deliberately. Do not claim that a schemaless collection makes coordinated migrations unnecessary; mixed document versions remain an application compatibility problem.
Declare comparison semantics for a backfill
Assume legacyCode and trackingCode should agree after a synthetic backfill and that this specific migration intentionally treats missing and null as the same absence. The read-only pipeline returns mismatches after normalizing that absence policy with $ifNull. Empty output is expected only when the inspected documents satisfy this stated value rule. It does not prove type identity under every numeric conversion or validate every other field. If missing and null are distinct business states, use a type-aware comparison instead of copying this policy.
db.getSiblingDB('quay_lab').shipments.aggregate([
{ $match: { $expr: { $ne: [
{ $ifNull: ['$legacyCode', null] }, { $ifNull: ['$trackingCode', null] }
] } } },
{ $project: { _id: 1, legacyCode: 1, trackingCode: 1 } },
{ $sort: { _id: 1 } }, { $limit: 20 }
]);
Expected after a correct backfill under the declared absence policy: no mismatch documents.
Define the last safe rollback point
Before cutover, rehearse restore time, driver behavior, query plans, roles, validation, index constraints and topology readiness. Establish one authoritative writer and fence old application paths; after destination-only writes begin, endpoint reversal without reconciliation can lose or fork history. Size pools and retry budgets from measured capacity rather than universal constants. Monitor storage, replication lag, consumer history and hot-document contention together. A green health endpoint or successful binary rollout is useful evidence, but neither proves that the business data and recovery chain remain correct.
Common errors and fixes
A binary upgrade is assumed to complete compatibility work
- Why it happens
- Driver, FCV, feature and rollback prerequisites were not reviewed.
- How to fix it
- Follow the exact supported upgrade procedure and verify application contracts.
- Prevent it next time
- Record source/destination versions and deployment capabilities before release.
Old readers fail after a new document shape is written
- Why it happens
- Producer rollout preceded compatibility across consumers.
- How to fix it
- Restore compatible reading and stage the schema transition.
- Prevent it next time
- Test mixed-version instances and bounded backfill states.
Rollback loses destination-only events
- Why it happens
- Routing reversal ignored divergent write history.
- How to fix it
- Fence writers and reconcile through the rehearsed recovery path.
- Prevent it next time
- Define rollback eligibility before accepting the first new-destination write.
Check your understanding
1. Why can adding trackingCode and immediately deleting legacyCode break a rolling deployment?
Show worked solution
Older instances or integrations may still read or write legacyCode during the overlap. Expand compatibility first, establish the new write contract, backfill and verify agreement, then switch authority and retire the old shape after consumers and rollback needs are satisfied. MongoDBโs flexible documents do not synchronize application versions or make absent-field assumptions safe automatically.
2. A destination has accepted unique writes for an hour. What is missing from a rollback plan that only changes the URI back?
Show worked solution
It must explain how every destination-only event is preserved or reconciled and how concurrent old writers are prevented. Prove the data boundary, compatible consumers and tested reverse-transfer or recovery process. A URI is routing information, not evidence that two histories agree. Keep protected artifacts and a clear authority decision before changing service ownership.
Key takeaways
- Schema flexibility does not remove compatibility and recovery obligations.
- Observable convergence and controlled writers make change reviewable.