Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

Several teams build large data exports (millions of rows) synchronously over HTTP and they keep timing out or overloading databases. Propose a standard approach.

The standard is an asynchronous export job: the client POSTs an export request and gets a job id, a worker streams rows from a read replica with fetch sizes and no ORM entity tracking into a file in object storage, and the client polls or is notified and downloads through a signed URL that expires. Jobs are queued and rate limited per tenant and globally so exports can never overload the primary database, and the whole pattern ships as a shared export service or library so teams stop solving it differently.

Why this question is asked

Large synchronous exports fail in two directions at once. For users, they time out after 30 or 60 seconds and leave truncated files. For the system, ten users clicking Export on a million-row report run ten long queries on the primary database, and checkout gets slow for everyone. When several teams hit this, the problem is a missing platform pattern, not a bug in one service. Interviewers ask this question to see whether we can design that pattern and get it adopted. In this answer, we will see why synchronous exports fail, design the job flow, protect the database, make the reading efficient, handle delivery and expiry, and package it so every team uses it.

1Why Synchronous Exports Keep Failing

A synchronous export holds an HTTP request open for the whole time it takes to query and write the data. Let us put numbers on a typical case: 5 million rows of about 200 bytes each is 1 GB of CSV. Even at a healthy 20,000 rows per second from the database, that is over 4 minutes. Every layer between the user and the service, the async timeout, the load balancer, the proxy, the user's own network, has to stay open for those 4 minutes, and any of them can end it.

The database side is just as bad. Each export is a long-running query, often with a sort over millions of rows, on the same primary database that serves transactions. Exports also arrive in bursts, for example at month end when every finance user exports the same report. So the design must take exports out of the request path and put a limit on how many run at once.

2The Job Flow

An export becomes a resource with its own lifecycle. The client creates it, the platform runs it, and the client fetches the result when it is ready.

Step throughAsynchronous export job
ClientExport APIJob queueWorkerObject storage
POST /exports {report, filters, format}
enqueue job (QUEUED)
202 Accepted, Location: /exports/81f2
job picked when a slot is free
stream rows from replica into file (multipart)
status READY, row count, size
GET /exports/81f2 (poll) or notification
signed download URL, expires in 15 min
download file directly

9 messages in order. Play them, or step through them one at a time.

Notice that every call to our API is short. The long part, reading and writing, happens in a worker that no HTTP timeout can touch, and the download goes straight to object storage, which handles gigabyte files well. If the worker crashes, the job is still in the queue or in the job table and can be retried from the start.

3Protecting the Databases With Queues and Limits

The queue is not just plumbing; it is the main protection. Workers have a fixed number of slots, say 8 for the whole platform, and each tenant may run at most 2 exports at a time. A burst of 300 month-end exports then becomes a queue that drains over an hour instead of 300 simultaneous queries. The API also rate limits job creation per user, for example 20 per hour, and can deduplicate identical requests from the same user so a double click does not run the same report twice.

Workers read from a read replica, a copy of the database kept up to date by replication, never from the primary. A replica may lag by a few seconds, which is fine for an export. Each export query also runs with a statement timeout, such as 30 minutes, so a bad filter cannot hold a replica connection forever.

4Reading Efficiently: Streaming, Fetch Size and No ORM Tracking

Inside the worker we stream, exactly as we would in a well-written endpoint: a cursor with a fetch size, one row at a time, written straight into a compressed output stream. We do not load entities through JPA, because the persistence context would keep every entity in memory and dirty checking would waste CPU on objects we never change. Plain JDBC with a RowCallbackHandler, or JPA projections into records, keep memory flat.

Java
void run(ExportJob job, OutputStream target) {
try (var out = new BufferedWriter(new OutputStreamWriter(new GZIPOutputStream(target), UTF_8))) {
replicaTx.executeWithoutResult(s ->
replicaJdbc.query(con -> {
var ps = con.prepareStatement(job.sql());
ps.setFetchSize(2000); // cursor, 2,000 rows per round trip
ps.setQueryTimeout(1800); // 30 minutes at most
return ps;
}, (RowCallbackHandler) rs -> writeCsvLine(out, rs)));
}
}
// 5,000,000 rows, ~1 GB CSV -> ~150 MB gzip, memory used: a few MB

The target stream writes into an object storage multipart upload, so even the file itself never sits fully in memory or on the worker's disk.

5Delivery, Expiry and Cleanup

When the job finishes, the user is told through polling, an in-app notification or an email with a link back to the product, never a direct file link in the email. The download URL is a signed URL, valid for a short time such as 15 minutes and generated each time the user clicks Download after an authorisation check, so a forwarded link stops working quickly.

Export files are copies of data, often personal data, so they must not live forever. A lifecycle rule deletes them after 7 days, the job record keeps who exported what and when as an audit trail, and very sensitive reports can require an approval before the job runs.

6Packaging It as a Shared Service

If each team builds this, we get ten job tables, ten worker pools and ten ways to forget the rate limit. The platform should provide it once, either as an export service teams register reports with, or as a library with the job table, queue integration, worker framework, CSV and spreadsheet writers (streaming ones, such as Apache POI's SXSSF for Excel), signed URLs and cleanup. A team then writes only the query and the column mapping for its report.

Adoption follows when the shared path is easier than the old one. We migrate the three exports causing the most incidents first, show their before-and-after database load, and add the job API to the service template. For small exports, say under 10,000 rows, synchronous streaming stays allowed, so teams are not forced into jobs where they add nothing.

Step throughThe shared export platform
  1. Team codereport query and column mapping only
  2. Export APIcreate, status, download link, rate limits
  3. Queue and workersglobal and per-tenant slots, retries
  4. Read replicasstreaming cursor, statement timeout
  5. Object storagegzip files, signed URLs, 7-day expiry

5 layers, from the top down. Step through them one at a time.

7Follow-up questions

Why not just add a bigger timeout and more database capacity?

Because both scale with the worst case. Long timeouts still break on deploys and flaky networks, and more database capacity is expensive and is used fully only at month end. A queue with a fixed number of slots gives predictable load, and the files survive client disconnects.

What if users need the data to be exactly consistent across tables?

Run the export inside a single read-only transaction at REPEATABLE READ (or the database's snapshot level), so all queries see the same snapshot. On a replica this still works, but long snapshots can delay cleanup or conflict with replication, so we keep exports bounded by the statement timeout and size them sensibly.

How does the client know the job is done without polling forever?

The status endpoint returns QUEUED, RUNNING with progress, READY or FAILED, and suggests a polling interval with a Retry-After header. Better, the platform publishes a completion event that the product turns into an in-app notification or email, and polling becomes a fallback.

8Conclusion

In this answer, we replaced synchronous HTTP exports with a standard asynchronous job: POST creates a job, a worker streams rows from a read replica with fetch sizes and no ORM tracking into object storage, and the client polls or is notified and downloads through a short-lived signed URL. Queues with global and per-tenant limits protect the databases, lifecycle rules remove old files, and a shared export service or library makes the safe pattern the easiest one for every team.

What to say in the interviewAbout 90 seconds aloud

Open with

I would make exports asynchronous jobs instead of long HTTP requests. The client posts an export request and gets 202 with a job id. A worker picks the job when a slot is free, streams the rows from a read replica with a cursor and a fetch size, writes them through a gzip stream into object storage, and marks the job ready.

Then, if they want more

The client polls the status or gets a notification, and downloads through a signed URL that expires after something like 15 minutes and is generated after an authorisation check.

The protection for the databases comes from the queue: a fixed number of worker slots globally, a limit per tenant, a rate limit on creating jobs, and statement timeouts, so a month-end burst becomes a queue rather than hundreds of simultaneous queries on the primary.

Inside the worker I avoid JPA entities, because the persistence context and dirty checking waste memory; JDBC with a row callback or projections keep memory flat even for millions of rows. Files are deleted after a few days and the job table keeps an audit trail.

Finally, I would ship this as a shared export service or library, so a team only writes its query and column mapping, migrate the noisiest exports first, and still allow small synchronous exports where a job adds nothing.

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 documentation below. 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 "Several teams build large data exports (millions of rows) synchronously over HTTP and...": https://scrivio-interview-library.netlify.app/spring-boot/web-other/b0f70f2aaa/