Skip to content

Design a pipeline that ingests a 10-million-row CSV with live progress

AdvancedAsked sometimesSystem designDesignArchitecture
Asked atAmazonFlipkartRazorpay
#streams#queues#idempotency#websockets#postgres

Company tags are compiled from public interview reports and are signals, not guarantees.

What interviewers are testing

Bulk ingestion is where senior actually gets tested: memory-safe streaming, work distribution, at-least-once delivery made harmless, and progress that never lies after a crash. Interviewers watch for the chunk-transaction insight — idempotency and progress are the same design problem, solved by committing results and the done-mark together — and for treating the WebSocket as a cache of Postgres rather than a database of its own.

Mental model

Nobody ships one giant job; you ship chunks. Each chunk is a single database transaction that is safe to replay: the rows and the "done" mark commit together, so a crash either happened before the chunk (redeliver, no trace) or after (nothing to replay). Progress is derived from committed chunks — never a drifting counter — and every realtime channel is allowed to lose messages, because the snapshot endpoint always heals.

Step-by-step solution

  1. Step 1: Stream the upload — memory stays flat

    The first failure mode is buffering: reading the file into memory, or worse, into a string, kills the Node process at a few gigabytes. The fix is to never hold the file at all — the browser slices it into multipart parts, and the API pipes each part straight through to object storage as it arrives. Backpressure does the flow control: when storage is slow, the stream pauses instead of growing a buffer. When the upload completes, the API holds only a pointer and a job id, inserts the job row, and enqueues the chunking task — then answers 202 immediately. Watch the animation: the HTTP request finishes in seconds regardless of file size, and the heavy work happens entirely off the request path. This is also what keeps the API responsive for everyone else: an endpoint that streams cannot be the endpoint that stalls under a large upload.

  2. Step 2: Chunk the job, protect the neighbors

    The second classic failure: enqueuing the whole file as one queue message. A single 10-minute task cannot be parallelized, cannot be retried in parts, and a redelivery restarts it from zero. The design instead cuts the object into small chunks — tens of thousands of rows each, split on record boundaries so no row is torn — and every chunk becomes its own queue item carrying its job id and row range. That alone gives parallelism and partial retries. The fairness dimension: queue per tenant (or weighted lanes), so one customer's 10-million-row flood occupies their lane, not the pipe every other customer shares. Watch the animation: the chunker only ever streams and slices; workers pull chunks from lanes, round-robin across tenants, and each claim is a lease that expires if the worker dies. One job becomes tens of thousands of small, parallel, independently retryable units — and starvation is a scheduling decision you made, not an accident you discovered.

  3. Step 3: Exactly-once chunks: commit the work and the mark together

    Here is the answer to the bonus question, stated as a design rule: never update progress separately from the work. Each chunk runs as one database transaction — insert the chunk's rows with a natural key, mark the chunk done, commit. Crash after 50,000 rows but before COMMIT? Those rows rolled back; the chunk never happened, and the queue redelivers it whole. On redelivery, ON CONFLICT makes the rows already committed by a partial earlier attempt a no-op — but there is nothing to detect and no offset to store, because the transaction boundary is the checkpoint. And progress? It is not a counter you increment; it is derived — COUNT the committed chunks, which can never drift from reality because it is computed from it. Read the code trace: the same BEGIN/COMMIT protects the rows, the done-mark, and therefore the progress number the user sees. Replay is not the failure path — it is the design.

  4. Step 4: Realtime progress: WebSockets as a cache of Postgres

    Progress has exactly one source of truth — the database — and the realtime channel is only its fast delivery. The worker that commits a chunk performs the progress update in the same transaction, then publishes a small event afterward (best-effort; lossy is acceptable precisely because Postgres holds the truth). Redis pub/sub fans the event to every WebSocket gateway instance, whichever one holds the user's connection, and the browser receives throttled updates — per second, never per row. Now the two hostile cases. Refresh or disconnect: the client re-fetches GET /jobs/:id for an authoritative snapshot, then resubscribes — missed events heal instantly, so the gateway can be stateless and disposable. Worker crash: the chunk's lease expires, the queue redelivers, and the replay is a no-op by construction from the previous step. Watch the animation: the database never moves, everything else is allowed to fail.

  5. Step 5: PostgreSQL when the table is 10 million rows deep

    At this scale the database patterns change shape. Rows go in as multi-row batches or COPY — tens of thousands at a time — because per-row inserts are 10 million round trips of overhead. Progress queries stop scanning: chunk-level state is tiny and indexed, so "how far are we" is O(chunks), not O(rows). Tables partition by job or by time range, which keeps per-job working sets small and lets retention be DROP PARTITION instead of a mass DELETE that bloats and locks. Indexes are chosen for the queries that actually run — the natural key's UNIQUE index carries both idempotency and lookups — and built with the workload in mind rather than reflexively on every column. Compare the panels: the left column is what happens when each row is an event, the right is what happens when the table is treated as a bulk store with a few sharp queries.

Edge cases & traps

  • Byte-range chunking can split a quoted CSV row in half: slice on record boundaries (or carry the trailing partial line into the next chunk) or the parser fails on valid files.
  • Zombie workers originate from lease math: if the visibility timeout is shorter than a worst-case chunk, two workers can hold one chunk — the idempotent writes make it harmless, never the lease.
  • Per-row WebSocket events melt the gateway on a 10M-row job: throttle to per-second aggregates, use per-job channels, and let reconnecting clients rely on the snapshot, not a backlog of events.
  • Bounded queues per tenant are the fairness mechanism: without a cap on in-flight chunks per customer, one upload can saturate the entire worker pool even with separate lanes.
  • A job must never query another job’s half-written data: stage rows per job and only publish results when the job completes, so readers see all-or-nothing.

Follow-up questions

Go deeper: Explore the System Design visualizer