Long-running operations: 202 Accepted and polling

Difficulty: Intermediate

Question

An endpoint generates a large report that takes several minutes. How do you design the API?

Answer

If you make the client wait on an open HTTP connection for minutes, you run into timeouts at browsers, load balancers and gateways, wasted server threads, and no way to recover if the connection drops. It is like standing at the counter for an hour instead of taking a token and getting notified. The fix is to make the operation asynchronous and turn the work into a resource you can track.

Pattern: the client sends POST /reports with parameters. The server validates quickly, persists a job record, enqueues the work for a background worker, and immediately responds 202 Accepted with a Location header pointing at a status resource, such as /report-jobs/job_123, and often a Retry-After hint. Never respond 201 here, since nothing has been created yet; 202 means accepted for processing, not complete.

The client polls GET /report-jobs/job_123. The response includes a status such as queued, running, succeeded, failed or cancelled, and optionally progress percent, timestamps and error details. While running, return 200 with the status body. When finished, either include a link to the result, or redirect with 303 See Other to the final resource, e.g. /reports/rpt_9 or a signed download URL. This 202 plus Location plus polling flow is documented in Microsoft's REST guidelines and Google's long-running operations pattern.

Polling behaviour needs to be gentle: return Retry-After so clients don't hammer you, encourage exponential backoff, and rate limit the status endpoint. Since status reads are cheap, cache job state in Redis rather than querying the primary database each time.

Alternatives to polling: webhooks, where the client supplies a callback URL and you notify on completion; server-sent events or WebSockets for live progress in a UI; or a push notification and email for user-facing reports. Offering polling as the baseline plus webhooks as an optimisation is the most robust combination, because polling always works even if the callback endpoint is down.

Reliability aspects worth mentioning: make job creation idempotent using an Idempotency-Key so a retried POST doesn't launch duplicate reports; make workers idempotent and retry safe; set a maximum runtime and mark hung jobs failed; store results with an expiry and clean up; support cancellation with DELETE /report-jobs/job_123 or POST .../cancel; and secure the job so only its owner can read status and results (object-level authorization again).

Sizing: the result may itself be big, so return a pre-signed, time-limited URL to object storage like S3 instead of streaming through your API servers. For very large datasets, consider export files in CSV or NDJSON.

Not every slow endpoint needs this pattern. If p99 latency is under a couple of seconds, stay synchronous. Use async when work exceeds your gateway timeout, involves third parties, or has unpredictable duration. Also consider whether the operation truly should be a POST to a job or whether the domain concept can be modelled as a resource with a lifecycle state, like an order that moves from pending to confirmed.

Code examples

Submit and poll flow

POST /v1/reports HTTP/1.1
Idempotency-Key: 9c1e...
Content-Type: application/json

{ "type": "sales", "from": "2026-08-01", "to": "2026-08-31" }

HTTP/1.1 202 Accepted
Location: /v1/report-jobs/job_123
Retry-After: 5

GET /v1/report-jobs/job_123
HTTP/1.1 200 OK
{ "id": "job_123", "status": "running", "progress": 40 }

GET /v1/report-jobs/job_123
HTTP/1.1 303 See Other
Location: /v1/reports/rpt_9

202 with a status link, polling with progress, and a 303 redirect to the finished report.

Key points

Common follow-up questions

Interview tips

Concepts covered

async operations, 202 Accepted, polling, job status

More API Design questions