An endpoint generates a 2-minute report. Best API design?
Options
- Keep the HTTP request open for the full two minutes
- Enqueue a job, return 202 Accepted with a status URL, then poll (Correct option)
- Return a 500 error and ask the client to retry later
- Use a GET request with a very large timeout on the client
Answer
Enqueue a job (Celery/RQ/SQS), return 202 Accepted with a status URL, let the client poll or receive a webhook
Why
Load balancers and proxies usually cut connections after 30-60 seconds, and a user staring at a spinner is bad UX.
Instead: put the job in a queue, answer immediately with 202 Accepted and a URL like /reports/123/status. A background worker does the work.
The client checks the status URL (polling) or gets notified (webhook/WebSocket) when it is ready.
Remember
Slow work → background job + 202 Accepted.