Quick revision
The short answer
Design the PostgreSQL schema from today's access patterns, backfill the 4 TB, and keep the target in sync from MongoDB change streams applied idempotently in source order, never through dual writes in the application. Verify continuously with counts per range, normalised checksums and shadow reads. Cut over reads per feature behind flags, then switch writes in a short freeze, and keep a reverse sync from PostgreSQL to MongoDB so rollback does not lose writes.
What to remember
In this answer, we planned a live migration from MongoDB to PostgreSQL. We designed tables from real access patterns after profiling odd document shapes, copied with a backfill plus change streams applied idempotently in order instead of dual writes, and verified continuously with counts, normalised checksums and shadow reads. We moved reads per feature behind flags, switched writes in a short freeze, kept a reverse sync so rollback loses nothing, and sized and rehearsed the new side from measured queries.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Design the PostgreSQL schema from today's access patterns, backfill the 4 TB, and keep the target in sync from MongoDB change streams applied idempotently in source order, never through dual writes in the application. Verify continuously with counts per range, normalised checksums and shadow reads. Cut over reads per feature behind flags, then switch writes in a short freeze, and keep a reverse sync from PostgreSQL to MongoDB so rollback does not lose writes.
Why this question is asked
Moving a live, 4 TB collection to a different kind of database is one of the riskiest projects a team can run, because there are two ways to fail: lose or corrupt data, or take downtime. Interviewers at a senior level ask this to see a complete plan, from schema to rollback, and to hear why some popular shortcuts such as application dual writes are dangerous. In this answer, we will design the target schema, choose the sync mechanism, walk through the backfill and catch-up, build continuous verification, plan the stepwise cutover and the rollback, and answer the follow-up about documents that fail the new constraints.
1Designing the Relational Schema From Real Access Patterns
A document collection rarely maps one-to-one onto tables. We start from the queries the service runs today and the new ones reporting wants. Embedded arrays that are queried, joined or constrained, such as an account's addresses or members, become child tables with foreign keys. Nested data that is only ever read and written as a whole, such as user preferences, can stay as a JSONB column, which keeps flexibility without forcing a design for every field.
Before freezing the schema, we run a profiling pass over the whole collection: field presence, types per field, array lengths, and value ranges. Collections that have lived for years always contain surprises, such as a balance stored as a string in 2019 documents or an address array with 4,000 entries. We decide, with the product owner, how each odd shape is handled: converted, defaulted, or moved to a quarantine table for review.
2Copying and Keeping in Sync
The tempting approach is dual writes: change the application to write to both MongoDB and PostgreSQL. The trouble is that the two writes are not atomic. When the second one fails, or a deploy rolls back half way, the stores drift silently, and nothing tells us. So we use change data capture instead. MongoDB change streams give every insert, update and delete in commit order, with a resume token that lets us continue after a restart.
The process has two phases. First we record the current change stream position, then backfill: read the collection in _id order in parallel ranges and write rows with upserts. After the backfill, a sync worker reads the change stream from the recorded position and applies every change. Changes that happened during the backfill are applied again, which is why every write must be idempotent: an upsert keyed by the document id that only applies a change if it is newer than what the row holds, for example by comparing a version or the change's cluster time.
6 messages in order. Play them, or step through them one at a time.
3Verifying Continuously
We verify at three levels, all the time rather than once. Counts per _id range on both sides catch missing data quickly. Checksums catch wrong data: for each range we compute a hash over a normalised form of each record on both sides, the same field order, numbers with fixed precision and times in UTC, so formatting differences do not count. A mismatching range is drilled down to individual ids and re-synced.
The third level is shadow reads. For a sample of real requests, the service runs the same query against both stores, returns MongoDB's answer to the user, and logs any difference. Shadow reads test what counts or checksums cannot: that the new queries, joins and indexes give the same answer to the same question. We fix every class of difference before moving real reads.
4Cutting Over in Steps and Rolling Back
Reads move first, feature by feature, behind flags: account lookup at 1 percent, then 10, then 100, while we watch errors and latency. Because the sync lag is about a second, features that read their own writes must move together with their writes, or stay on MongoDB until the write switch.
Writes switch in one short freeze. We stop writes for a minute or two, wait until the change stream lag reaches zero and the last checksums match, flip the write flag to PostgreSQL, and resume. From that moment, PostgreSQL is the source of truth, and a reverse sync, CDC from PostgreSQL back into MongoDB, keeps the old store current for a period such as two weeks. If something serious appears, rollback is the same freeze in the other direction, and no write made after the switch is lost. Only when that period ends quietly do we stop the reverse sync and retire the collection.
- Shadow reads clean for a weekdifferences fixed by class
- Move reads per feature behind flags1, 10, 100 percent
- Freeze writes, wait for lag 0a minute or two
- Switch writes to PostgreSQLstart reverse sync to MongoDB
- Serious problem within the period?YesFreeze, switch writes back to MongoDBNoStop reverse sync, retire the collection
5 steps in order. Play them, or step through them one at a time.
5Capacity and a Rehearsal
The PostgreSQL side must be sized from measured query shapes, not guessed. We capture a day of real queries from MongoDB's profiler, translate them, and replay them at 8,000 reads and 1,500 writes per second plus peak headroom, with the indexes the shadow reads showed we need. Then we run a full rehearsal on a copy: backfill, catch-up, verification and the cutover steps, timed. The rehearsal finds the slow index build and the forgotten cron job before the real night does.
6Follow-up questions
How do you handle documents that fail the new constraints during the backfill?
We never let them block the copy or disappear. The sync worker writes a failing document to a quarantine table with the raw JSON, the constraint it failed and the time, and counts them by reason on a dashboard. Each reason gets a decision: a conversion rule, a default, or a manual fix in the source. After fixing the rule, we re-emit those ids through the pipeline. The cutover gate includes zero quarantined documents, or an explicit sign-off for the ones that are knowingly dropped.
Why apply changes in source order?
Because two updates to the same document applied in the wrong order leave the older value. Ordering per document is what matters, so we can parallelise by hashing the document id to a worker and keep each document's changes in order, while the version check in the upsert protects against replays.
What if the change stream's oplog window is shorter than the backfill?
Then the resume token expires before we start applying and changes are lost. We check the oplog window first and enlarge it if needed, or backfill in ranges with their own start tokens, so the catch-up always starts from a position the oplog still holds.
7Conclusion
In this answer, we planned a live migration from MongoDB to PostgreSQL. We designed tables from real access patterns after profiling odd document shapes, copied with a backfill plus change streams applied idempotently in order instead of dual writes, and verified continuously with counts, normalised checksums and shadow reads. We moved reads per feature behind flags, switched writes in a short freeze, kept a reverse sync so rollback loses nothing, and sized and rehearsed the new side from measured queries.
Open with
I would start with the schema: profile the whole collection to find the real field shapes, turn arrays that are queried or constrained into child tables, and keep blobs that are always read whole as JSONB.
Then, if they want more
For the copy, I would avoid application dual writes because they drift silently when one write fails. Instead I record the change stream position, backfill the 4 terabytes in parallel _id ranges with upserts, then apply the change stream from that position. Every write is an idempotent upsert that only applies newer versions, so replays during the overlap are harmless.
Verification runs all the time: counts per range, checksums of normalised records, and shadow reads where the service queries both stores and logs differences. Cutover is in steps. Reads move per feature behind flags, then writes switch in a short freeze once lag is zero and checksums match.
From then on PostgreSQL is the source of truth, and a reverse sync keeps MongoDB current for a couple of weeks, so rollback is just switching back without losing writes. Before the real thing I would size PostgreSQL from replayed production queries and do a full rehearsal.
Documents that fail constraints go to a quarantine table with the reason, never silently dropped.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Plan the migration under live traffic: how data is copied and kept in sync, how you...": https://scrivio-interview-library.netlify.app/architecture/storage-and-pipelines/d61b27f186/