REST API Asynchronous Operations
Some jobs take too long for a normal request. Generating a large report, converting a video, or importing a million rows can run for minutes. A client that waits on an open connection risks timeouts and wasted resources. Asynchronous design solves this by accepting the job at once and delivering the result later.
The Laundry Ticket Picture
You drop clothes at a laundry shop and receive a ticket. The shop does not make you stand at the counter for two hours. You leave, and you return later with the ticket to check progress and collect the clothes. An asynchronous API gives the client a ticket in the same way.
Synchronous vs Asynchronous
Synchronous: Client ── POST /reports ──→ Server (works for 4 minutes...) Client ←── 200 OK ───────── Server risk: timeout Asynchronous: Client ── POST /reports ──→ Server Client ←── 202 Accepted ──── Server (ticket: /jobs/88) Client ── GET /jobs/88 ───→ Server "running, 40%" Client ── GET /jobs/88 ───→ Server "done, result at /reports/31"
Step by Step Pattern
Submit the Job
The client sends a normal POST request. The server validates the input and queues the work.
Return 202 Accepted
The server responds immediately with 202 Accepted and a Location header that points to a status resource.
HTTP/1.1 202 Accepted
Location: /jobs/88
Retry-After: 5
{ "jobId": 88, "status": "queued" }
Check the Status
The client calls the status URL. The server reports the job state.
GET /jobs/88
{
"jobId": 88,
"status": "running",
"progress": 40
}
Collect the Result
A finished job points to its result.
{
"jobId": 88,
"status": "completed",
"resultUrl": "/reports/31"
}
Some APIs reply with 303 See Other and a Location header that sends the client straight to the result.
Job States
queued ──→ running ──→ completed
└──→ failed
queued ──→ cancelled
| State | Meaning |
|---|---|
| queued | Job waits for a worker |
| running | Worker processes the job |
| completed | Result is ready |
| failed | Job ended with an error |
| cancelled | Client stopped the job |
Polling Etiquette
Clients that poll too fast waste server capacity. The Retry-After header tells them how long to wait. Good clients increase the wait gradually, such as 2, 4, 8, and 16 seconds, and stop after a sensible limit.
Cancelling a Job
A DELETE request on the status URL cancels a queued or running job. Return 204 No Content on success and 409 Conflict when the job already finished.
Push Instead of Poll
Polling works everywhere but creates extra traffic. A client can register a callback URL when it submits the job, and the server sends a notification when the job ends.
A Video Conversion Example
A learning platform lets teachers upload lecture videos. Conversion into several screen sizes takes about six minutes. The upload endpoint stores the file, creates a job, and returns 202 Accepted with a status URL. The teacher sees a progress bar that reads the job record every few seconds. When the status turns completed, the page shows the finished video links. The teacher can close the browser and return later, because the job keeps running on the server.
Upload ──→ queue ──→ worker converts 360p, 720p, 1080p ──→ job completed
│
progress: 20% ... 60% ... 100%
Design Tips
- Pick 202 for accepted work and avoid 200 for jobs that have not finished.
- Keep job records for a defined period, such as 7 days.
- Return clear error details inside failed job records.
- Check ownership so users see only their own jobs.
Common Mistakes
Returning 200 Too Early
A 200 response implies completed work. Use 202 until the job truly finishes.
No Status Resource
Clients cannot track a job without a status URL. Always return one.
Endless Job Records
Old records consume storage. Delete or archive them after the retention period.
