Dual-Write Migrations Without Stopping Production
Practical lessons from moving live Django domains off MongoDB onto PostgreSQL using feature flags, mixed-ID resolvers, and continuous reconciliation.
Most database migration write-ups make dual-writes sound like a four-bullet checklist: add the new table, write to both stores, backfill, flip reads. In a live production system where product code keeps shipping every day, the failure modes live in the gaps between those four steps.
We moved core domains of a multi-tenant Django platform from MongoDB to PostgreSQL while keeping availability above 99.9%. Five practices made the cutover work.
1. Migrate by domain, never by whole database
Attempting to synchronize an entire monolith in one sweep turns every unrelated bug into a blocker for cutover. Isolate one bounded context at a time: a slice of models whose write paths you can enumerate and audit in a single afternoon.
If you cannot list every place a model is mutated, you are not ready to dual-write it yet. Consolidate writes behind explicit service functions first, then attach the feature flag there.
2. Treat feature flags as four explicit states
A single boolean (USE_POSTGRES) is too coarse. Every migrating domain moves through four distinct states:
legacy_only: reads and writes hit the old store.dual_write_legacy_read: writes go to both stores; reads still come from the old store while discrepancies are logged.dual_write_new_read: reads switch to PostgreSQL while writes still mirror to the old store as a rollback safety net.new_only: legacy writes stop; cleanup and deletion begin.
Keeping these four transitions explicit lets you roll back reads in seconds without losing newly written rows.
3. Plan for mixed identifiers from day one
The most underestimated friction in a MongoDB-to-PostgreSQL cutover is identifier shape. External webhooks, cached client payloads, and queued Celery tasks will keep carrying 24-character hex ObjectId strings long after your new tables start issuing integer primary keys.
Put a small, explicit resolver at the boundary of each migrating domain that accepts either identifier format and normalizes it before hitting internal services. Put a removal date on that resolver so it does not stay in the codebase forever.
4. Make reconciliation idempotent and boring
Backfill scripts will be interrupted by deploys, rate limits, or bad historical rows. Design every backfill and verification job so it can be re-run safely over the same range:
- Upsert by stable natural or legacy key.
- Log exact field-level diffs when the two stores disagree instead of printing generic counts.
- Run reconciliation after dual-writes are already enabled, then run it again right before flipping reads. If drift is not zero, do not flip.
5. Schedule severance like a feature
Dual-write migrations often stall right before step four. Once reads are on PostgreSQL and the rollback window has passed, schedule dedicated cleanup releases to remove legacy write hooks, compatibility shims, and dead collections. Half-retired stores double the operational burden for zero value.
Comments