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.
Client asks repeatedlyServer pushes eventREST 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.
Request/response resourcesServer → client streamBidirectional connection