Asynchronous Operations and Long-Running APIs
Design APIs for jobs that cannot complete within one HTTP request.
202 Accepted and Background Jobs
Asynchronous Processing
Some operations take seconds or minutes: video processing, report generation, large imports, cloud provisioning, or data exports.
Instead of holding an HTTP connection open, the API can return 202 Accepted and create a job resource.
Example:
POST /exports → 202 Accepted
Location: /jobs/job_123
The client can then poll the job resource until it reaches a terminal state.
Polling vs Webhooks
Polling is simple: the client periodically asks for the current state. It works well when clients can initiate communication and updates are infrequent.
Webhooks allow the server to push event notifications to another system. They are efficient for server-to-server integrations but require the receiver to expose an endpoint.
Choose based on ownership, connectivity, reliability requirements, and whether the client can receive inbound requests.
Comparison
Client asks repeatedly
Simple but can create unnecessary traffic.
Server pushes event
Efficient but requires inbound endpoint and retry handling.
REST vs SSE vs WebSockets
REST over HTTP request-response is ideal for resource operations and ordinary application workflows.
Server-Sent Events (SSE) provide a one-way stream from server to client and are useful for notifications and live updates.
WebSockets provide bidirectional communication and are useful for interactive real-time systems such as chat, multiplayer applications, and collaborative editing.
These technologies are not competitors in every situation. A production application can use REST for CRUD operations, WebSockets for live collaboration, and webhooks for external integrations.
Transport Choice
Request/response resources
Best for standard API operations.
Server → client stream
Useful for live notifications and feeds.
Bidirectional connection
Useful for interactive real-time communication.