Quick revision
The short answer
We turn every export into an asynchronous job: the user asks for it, a worker streams rows from the database page by page into an SXSSF workbook, uploads the file to object storage and notifies the user with a short-lived download link. Because a sheet holds at most 1,048,576 rows, a 2-million-row report is split across sheets or offered as CSV. Access control and masking are applied per report when the job runs, and quotas, monitoring and file expiry keep the service affordable and safe.
What to remember
In this answer, we designed a self-service export capability. Exports run as asynchronous jobs: the API records and queues them, workers stream rows from the database with a fetch size or keyset pages into an SXSSF workbook, upload to object storage and notify the user with a pre-signed link. Reports beyond Excel's 1,048,576-row limit are split across sheets or offered as CSV. Permissions and masking are applied per report in the worker, every export is audited, and quotas, monitoring and expiry keep the platform safe and affordable.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We turn every export into an asynchronous job: the user asks for it, a worker streams rows from the database page by page into an SXSSF workbook, uploads the file to object storage and notifies the user with a short-lived download link. Because a sheet holds at most 1,048,576 rows, a 2-million-row report is split across sheets or offered as CSV. Access control and masking are applied per report when the job runs, and quotas, monitoring and file expiry keep the service affordable and safe.
Why this question is asked
Self-service exports are popular with finance teams because they let people work with data without waiting for IT. They are also one of the easiest ways to take down a reporting service: a single request for 2 million rows, built in memory inside an HTTP request, can exhaust the heap and time out the gateway. Interviewers ask this design question to see whether we can turn a simple-looking feature into a platform capability that scales and stays secure. In this answer, we will look at why synchronous exports fail, design the job flow, keep memory flat while writing, handle Excel's row limit, add access control and masking, and finish with quotas, monitoring and expiry.
1Why a Synchronous Export Fails at This Size
The naive design is an endpoint that runs the report query, builds a workbook and streams it back in the response. For 2,000 rows that works. For 2 million rows with 20 columns it means 40 million cells. With a normal XSSFWorkbook, each cell is an object in memory, so the heap needed runs into many gigabytes. The query itself can take minutes, and load balancers and gateways often cut requests at 30 or 60 seconds. If the user refreshes, a second copy starts.
So the first design decision is to separate asking for an export from producing it. The request returns immediately with a job id, and the work happens in the background where we control time, memory and concurrency.
2The Export Job Flow
The user picks a report and its filters in the UI and presses Export. The API checks that the user may run this report, records an export job (user, report, filters, status QUEUED) and puts a message on a queue. A pool of export workers takes jobs from the queue. A worker runs the report, writes the file, uploads it to object storage such as S3 under a key like exports/2026/11/job-8812.xlsx, and marks the job DONE. The user receives a notification, by email or in the app, with a download link.
The link is a pre-signed URL, a link that grants access to one object for a limited time, for example 24 hours, so the file is never public and the download goes straight from object storage without loading our service.
10 messages in order. Play them, or step through them one at a time.
The request returns at once; the heavy work runs in workers with their own limits.
3Keeping Memory Flat While Writing
The worker must never hold the whole report in memory, neither the database rows nor the workbook. On the database side we read in pages or with a streaming cursor. With JDBC we set a fetch size so the driver brings, say, 5,000 rows at a time; PostgreSQL's driver only streams when the connection is not in autocommit mode, so we run the read inside a read-only transaction. Keyset pagination (WHERE id > :lastId ORDER BY id LIMIT 5000) is another good option, because it stays fast on later pages, unlike OFFSET.
On the Excel side we use SXSSFWorkbook, POI's streaming writer. It keeps only a window of recent rows in memory, such as 100, and flushes older rows to a temporary file. Memory use is then roughly the same for 2,000 rows and for 2 million.
Notice that the styles are created once and reused, column widths are fixed rather than auto-sized, and the output goes to a file on disk, which we then upload, never to a byte array in memory.
4Respecting Excel's Row Limit
An Excel sheet holds at most 1,048,576 rows, including the header. A 2-million-row report does not fit on one sheet. We have three honest options. We can split it across sheets (Report 1, Report 2), each with its own header, which keeps it in one file but makes filtering across the data awkward. We can produce CSV instead, which has no row limit and is smaller and faster to write, but has no formats or types. Or we can warn the user before the job starts, using a cheap count query, that the result is too big for Excel and suggest narrowing the filters.
A good platform shows the estimated row count on the export screen and chooses for the user: .xlsx up to a threshold such as 500,000 rows, a choice between split sheets and compressed CSV above it. Very large Excel files are also slow to open, even when they fit, so the threshold protects the person reading the file as well as our servers.
5Access Control and Data Masking
Exports are a common way for data to leave a company, so security must be part of the design. The API checks permission when the job is requested, and the worker applies the same row-level and column-level rules when it runs the query, using the identity of the user who asked, not the worker's own all-powerful account. If a user may see only their region, the export contains only their region.
Columns with personal or sensitive data, such as account numbers or salaries, are masked or removed according to the report's definition and the user's role, for example showing only the last four digits. Every export is written to an audit log: who exported which report, with which filters, how many rows, and when the file was downloaded. Many organisations need that record for compliance.
6Quotas, Monitoring and Expiry
Without limits, a self-service feature becomes a way to overload the database. We set quotas: for example at most two running exports per user, twenty per day, and a global limit on concurrent workers so reports cannot starve the main database; ideally exports read from a replica. Identical requests from the same user within a few minutes reuse the existing job.
We monitor queue depth, job duration, rows per second, failures and file sizes, and alert when the queue waits too long. Generated files expire: the link after 24 hours, and the object itself after, say, seven days through an object storage lifecycle rule. That keeps storage costs down and limits how long sensitive data sits outside the database.
7Follow-up questions
How long would a 2-million-row export take?
It depends on the query and the column count, so we measure it with a load test rather than guess. As a rough guide, streaming writers can produce on the order of tens of thousands of rows per second per worker, so a job like this takes minutes, not seconds. That is exactly why it must be asynchronous and why we show the user a progress state.
Why not stream the file directly in the HTTP response?
Streaming avoids the memory problem, but the request still runs for minutes, can be cut by proxies, starts again on every refresh and cannot be retried or audited easily. A job with object storage survives worker restarts, can be retried, and lets the user download the file again without regenerating it.
What if the report data changes while the job runs?
Reading in one read-only transaction gives a consistent snapshot in PostgreSQL, but holding a long transaction has costs on a busy primary. Reading from a replica, or recording an as-of timestamp in the filters and querying by it, gives consistent results without holding the primary.
8Conclusion
In this answer, we designed a self-service export capability. Exports run as asynchronous jobs: the API records and queues them, workers stream rows from the database with a fetch size or keyset pages into an SXSSF workbook, upload to object storage and notify the user with a pre-signed link. Reports beyond Excel's 1,048,576-row limit are split across sheets or offered as CSV. Permissions and masking are applied per report in the worker, every export is audited, and quotas, monitoring and expiry keep the platform safe and affordable.
Open with
I would make every export an asynchronous job. The API checks the user's permission for the report, records a job and puts it on a queue, and returns a job id immediately.
Then, if they want more
Export workers take jobs, run the report with the user's own row and column permissions, and stream the data: on the database side with a JDBC fetch size inside a read-only transaction, or keyset pagination, and on the Excel side with SXSSFWorkbook, which keeps about a hundred rows in memory and flushes the rest to temporary files, so memory stays flat.
The file goes to disk and then to object storage, and the user gets a notification with a pre-signed URL valid for about a day. Because a sheet holds at most 1,048,576 rows, a 2-million-row report is split across sheets or, better, offered as CSV, and I would show an estimated row count before the user starts.
Sensitive columns are masked per report and role, and every export is audited. Finally I would add quotas per user and globally, read from a replica, monitor queue depth and job duration, and expire links and files with a lifecycle rule.
About this answer
- Last updated
- 12 November 2026
- 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 "Finance wants self-service Excel exports of any report, some with 2 million rows. Design...": https://scrivio-interview-library.netlify.app/java/document-libraries/ea171c3677/