All at Once
A medium Pipeline Design interview practice problem on DataDriven. Design the pipeline on the canvas: sources, transforms, storage and checks, then get feedback on it.
- Domain
- Pipeline Design
- Difficulty
- medium
- Seniority
- L4
Problem
Application data lives as nested, schemaless documents in MongoDB, about 400 million of them, of which roughly 2 percent change each day. The nightly full export into Redshift takes 8 hours. Design a batch pipeline that pulls only the changed documents, reshapes them, and stages each refresh before it loads into the warehouse.
Two databases. One direction. No data left behind.
Business requirements
- The analytics team wants to query application data with the same SQL they use for everything else, without parsing nested documents.
- Analysts run reports against the warehouse all day, so each refresh should be assembled in a staging area before it lands in the tables they are querying.
Worked solution and explanation
Paying for the 98 percent that did not change
The nightly export spends 8 hours re-reading 400 million documents to deliver the around 8 million that changed. The idea that fixes it is the incremental load: keep a high-water mark of how far the last run got, read only what moved past it, and merge that slice into the warehouse by key. You can spot it whenever a question gives you a large total and a small daily change rate. Flattening and staging then answer the 2 stakeholder asks: plain SQL over flat tables, and a refresh that never looks half-done to someone running a report.
From MongoDB to the warehouse in 4 decisions
Step 1: Read only what changed
changed_document_extraction needs a way to tell changed documents from the rest. If the application stamps every write with an indexed updated_at, query the range from the stored watermark to the start of this run. If it does not, read the MongoDB change stream, which records every insert, update and delete. Either way the cost of a run now tracks the 8 million changes and stops tracking the size of the collection.
Step 2: Give schemaless data a schema
flattening_transform turns nested documents into rows. Top-level scalar fields become columns on a parent table keyed by _id, and each embedded array becomes a child table keyed by the parent _id plus the element's position. Since nothing enforces a schema at the source, the job declares the columns and types it expects, fills a missing field with NULL, and casts on the way in. Spark fits because the batch flattens in parallel.
Step 3: Land the batch in staging
Each run writes the flattened output to staging_area as files under its own path. Nothing in the warehouse has changed yet, so a failed extract or a bad flatten costs analysts nothing, and the files stay there to inspect or reload.
Step 4: Load in one transaction
warehouse_tables takes the staged files with a bulk load into a scratch table, then replaces the matching rows in the live table inside a single transaction. A report running during the load reads last night's rows until the commit and the new rows after it.
- mongodb_source (source, MongoDB)
- changed_document_extraction (transform)
- flattening_transform (transform, Spark)
- staging_area (storage, object storage)
- warehouse_tables (storage, Redshift)
- mongodb_source -> changed_document_extraction
- changed_document_extraction -> flattening_transform
- flattening_transform -> staging_area
- staging_area -> warehouse_tables
-- orders_stage is a scratch table, emptied before each run
-- 1. bulk load this run's staged files
COPY orders_stage
FROM 's3://staging/orders/run_date=2026-03-01/'
IAM_ROLE default
FORMAT AS PARQUET;
-- 2. swap the changed rows in one transaction
BEGIN;
DELETE FROM orders
USING orders_stage
WHERE orders.order_id = orders_stage.order_id;
INSERT INTO orders
SELECT * FROM orders_stage;
COMMIT;An illustrative orders table: the bulk load fills orders_stage, and the delete and insert commit together.
- Write into the live tables
- The flatten job inserts into the tables analysts are querying as rows arrive. A report that runs mid-load joins tonight's parent rows to last night's child rows, and a failure partway through leaves a mix that someone has to clean up by hand.
- Stage, then commit once
- The whole refresh sits in
staging_areafirst. The load intowarehouse_tablesis one bulk load and one transaction, so readers see the old rows or the new ones, and a failed run changes nothing they can see.
Document loads leave stale child rows
Common follow-up questions
- The load fails at 3am after the files are staged. What happens on the re-run?
The re-run loads the same staged files and replaces the same rows, so the tables end up identical, because the watermark has not moved and delete-then-insert by
_idis repeatable. Nightly jobs fail often enough that a re-run you have to think about turns into a recurring incident. - A team starts writing a new nested field next month. What does your pipeline do?
The declared column list in
flattening_transformignores the field, so the load keeps working, and a per-run count of unmapped fields tells you it appeared. Schemaless sources change without notice, and once you add the column you fill its history with a one-off full re-extract through the same path. - Analysts now want the data within the hour. What changes?
Run the same job hourly, since its cost tracks the number of changed documents and each run carries a few hundred thousand of them on average. Below a few minutes a scheduled range query stops fitting and you would consume the change stream continuously, a bigger build you can defer until someone asks for it.