Design change data capture from a sharded MongoDB 7.0 cluster into an analytics warehouse. The orders collection holds 3 TB; each order embeds an items array, and the app changes orders with partial updates ($set of the status, $push of an item) and hard deletes. The warehouse needs current-state orders and order_items tables with lag under 5 minutes, and deletes must reach it. An outage of the pipeline of up to 24 hours must not force a full reload, and the initial load must not leave a gap before streaming starts. Explain where the change stream is opened and what ordering it gives, how the pipeline gets the full row when an update event carries only the changed fields, how the initial load hands over to the stream, how deletes and array changes are applied to the two tables, and what you size and monitor on the MongoDB side.
Open one change stream on mongos (or Debezium's MongoDB connector); record its position, snapshot, replay with _id upserts. Updates carry only deltas: use post-images (6.0+) or full documents; deletes bring only _id. Rebuild an order's items; keep oplog over 24 h.
- Opening a change stream per shard and losing the cluster-wide ordering that
mongosgives - Assuming an update event carries the whole document, or that
updateLookupreturns the document as it was at the time of the update - Starting the stream after the snapshot ends, which drops the changes made while the snapshot ran
- What does Debezium's MongoDB connector option
capture.modechange in the events the warehouse receives? - How would you keep a history table of order status changes as well as the current state?
Where to open the stream. On a sharded cluster the stream is opened through mongos: it opens a stream on every shard, then sorts and merges the events, so changes to one document arrive in order. Debezium's MongoDB connector does the same: it delegates capture to change streams. The topic key is the document _id, so per-key order holds.
The full row. An update event carries updateDescription — the changed and removed fields, not the document.
fullDocument: "updateLookup"reads the document at processing time: it may be newer than the event, or empty if the document has since been deleted.- Pre/post-images (MongoDB 6.0+,
changeStreamPreAndPostImageson the collection,fullDocument: "required"or"whenAvailable") give the document exactly as of this change; they rely on pre-images inconfig.system.preimages, which grows with the change volume. - In Debezium the default
capture.mode=change_streams_update_fullputs the full document inafter; the*_with_pre_imagemodes addbefore.
Snapshot-to-stream handover. Open the stream and record its token → snapshot the collection (in parallel by _id ranges) → replay the stream from the recorded token. Events that are also in the snapshot repeat harmlessly: applying them is an upsert by _id with the latest version.
Deletes and the array. A delete event holds only documentKey (the _id and shard key): remove the row from orders and all the order's rows from order_items. On any change to an order, rebuild its rows in order_items whole (delete and insert by order_id) — that way $push, $pull and reordering are handled the same way.
MongoDB side. The oplog window on every shard must exceed 24 hours at peak write rate with a margin (replSetResizeOplog, oplogMinRetentionHours). Monitor the age of the saved token against the oplog window, the pipeline's lag, and the growth of config.system.preimages. In Debezium, snapshot.mode=when_needed recovers a lost position at the cost of a new snapshot.