Stream a Large Transaction CSV Export in Constant Memory
Last updated: November 29, 2026
Quick Overview
A customer with millions of card transactions clicks 'Export CSV' and the worker runs out of memory. Rewrite the export to stream rows in pages with bounded memory and keep it safe to open in a spreadsheet.
Ramp
November 29, 20260
0
0 solved
A customer with millions of card transactions clicks 'Export CSV' and the worker runs out of memory. Rewrite the export to stream rows in pages with bounded memory and keep it safe to open in a spreadsheet.
Ramp's finance customers export transactions for audits and month-end close, and large enterprises have far more rows than a naive `fetchall()` can hold. The interviewer looks for generators, keyset pagination instead of OFFSET, and awareness of CSV pitfalls like quoting and formula injection.
What the Interviewer Expects
- Explains why loading every row and building one string blows up memory
- Uses a generator that yields chunks so the HTTP layer can stream
- Paginates with `WHERE id > last_id` instead of OFFSET
- Lets the csv module handle quoting of commas and quotes
- Neutralizes spreadsheet formula injection in free-text fields
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you resume an export that fails halfway through?
- When would you move this to an async job that emails a download link?
- How do you keep the export consistent if rows are inserted while it runs?
- How would you add gzip without buffering the whole file?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
Export one business's transactions as CSV with a header row. Peak memory must not grow with row count. Output must parse back correctly even when memos contain commas or quotes, and opening it in Exce...
High-Level Design
The original code did rows = cursor.fetchall() and then joined strings, so memory was proportional to the full result twice over. I turn the export into a generator. Each iteration fetches one page ...