Quick revision
The short answer
POST /reports should not wait for the report. It creates a job and returns 202 Accepted with a Location header pointing to a status resource such as /report-jobs/{id}. Clients GET that resource to see the state (PENDING, RUNNING, SUCCEEDED or FAILED), the progress and, when it is done, a link to the result; they poll at the pace a Retry-After header suggests, or get a webhook or server-sent event instead. An Idempotency-Key makes retries safe, and jobs and results expire after a documented retention period.
What to remember
In this answer, we designed an API for a report that takes 5 to 20 minutes. POST /reports returns 202 Accepted with a Location header for a job resource, the job moves through PENDING, RUNNING, SUCCEEDED and FAILED with progress and a link to the result, clients poll at the pace Retry-After sets or receive a webhook or server-sent event, an Idempotency-Key stops retries from starting duplicate jobs, and jobs and results expire after a documented retention period.
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.
POST /reports should not wait for the report. It creates a job and returns 202 Accepted with a Location header pointing to a status resource such as /report-jobs/{id}. Clients GET that resource to see the state (PENDING, RUNNING, SUCCEEDED or FAILED), the progress and, when it is done, a link to the result; they poll at the pace a Retry-After header suggests, or get a webhook or server-sent event instead. An Idempotency-Key makes retries safe, and jobs and results expire after a documented retention period.
Why this question is asked
Holding an HTTP connection open for 20 minutes fails in practice long before the report is ready: load balancers and proxies cut idle connections after a minute or so, mobile networks drop, and every waiting request occupies a server thread. Interviewers use this design question because the answer is a well-known pattern with a few details that separate a solid API from a fragile one. In this answer, we will see why a long request breaks, then design the 202 response, the job resource and its states, how clients learn that the job is done, how to make job creation idempotent, and how long we keep jobs and results.
1Why a 20-Minute Request Breaks
In a synchronous design, the client sends POST /reports and waits for the response with the finished report. Between the two there are many components, each with its own timeout. A cloud load balancer often closes connections that see no data for 60 seconds, a reverse proxy may have a read timeout of 60 to 120 seconds, and the client's HTTP library may give up even earlier. So the report finishes after 15 minutes and nobody is there to receive it.
Even when the connection survives, it is expensive. Each waiting request holds a thread on our server, and if a client retries after its timeout, we start a second identical report. The fix is to separate starting the work from getting the result.
2Starting the Job: 202 Accepted With a Location
The POST now only validates the request, records a job and returns at once. The status code 202 Accepted means exactly this: the request was accepted for processing, but the processing is not finished. The Location header tells the client where to look for progress.
Notice that the response already carries the job's first state, so the client does not need an extra call to show 'queued'. Retry-After: 30 tells it how long to wait before checking.
3The Job Resource and Its States
The job is a resource of its own, readable with GET. It has a small, fixed set of states. PENDING means it is waiting for a worker, RUNNING means a worker is building the report, SUCCEEDED means the result is ready, and FAILED means it will not be produced. SUCCEEDED and FAILED are final: once a job reaches them it never changes again, which lets clients stop polling.
- POST /reports202 Accepted, Location /report-jobs/{id}
- PENDINGwaiting for a worker
- RUNNINGprogress 0 to 100
- Did the report build succeed?YesSUCCEEDED, link to /reports/{id}NoFAILED, problem details in error
- Expirejob and result removed after the retention period
5 steps in order. Play them, or step through them one at a time.
Notice two choices. GET on the job returns 200 in every state, because the job itself was found; the state is in the body, and the failure reason is a problem details object inside it. And the result is a separate resource, /reports/r_2207, linked from the job. That keeps the job small and lets the report be downloaded, cached or streamed like any other resource.
4Telling the Client the Job Is Done
Polling is the baseline every client can use: GET the job, wait the number of seconds in Retry-After, repeat until the state is final. The server controls the load through Retry-After, for example 30 seconds while pending and 60 while running a report that takes 15 minutes. Without that hint, clients tend to poll every second, which is 900 requests for one report.
For clients that can receive calls, a webhook avoids polling. The client registers a callback URL, and when the job reaches a final state, we POST a small notification with the job id. The notification should carry only the id and state, and the client then GETs the job, so a forged or replayed webhook cannot inject data. For browser clients, server-sent events on a URL such as /report-jobs/{id}/events can push state changes while the page is open. Both are additions; the job resource stays the source of truth.
8 messages in order. Play them, or step through them one at a time.
5Making Job Creation Idempotent
Networks fail at the worst moment. If the 202 response is lost, the client does not know whether the job was created and will retry the POST. Without protection, every retry starts another 20-minute report. So the client sends an Idempotency-Key header, a unique value it generates per logical request, and the server stores it with the job.
When a POST arrives with a key the server has seen before for the same client, it does not create a new job; it returns the same 202 and the same Location as the first time. If the same key arrives with a different body, the server rejects it with 422, because that is a client bug. Keys are kept at least as long as clients might retry, typically 24 hours.
6Retention and Expiry
Reports and job records cannot be kept forever: reports can be large and may contain sensitive data. So we set and document a retention period, for example results for 7 days and job records for 30 days, and show the expiry time in the response, as expiresAt does above. After that, GET on the result returns 410 Gone or 404, and the documentation says which, so clients know to start a new job instead of retrying.
Documenting retention is part of the contract. A client that stores the result link in its own database for a monthly email must know the link stops working after a week.
7Follow-up questions
Should GET on a finished job redirect to the result with 303 See Other?
Some APIs do that, and it is valid HTTP. Returning 200 with a result link is usually friendlier, because many HTTP clients follow redirects automatically and would download a large report when they only wanted the status. Whichever we choose, it must be the same across the API and documented.
How would the client cancel a report it no longer needs?
We can offer DELETE /report-jobs/{id} or POST /report-jobs/{id}/cancel. The job then moves to a CANCELLED final state, and the worker checks for cancellation between steps and stops. Cancelling a job that is already final has no effect.
Why not use a WebSocket instead?
A WebSocket keeps a connection open for the whole job, which brings back the timeout and resource problems for something that changes state only a few times. Polling with Retry-After, webhooks or server-sent events are simpler and survive reconnects, because the job resource always holds the current state.
8Conclusion
In this answer, we designed an API for a report that takes 5 to 20 minutes. POST /reports returns 202 Accepted with a Location header for a job resource, the job moves through PENDING, RUNNING, SUCCEEDED and FAILED with progress and a link to the result, clients poll at the pace Retry-After sets or receive a webhook or server-sent event, an Idempotency-Key stops retries from starting duplicate jobs, and jobs and results expire after a documented retention period.
Open with
I would turn the request into a job. POST /reports validates the input, stores a job and immediately returns 202 Accepted with a Location header like /report-jobs/rj_81f3, a Retry-After hint, and the job's first state in the body.
Then, if they want more
The job is its own resource. GET on it returns 200 with a status of PENDING, RUNNING, SUCCEEDED or FAILED, a progress value, timestamps, and when it succeeded a link to the result, which is a separate resource such as /reports/r_2207. If it failed, the job carries a problem details object explaining why.
SUCCEEDED and FAILED are final, so clients know when to stop. Clients poll at the pace the server sets with Retry-After, say every 30 to 60 seconds, so a 15-minute report costs a few dozen requests instead of hundreds.
Clients that can receive calls can register a webhook, and browsers can use server-sent events, but the notification only carries the id and the client re-fetches the job. To make retries safe, the client sends an Idempotency-Key, and a repeated POST with the same key returns the same job instead of starting a second 20-minute report.
Finally I document retention, for example results for seven days and jobs for thirty, show expiresAt in the response, and return 410 Gone afterwards.
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.
- Official documentation
- Spring Framework reference, Spring Web MVC
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 "A POST /reports request takes 5 to 20 minutes to generate a report. Design the API so...": https://scrivio-interview-library.netlify.app/spring-boot/rest-api-design/c8d35b9afd/