How do you handle long-running async operations in REST APIs?
Answer
Long-running operations (video processing, report generation, batch imports) should not block the HTTP connection. The accepted pattern is the 202 Accepted response: the API immediately returns 202 with a job ID and a link to poll status: { "jobId": "abc123", "statusUrl": "/jobs/abc123" }. The client polls GET /jobs/abc123, which returns the job status (pending, processing, completed, failed). On completion, the response includes a link to the result: { "status": "completed", "resultUrl": "/reports/xyz" }. The alternative to polling is webhooks — the client registers a callback URL and the server POSTs to it when the job finishes. Webhooks are more efficient than polling but require the client to expose a public HTTPS endpoint. A third option is Server-Sent Events (SSE) for streaming progress updates.
Previous
What is the difference between PUT and PATCH?
Next
What are the best practices for filtering, sorting, and searching in REST APIs?
More REST API Design Questions
View all →- Intermediate What is HATEOAS and how is it implemented?
- Intermediate What are the main API versioning strategies in REST and what are their tradeoffs?
- Intermediate What are the pagination strategies in REST APIs?
- Intermediate What is rate limiting and how is it communicated in REST APIs?
- Intermediate How does HTTP caching work in REST APIs with ETag and Cache-Control?