Scrivio Interview library
DesignIntermediate7 min read · updated 12 November 2026

Your service must generate 100,000 PDF statements every month-end within two hours. Design the generation pipeline.

We put one job per statement on a queue and let a horizontally scaled pool of workers render PDFs in parallel, with each job idempotent and keyed by statement id so retries never produce duplicates. Workers keep templates and fonts loaded once rather than per document, store each PDF in object storage with metadata, and record progress and failures so failed statements can be retried. We size the pool from a load test of per-document time and memory, and validate output, as PDF/A if archiving requires it, with an audit record for every statement.

Why this question is asked

Month-end statement runs are a classic batch problem with a hard deadline: 100,000 customers expect their statement the next morning, and regulations often say how long the documents must be kept. A design that works for 1,000 statements can miss the window by hours at 100,000, and a crash at statement 63,000 must not mean starting again or sending some customers two statements. Interviewers ask this to see whether we can turn a throughput target into a concrete design. In this answer, we will do the capacity arithmetic, design the job flow, make jobs idempotent, speed up rendering, store and track the output, and finish with validation and audit.

1Turning the Deadline Into a Throughput Target

Two hours is 7,200 seconds, so 100,000 statements need about 14 documents per second on average. We should not plan for exactly that: the run needs time to start, some documents fail and retry, and the database that provides statement data has its own limits. A safe target is to finish in about one hour, which means roughly 28 documents per second.

Now we need the cost of one document, which only a load test can tell us. Suppose a realistic statement with three pages and a transaction table takes 250 ms of CPU to render with an HTML-to-PDF library once fonts and templates are warm. One core then renders 4 documents per second, so 28 per second needs about 7 busy cores. With 2-core workers and some headroom, 5 or 6 worker pods would do it. The numbers here are illustrative; the method, measure one document and multiply, is what matters.

Java
statements = 100_000
deadline = 2 h = 7_200 s -> 14 docs/s minimum
target = 1 h = 3_600 s -> 28 docs/s with headroom
per document = 250 ms CPU -> 4 docs/s per core (from a load test)
cores needed = 28 / 4 -> 7 cores
workers = 2 cores each -> 4 pods busy, 6 pods with headroom

2Designing the Job Flow

A scheduler starts the run: it creates a run record for October 2026 and one job per statement, and publishes the job ids to a queue such as Kafka, RabbitMQ or SQS. Workers consume jobs in parallel. For each job a worker loads the statement data, renders the PDF, uploads it to object storage, and marks the job done. Because work is pulled from a queue, adding workers increases throughput without any change to the code, and a slow or crashed worker simply leaves its messages for others.

The data loading step deserves care. 100,000 small queries arriving at once can overload the database at the start of the run. Often it is better to prepare the statement data in a batch step first, writing one row or document per statement into a staging table, so that rendering workers read pre-computed data by key.

Step throughOne statement through the pipeline
SchedulerQueueWorkerObject storageJob table
create run 2026-10 and 100,000 jobs
publish job ids
job: statement ACC-1042 / 2026-10
already done? no
load data, render PDF with cached template and fonts
put statements/2026-10/ACC-1042.pdf
mark DONE with checksum
acknowledge message

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

The message is acknowledged only after the file is stored and the job is marked done.

3Making Every Job Idempotent

Queues deliver at least once: if a worker crashes after uploading the file but before acknowledging the message, the job is delivered again. Retries after failures do the same. So every job must be safe to run twice, which we call idempotent.

We key everything by the statement's natural identity, account plus period. The object storage key is deterministic, statements/2026-10/ACC-1042.pdf, so a second render overwrites the same object instead of creating a duplicate. The job table has a unique key on (period, account), and a worker checks the status first and skips jobs already marked done. Customer notifications, which are not idempotent by nature, are sent by a separate step that reads only DONE jobs and records which emails were sent.

4Rendering Faster With Cached Templates and Fonts

Rendering time is dominated by work that does not need to be repeated per document. Template engines such as Thymeleaf parse templates once and cache them, as long as template caching is enabled, which it is by default in production profiles. Fonts are the bigger trap. Loading and parsing a TrueType font file, especially a large one covering many scripts, can take longer than laying out the whole statement. Code that reads the font file from disk for every document can easily double the cost.

So we load font files once per worker into memory and give the renderer the bytes, or use the library's mechanism for sharing fonts across documents (check what your library supports, since the embedded subset is still created per document). We also keep images such as logos in memory, avoid network fetches for CSS or images during rendering, and reuse renderer configuration objects where the library allows it. Each worker runs a small number of rendering threads, roughly one per core, since rendering is CPU-bound.

5Storing Output and Tracking Progress

Each PDF goes to object storage, with metadata stored next to it in the job table: account, period, object key, size, page count, a SHA-256 checksum and the template version used. Object storage is cheap, durable and serves downloads directly through short-lived signed links, so the customer portal never loads PDFs through our services.

Progress is a query on the job table: how many jobs are QUEUED, RUNNING, DONE and FAILED. A dashboard shows documents per second and the projected finish time, so we know at 30 minutes in whether we will make the deadline and can add workers. Failed jobs record the reason and the attempt count; transient failures retry automatically with backoff, and after three attempts a job goes to a dead-letter queue for a person to look at. A run is complete only when every job is DONE or explicitly handled.

Job states for a statement run
StateMeaningNext step
QUEUEDMessage published, not startedPicked up by a worker
RUNNINGA worker is rendering itDONE, or FAILED on error
DONEPDF stored, checksum recordedNotification step may send it
FAILEDError recorded with attempt countRetry with backoff, up to 3 times
DEADFailed after all retriesInvestigated and re-queued by a person

6Validating Output and Keeping Audit Records

Statements are often legal documents that must be archived for years. PDF/A is the ISO standard for long-term archiving: it requires embedded fonts, forbids external dependencies and encryption, and includes colour profiles, so the document looks the same in thirty years. If archiving rules apply, we generate PDF/A (many libraries can) and validate a sample of every run, or every document, with a validator such as veraPDF in a later pipeline step.

Finally, the audit record: for every statement we know which data snapshot and template version produced it, when, and its checksum. If a customer disputes a statement, we can show exactly what was generated and prove it has not changed since.

7Follow-up questions

Why not generate everything in one big batch job instead of a queue?

A single job can work, for example with Spring Batch partitioning, but it scales only as far as one process or a fixed set of partitions, and recovery depends on its restart logic. A queue with independent workers scales by adding pods, isolates failures to single statements and makes progress tracking simple. Both need idempotent writes.

How would you load test this?

Render a few thousand real-looking statements, with the largest transaction lists we expect, on one worker of the production size, and measure documents per second, CPU and memory per thread. Then run several workers against a production-like database to find where the shared parts, database and storage, start to limit throughput.

What if one statement is huge, with 5,000 transactions?

It takes much longer and much more memory than average. We detect such statements in the preparation step and route them to a separate queue with fewer, larger workers, so they do not block the main run or cause out-of-memory errors on normal workers.

8Conclusion

In this answer, we designed a month-end pipeline for 100,000 PDF statements. We turned the two-hour deadline into a throughput target and sized the workers from a measured per-document cost. A scheduler creates one idempotent job per statement, keyed by account and period, and queue-based workers render with cached templates and fonts, store PDFs in object storage with metadata, and record states so failures retry and progress is visible. PDF/A validation and audit records with checksums make the output fit for archiving.

What to say in the interviewAbout 80 seconds aloud

Open with

I'd start with arithmetic: 100,000 statements in two hours is about 14 per second, and I'd aim to finish in an hour, so about 28 per second. A load test gives me the per-document cost, say 250 milliseconds of CPU, so I'd need about seven cores, a handful of two-core worker pods with headroom.

Then, if they want more

The design is a scheduler that creates a run and one job per statement, publishes the job ids to a queue, and stateless workers that consume them in parallel and scale horizontally.

Each job is idempotent: keyed by account and period, with a deterministic object storage key and a unique job record, so a redelivered message just overwrites the same file or is skipped. Workers keep compiled templates and parsed fonts in memory rather than loading fonts per document, which is often the biggest cost, and render roughly one document per core.

PDFs go to object storage with metadata and a checksum in the job table, which also gives me progress, failure reasons and retries with a dead-letter queue. If the statements must be archived I'd produce PDF/A and validate it with something like veraPDF, and keep an audit record of the template version and data used for every statement.

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 "Your service must generate 100,000 PDF statements every month-end within two hours....": https://scrivio-interview-library.netlify.app/java/document-libraries/5adf4da9da/