Video in progress

Why is it bad to run an operation that takes several minutes (generating a report, processing a video) through a synchronous API, and what contract do you propose instead?

A long synchronous request runs into gateway and client timeouts, ties up a thread and a connection, and if it's cut off, resending creates duplicate work. Better is an asynchronous contract: 202 Accepted and a job resource, with status known through polling, a webhook or SSE.

📖 Answer

What happens in the synchronous version: the client waits 3 minutes, a load balancer in between drops the connection with its 60 s idle timeout, the client gets an error and resends. The server is finishing the first request anyway, so now two reports are being generated. On top of that, if the pod dies during a deploy, the work is lost.

The asynchronous contract:

POST /reports
Idempotency-Key: 7f3c...
{ "type": "sales", "from": "2026-10-01", "to": "2026-10-31" }

HTTP/1.1 202 Accepted
Location: /reports/jobs/8812
{ "jobId": 8812, "status": "QUEUED" }
GET /reports/jobs/8812
{ "jobId": 8812, "status": "DONE", "resultUrl": "/reports/files/8812.xlsx" }

Inside: the job is written to the DB and goes onto a queue (Kafka/RabbitMQ), and workers scale separately. The job state is in the DB, so if a worker dies another one continues or restarts it.

Ways to learn the result:

  • Polling: the simplest, telling the client how long to wait with Retry-After;
  • A webhook: for server-to-server integrations; signed, with retries, and the client receives it idempotently;
  • SSE/WebSocket: for showing progress in a UI.

The Idempotency-Key matters: if the client resends the POST after a network error, the server doesn't open a new job, it returns the earlier one.

Other things to think about: a deadline and cancellation for jobs (DELETE /reports/jobs/8812), how many jobs one client can run at once (a quota), and how long result files are kept. Without these, the queue fills up one day and everyone says "reports aren't working".