Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

Partners send daily CSV files with 10 million rows and often change columns. Design a robust ingestion process.

We give every partner file format a versioned schema and check the header against it before reading any data. We stream the rows, validate each one, and write failures to a reject file with the row number and reason. Valid rows are loaded into staging tables in large batches, with COPY on PostgreSQL, and merged into the final tables idempotently. The job keeps checkpoints so it can restart, for example with Spring Batch, and we monitor volumes and rejection rates with alerts and automatic notices to partners.

Why this question is asked

Daily partner files are the backbone of many businesses: orders from marketplaces, prices from suppliers, transactions from banks. They are also fragile. A partner adds a column, renames another, sends yesterday's file twice or a file that stops half way, and an import that assumed a fixed layout either crashes at 3 a.m. or, worse, loads wrong data silently. Interviewers ask this design question to see whether we can build ingestion that is fast at 10 million rows and safe against change. In this answer, we will define the file contract, design the flow from landing to final tables, look at validation and rejects, loading with COPY and an idempotent merge, restartability, and finally monitoring and partner feedback.

1Contracting Each File Format With a Versioned Schema

The root problem with changing columns is that the format is an unwritten agreement. So we write it down: for each partner and file type, a schema that lists the columns, their types, which are required, formats for dates and decimals, the encoding, the delimiter and the version. The schema lives in version control, and each change creates a new version, such as orders v3, that the partner agrees to before sending.

The importer reads the header line first and compares it with the schema before touching any data. Columns are mapped by name, not position, so a reordered file still loads. A missing required column or an unknown version stops the file immediately with a clear message, instead of loading millions of shifted values. An extra column can be accepted and ignored, logged as a warning, which lets partners add data without breaking us; whether that is allowed is part of the contract.

2The Ingestion Flow From Landing to Final Tables

A file arrives, by SFTP or object storage upload, in a landing area. A job picks it up, computes a SHA-256 checksum and records it in an ingested files table; a checksum seen before means a duplicate delivery, and the file is skipped. The raw file is copied to an immutable archive, so we can always reprocess or prove what we received. Then the header is validated, rows are streamed and validated, valid rows go into a staging table and invalid ones into a reject file, and finally a merge moves staging data into the final tables in one transaction.

Step throughDaily partner file ingestion
  1. File landsSFTP or object storage
  2. Checksum already ingested?
    YesSkip as a duplicateNoRecord it, archive the raw file
  3. Header matches a schema version?
    YesContinueNoStop, notify the partner
  4. Stream and validate rowsrejects to a file with row number and reason
  5. Reject rate under the threshold?
    YesContinueNoStop before merging, alert
  6. COPY valid rows into staginglarge batches
  7. Merge staging into final tablesidempotent upsert, one transaction
  8. Report resultsmetrics, partner notice

8 steps in order. Play them, or step through them one at a time.

3Streaming, Validating and Rejecting Rows

Ten million rows must never be held in memory. A CSV parser such as Commons CSV reads one record at a time, so memory stays flat. Each record is validated against the schema: required fields present, types parse, values in range, references plausible. A valid row is converted to our internal form; an invalid row is written to a reject file together with its row number, the column and the reason, such as row 4,118,203, column amount: not a decimal (12,50).

We then apply a threshold. A handful of bad rows is normal; if 30 percent of rows fail, something is wrong with the whole file, such as a changed decimal separator, and loading the remaining 70 percent might do more harm than good. So if the reject rate passes a limit agreed per partner, say 1 percent, the job stops before merging and alerts. Below the limit, valid rows load and rejects are reported.

4Loading With COPY and Merging Idempotently

Inserting 10 million rows one statement at a time takes far too long. Even JDBC batch inserts are much slower than PostgreSQL's COPY command, which streams rows into a table in bulk and can load hundreds of thousands of rows per second on ordinary hardware. The PostgreSQL JDBC driver exposes COPY through CopyManager, so we can stream validated rows straight into an unlogged or ordinary staging table.

Java
CopyManager copy = new CopyManager(connection.unwrap(BaseConnection.class));
long loaded = copy.copyIn(
"COPY staging_orders (file_id, partner_id, order_ref, amount, ordered_at) FROM STDIN WITH (FORMAT csv)",
validatedRowsReader); // a Reader producing validated CSV lines
// loaded == 9_998_412
-- then, in one transaction, merge into the final table
INSERT INTO orders (partner_id, order_ref, amount, ordered_at)
SELECT partner_id, order_ref, amount, ordered_at FROM staging_orders WHERE file_id = :fileId
ON CONFLICT (partner_id, order_ref)
DO UPDATE SET amount = EXCLUDED.amount, ordered_at = EXCLUDED.ordered_at;

The merge is idempotent because it is keyed by the partner's natural key, partner and order reference: running the same file twice updates the same rows to the same values instead of creating duplicates. PostgreSQL 15 and later also offer MERGE for more complex rules. Doing the merge in one transaction means the final tables show either all of today's file or none of it, never half.

5Making the Job Restartable

A 10-million-row job can fail at row 7 million because a database restarts or a pod is evicted. Restarting from zero wastes time and, without idempotency, would duplicate data. So the job keeps checkpoints. Spring Batch gives us this structure: a job made of steps, such as validate-and-stage and merge, where the staging step processes rows in chunks of, say, 10,000, and after each chunk commits and stores its position in the job repository. On restart, the step continues from the last committed chunk, and steps already completed are skipped.

Our design makes restart even simpler: staging rows are tagged with the file id, so a failed staging step can delete that file's staging rows and start the step again, and the merge is idempotent anyway. The job instance is identified by the file checksum, so the same file is never processed as two different jobs.

6Monitoring and Notifying Partners

Each run records metrics: rows read, loaded and rejected, duration, and the file's arrival time. We alert on what matters to the business: a file that has not arrived by its expected time, a row count far outside the normal range (a partner who usually sends 10 million rows sends 2 million), a reject rate above the threshold, or a run that takes twice as long as usual.

Partners are notified automatically: a summary email or API callback after each file, with counts and a link to the reject file, and an immediate notice when a file is refused because its header does not match the schema. Many data quality problems are then fixed by the partner the same day, without a support ticket.

7Follow-up questions

Why load into staging first instead of directly into the final tables?

Staging lets us load fast with COPY without constraints and indexes slowing every row, inspect or count the data before it becomes visible, and merge in a single transaction. If anything goes wrong, we delete the staging rows for that file and the final tables were never touched.

How long would a 10-million-row file take?

It depends on row width and hardware, so we measure. As a guide, parsing and validating in Java can handle a few hundred thousand simple rows per second, and COPY is similar, so the staging step takes minutes, and the merge depends on indexes and how many rows already exist. The point of the design is that it scales linearly and restarts cheaply.

What do you do when a partner changes columns without warning?

The header check refuses the file before any data loads, the partner gets an automatic notice listing the differences, and our team gets an alert. If the change is reasonable, we add a new schema version and reprocess the archived file; nothing was loaded wrongly in the meantime.

8Conclusion

In this answer, we designed ingestion for daily 10-million-row partner CSV files. Each format has a versioned schema, and the header is checked before any data is read. Rows stream through validation, failures go to a reject file with row numbers and reasons, and a reject-rate threshold stops bad files. Valid rows load into staging with PostgreSQL's COPY and merge into the final tables idempotently in one transaction. Checkpoints with Spring Batch make the job restartable, duplicate files are detected by checksum, and monitoring with alerts and automatic partner notices closes the loop.

What to say in the interviewAbout 80 seconds aloud

Open with

I'd start with a contract: each partner file type has a versioned schema with columns, types, formats and encoding, kept in version control. The importer checks the header against the schema before reading data and maps columns by name, so reordering is fine, but a missing required column or unknown version stops the file and notifies the partner.

Then, if they want more

Files land in an archive, and I record a checksum so duplicate deliveries are skipped. Then I stream rows with a CSV parser, validate each one, and write failures to a reject file with the row number and reason; if the reject rate passes an agreed threshold, I stop before merging.

Valid rows go into a staging table in bulk with PostgreSQL's COPY through CopyManager, which is far faster than inserts, tagged with the file id. A merge, INSERT ON CONFLICT on the partner's natural key, or MERGE, moves them into the final tables in one transaction, so it's idempotent and all-or-nothing.

I'd run it as a Spring Batch job with chunked steps and checkpoints so it restarts from where it failed. Finally I'd monitor arrival time, row counts, reject rates and duration, alert on anomalies, and send partners an automatic summary with their reject file.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 15
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 "Partners send daily CSV files with 10 million rows and often change columns. Design a...": https://scrivio-interview-library.netlify.app/java/document-libraries/51ee92f4fb/