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

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.

Read only the changed documents, flatten them, land each refresh in object storage, then load it into the warehouse in one step so reports never see a half-loaded run.
  • 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
Stage, then replace by key
-- 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_area first. The load into warehouse_tables is 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