Fundamentals

Streaming large results

An export that reads rows one at a time can still hold every row in memory; it only stays flat when each row goes straight to the file.

3 min read

The failure

A report export reads rows from the database and writes a CSV file. Built the obvious way, it holds the whole result in memory, sometimes more than once: the database driver’s copy, the list of rows in the app, and the CSV text before it reaches the disk. Memory grows with the size of the export until the process hits its limit.

The simulator

Start at a yearly export with everything loaded at once: it runs out of memory. Try the other strategies, then grow the rows.

SimulatorExport a report to CSV

The export

Turn on JavaScript to change the numbers.

Strategy

Query: buffered (holds every row) · Rows: kept in a list (holds every row) · CSV: built in memory (holds every row)

100,000
a yearly export: about 100,000 rows
40
the real report: up to 30–40
2 GB
the real limit: about 2 GB

Out of memory while reading row 88,081 of 100,000

Memory2 GB of 2 GB, over the limit
Rows written as CSV
0
File on disk
0 B
Every strategy at these numbers
StrategyPeak memoryResultRows written as CSVFile on disk
Load everything (current)over the limit (needs 2.3 GB)Out of memory at row 88,081 (reading)00 B
Generator over a buffered query795 MBCompleted100,00046 MB
Generator, CSV kept in memory78 MBCompleted100,00046 MB
Stream each row to the file32 MBCompleted100,00046 MB

Model with illustrative numbers, not measurements from the real export.

What’s happening

Every mechanism here is general. None of it is a claim about one team’s code.

Load everything. The driver’s copy of the result, the list of row objects and the CSV string all grow with the row count. Each row is held three times.

A generator over a buffered query. The app sees one row at a time, so it looks like streaming. But a buffered query has already fetched every row into the driver before the app sees the first one. The generator only changes how the rows are handed over. It is a common trap.

A generator with the CSV kept in memory. Now the rows really do pass one at a time. The output piles up instead: every row is added to a string that is written to the file only at the end.

The fix. An unbuffered query, a generator, and each row written to the file as it comes. In this model, peak memory then depends on the number of columns, not on the number of rows. All three pieces were needed in the real export: take any one away and memory grows with the export again.

By the numbers above, a yearly export of about 100,000 rows with up to 30 to 40 columns is up to 3–4 million values.

Unbuffered reads have a cost. In many drivers the connection cannot run another query until the result has been read to the end. In this case it caused no problems.

Where I hit this in production

The job was a report export: read data from the database and write it to a CSV report on the server. It was started by a user or by a scheduled job.

How many rows depended on what the user selected. A yearly export was about 100,000 rows, with up to 30 to 40 columns.

What failed was memory. It grew as the exported date range grew. The limit was about 2 GB. Before the fix, even the yearly export exceeded it, and larger ones always did.

The first attempt read the buffered results in chunks, meant to avoid loading everything. The better solution was to stream the data directly to the file on disk.

The fix was unbuffered queries, a generator, and streaming the output. The team built a reusable way in the app to combine a generator with streaming. Unbuffered queries caused no side effects, but on their own they were not the best solution.

To stop it coming back, there is an architecture rule: new reports reuse the generator and streaming setup.

← All fundamentals