Web Development Bug Report

Our report endpoint kept hitting the API gateway's 29 second limit, so we made it asynchronous

Large customers' monthly reports failed with 504 after exactly 29 seconds, while the backend finished the work a minute later and threw it away. Raising timeouts only moved the limit. We switched to 202 Accepted with a job resource, and wrote down the limits to check on every gateway in the path.

Owen Gallagher Owen Gallagher @owengallagher · November 28, 2026 · 7 min read · 0 Views
Banner: long requests timing out at an API gateway versus an async job

Our analytics API lets customers download a monthly usage report as CSV. Small customers got their file in a few seconds. Our largest customers started reporting errors at the start of each month:

HTTP/2 504
content-type: application/json
x-amzn-errortype: IntegrationTimeout

{"message": "Endpoint request timed out"}

Every failure arrived after almost exactly 29 seconds. The backend logs for the same requests told a different story:

10:00:03.112 report.start  account=acc-0091 month=2026-10 rows_est=4.1M
10:01:19.406 report.done   account=acc-0091 duration_ms=76294 bytes=612003311
10:01:19.410 http.write    error="broken pipe: client disconnected"

The report took 76 seconds and completed. By then the gateway had already returned a 504 to the customer and closed the connection. The work was done and discarded. Customers retried, which started the same 76 seconds of work again, several times, at the busiest time of the month.

The path

client -> CloudFront -> API Gateway (REST API) -> ALB -> report service (ECS)

Each hop had its own timeout, and nobody had listed them together:

Hop Setting Value
CloudFront origin response timeout OriginReadTimeout 30 s (default)
API Gateway REST integration timeout integration timeout 29 s (default maximum)
ALB idle timeout idle_timeout.timeout_seconds 60 s
report service HTTP server write timeout WriteTimeout 120 s

The smallest limit in the chain wins. API Gateway's 29 seconds was the first to fire.

Why "just raise the timeout" was not the answer

The first proposal was to raise the limits. API Gateway now allows the integration timeout of REST APIs to be raised above 29 seconds for Regional and private APIs through a service quota request, at the cost of reduced throttle quotas for the account. CloudFront's origin timeout can be raised too, with a request for values above 60 seconds.

We decided against relying on it, for reasons that apply to any gateway:

  1. The work keeps growing. The largest report took 76 seconds that month. It would take longer as customers grew. Any fixed limit would be hit again.
  2. Long requests are fragile. A request that runs for minutes fails on any network blip, deploy, load balancer scale-in or mobile connection change. All of the work is lost each time.
  3. Clients have their own timeouts. Customer scripts used HTTP libraries with 30 or 60 second defaults. We cannot raise those.
  4. Retries multiply cost. Every timed-out request was retried, so heavy reports ran several times.
  5. Resources are held. Each running report held a worker, a database connection and gigabytes of buffered data for its whole duration.

The underlying mismatch is that a synchronous HTTP request is a poor container for work that takes longer than a person or script is willing to keep a connection open.

Environment

Item Value
Gateway Amazon API Gateway REST API, Regional
Backend Go service on ECS behind an ALB
Report size up to 4M rows, 600 MB CSV for the largest accounts
Failure 504 at 29 s for reports above roughly 25 s of work

1. 202 Accepted and a job resource

The endpoint now creates a job and returns immediately:

POST /v2/reports
Content-Type: application/json

{"type": "monthly_usage", "month": "2026-10"}
HTTP/1.1 202 Accepted
Location: /v2/reports/jobs/rj_7c41d2
Retry-After: 10
Content-Type: application/json

{"id": "rj_7c41d2", "status": "queued", "created_at": "2026-11-01T10:00:03Z"}

RFC 9110 describes 202 Accepted as "the request has been accepted for processing, but the processing has not been completed", which is exactly this. The Location header points at the job, and Retry-After suggests when to check.

The client polls the job:

GET /v2/reports/jobs/rj_7c41d2

HTTP/1.1 200 OK
{"id": "rj_7c41d2", "status": "running", "progress": 0.42}

When the job finishes:

HTTP/1.1 200 OK
{
  "id": "rj_7c41d2",
  "status": "succeeded",
  "result": {
    "url": "https://reports.example.com/rj_7c41d2.csv.gz?X-Amz-Expires=900&...",
    "expires_at": "2026-11-01T10:16:20Z",
    "bytes": 71204118
  }
}

The result is a presigned object storage URL, valid for 15 minutes. The download goes straight from object storage, not through the gateway or our service, so its size and duration no longer matter to any of the timeouts above. We also gzip the CSV, which shrank the largest file from 600 MB to 71 MB.

2. Make job creation idempotent

Clients that previously retried on 504 would now create duplicate jobs on network errors. POST /v2/reports accepts an Idempotency-Key header, and without one, an identical request (same account, type and month) within 10 minutes returns the existing job instead of starting a new one.

3. Run the work in a worker, not in the request

The job goes onto an SQS queue. A separate worker service generates the report, streaming rows from a read replica into a gzip writer that uploads to S3 in parts, so memory stays flat regardless of size. The worker updates job status and progress in a small table. Failures are recorded with a reason, and the job can be retried by the client with a new request.

4. Optional: notify instead of poll

For customers who prefer it, the request can include a callback_url. When the job finishes, we POST the job resource to it, signed with an HMAC header. Polling remains the default, because it works through every firewall.

5. List the timeouts for every path

We wrote down every hop's timeout for each public path, in the repository, and a test fails if any synchronous endpoint's p99 latency in the last 30 days is above half of the smallest timeout on its path. For API Gateway specifically, the limits we check:

Limit Value we use Notes
REST API integration timeout 29 s can be raised for Regional/private APIs via quota, with throttle trade-offs
HTTP API integration timeout 30 s hard maximum
Payload size 10 MB larger responses must go through object storage
WebSocket idle connection 10 min not used here

1. The largest report end to end. For the customer with 4.1 million rows:

JOB=$(curl -s -X POST "$API/v2/reports" -H "Authorization: Bearer $T" \
  -d '{"type":"monthly_usage","month":"2026-10"}' | jq -r .id)
while :; do
  S=$(curl -s "$API/v2/reports/jobs/$JOB" -H "Authorization: Bearer $T")
  echo "$S" | jq -c '{status, progress}'
  [ "$(echo "$S" | jq -r .status)" = succeeded ] && break
  sleep 10
done
{"status":"queued","progress":0}
{"status":"running","progress":0.11}
...
{"status":"succeeded","progress":1}

Job creation returns in about 40 ms. The report completes in about 70 seconds, and the download of the 71 MB file takes a few seconds directly from S3.

2. Month-start load. The first business day of the next month had zero 504s on the report endpoints. Report generation ran once per request instead of three to five times, and read replica CPU during the peak dropped by about 60 percent.

3. Client migration. The old synchronous endpoint stays for small reports (under 50,000 rows) and returns a Deprecation header plus a link to the job API. We contacted the customers whose scripts called it for large reports and helped them move.

4. Security implications. The presigned URL is the only way to fetch the file, expires after 15 minutes, and is generated only after checking that the caller owns the job. Job IDs are random and unguessable, and the job endpoint checks account ownership on every read, so knowing a job ID is not enough. Callback URLs are validated against a customer-registered allow list to prevent the callback feature from being used to make our servers send requests to arbitrary internal addresses.

Takeaways

  • The smallest timeout on the request path wins. List every hop's limit in one place.
  • Raising gateway timeouts only moves the limit for work that keeps growing.
  • For long work, return 202 Accepted with a job resource, Location and Retry-After.
  • Deliver large results through presigned object storage URLs, and make job creation idempotent.

References: RFC 9110, 202 Accepted, Amazon API Gateway quotas, CloudFront origin response timeout, Amazon S3 presigned URLs.

Add a label

#aws #api-gateway #timeouts #async-jobs #http-202
Written by
Owen Gallagher @owengallagher

I am a site reliability engineer on a platform team that runs the edge for dozens of internal services: API gateways, reverse proxies, load balancers and the resilience settings everyone copies from each other. The incidents I learn most from happen when a safety mechanism does the wrong thing. A rate limit that locked out an entire office behind one IP, a circuit breaker that never opened, a liveness probe that restarted every pod during a database blip, two nodes that both decided they were primary. Each post explains what the mechanism was meant to do, why it misfired in our setup, how we changed the configuration and how we drilled the failure afterwards to prove it. I keep the examples close to real configs so you can compare them with your own.

Advertisement

Comments

0

No comments yet. Be the first to comment!

Sign In to post a comment.