Long-running operations: 202 and polling

API design: REST, HTTP, GraphQL and gRPC Mid-level Python 3.13+ PostgreSQL 17+ Reviewed By Best Latest Apps

Order the steps

Order the lifecycle of an export request.

  • Client POSTs /exports with an Idempotency-Key
  • Worker picks the job and updates status to running
  • Worker writes the file and sets status succeeded with a result link
  • Client polling /exports/{id} sees succeeded and downloads
  • Server stores a pending operation, enqueues a job, returns 202 + Location

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A E B C D

  1. Client POSTs /exports with an Idempotency-Key Retries can't start two exports.
  2. Server stores a pending operation, enqueues a job, returns 202 + Location Request ends in milliseconds.
  3. Worker picks the job and updates status to running Progress is visible on the operation.
  4. Worker writes the file and sets status succeeded with a result link The link can be a short-lived presigned URL.
  5. Client polling /exports/{id} sees succeeded and downloads Polling used Retry-After as its interval.

30-second answer

Don't hold the request open. Accept it with 202 Accepted and a Location header pointing to an operation resource (/exports/{id}) that reports status (pending, running, succeeded, failed) and, when done, a link to the result. Clients poll that URL with backoff, or you notify them by webhook. The job runs in a worker from a queue, and creating it should take an idempotency key so retries don't start two exports.

Practise saying it · 30 s. No recording, just a timer

How do you expose a slow operation?

Say your answer out loud, then check which of these points you covered:

  • 202 + Location
  • Operation status resource
  • Worker from a queue
  • Polling or webhook
  • Idempotent create

Deep dive

Return Retry-After on the status resource to suggest a polling interval.

Store progress and errors on the operation so clients can show them.

Results can be a presigned URL with a short expiry.

Make cancel an explicit action on the operation.

Common mistakes

  • Holding an HTTP request for minutes behind load balancers with 60 s timeouts.
  • Polling every 100 ms.
  • No way to find the result after the client restarts.

Senior take & follow-ups

Describe 202 + status resource + worker + result link, with idempotency and polling hints.

Follow-up questions

Think of your answer first, then open the hint.

  1. Polling or webhooks?

    Show hint

    Polling is simplest for clients; webhooks for servers that can receive them.

  2. Where does the job state live?

    Show hint

    In a database row the worker updates.

Suggest a fix if something here is wrong or out of date.