liveBook · Manning

Long-running operations pattern

As explained in The Design of Web APIs, Second Edition

A long operation is an API interaction pattern in which work that exceeds normal request latency is initiated asynchronously and completed through later status checking or notification.

When an API operation takes more than a few seconds, waiting for it to finish in one request can strain connections and waste server resources if the connection fails. Start the work asynchronously, then let the consumer check its status or receive a notification. section 14.7

Common design choices include:

  • Return a reference to the running operation and let the consumer poll for status. section 14.7.1
  • Use a provider-initiated callback, similar to a webhook, to avoid polling; alternatively, let consumers choose an execution mode with the Prefer header. section 14.7

Stock market simulation

A simulation that can take an hour should not keep its POST request open until completion. Return 202 Accepted with the operation status and a Location header such as /simulations/12345. The consumer can poll that URL for IN_PROGRESS or DONE; progress, estimated completion time, or Retry-After can help guide polling frequency. section 14.7.1

How The Design of Web APIs, Second Edition covers it

How deep it goes
Compares polling, callbacks, and execution modes, then details polling requests, responses, progress data, and timing guidance.
Examples
Uses video analysis and stock market simulations to illustrate work that exceeds normal request latency.
What you can do after
Build asynchronous API flows that return an operation reference and let consumers check status without waiting on an open request.
How it connects
Connects asynchronous operations with polling, callbacks, webhooks, the Prefer header, and HTTP status and response headers.

Also explained in

Compare how 2 books explain long-running operations pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Design of Web APIs, Second Edition. The section links point to the parts of the book it draws on.