man@mndev:~/portfolio/notes/aws
← portfolio

Hunting a 30-second delay through CloudFront and Lambda

A page sometimes took ~30 seconds behind the CDN. How I narrowed it down layer by layer to one redirect response.

AWS··2 min read

Some requests to one of our pages took around 30 seconds to get through the CDN. Not every request, and never locally. A suspiciously round number is a clue in itself, so that’s where I started.

Why “exactly 30 seconds” matters

CloudFront’s default origin response timeout is 30 seconds. When a delay lines up with a timeout like that, it usually means one side is waiting for something the other side thinks it already sent. Often the thing it’s waiting for is the end of the response.

Split the path into layers

The request went through several hops: browser → CloudFront → Lambda (running behind Lambda Web Adapter) → app code and a shared SDK. I timed each hop separately instead of guessing.

curl -s -o /dev/null -D - \
  -w "connect=%{time_connect} ttfb=%{time_starttransfer} total=%{time_total}\n" \
  https://example.com/some/path

I ran it three ways:

Target What it tells you
Through CloudFront What users see
Straight to the origin URL Whether the app itself is slow
A path that returns 200 vs one that returns 3xx Whether the response type matters

The origin answered fast every time. CloudFront was slow only on the redirect paths. That ruled out cold starts and slow downstream calls, and pointed at how the redirect response itself was built.

The redirect was the problem

A redirect looks trivial: a status code and a Location header. But the edge still needs to know when the response is complete: a body length, or a cleanly closed stream. If it can’t tell, it keeps waiting until a timeout ends things.

The fix was in how the app produced that redirect response. The rule I now follow:

Checklist for “slow only behind the CDN”

  1. Is the delay close to a known timeout (CDN, load balancer, Lambda)? Write the number down.
  2. Time the edge and the origin separately.
  3. Compare response types (200 vs 3xx vs 304), not just endpoints.
  4. Inspect the raw headers: Content-Length, Transfer-Encoding, Connection.
  5. Once you find it, lock it in with a regression test at the HTTP level.

Most “CDN is slow” bugs I’ve seen turned out to be origin responses that weren’t quite finished.

// related