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:
- Send redirects as complete, empty responses: status,
Location, and an explicit empty body. - Don’t let framework defaults, adapters or shared SDK helpers decide the body for you on a
3xx. - Add a test that asserts the exact headers of the redirect, not just the status code.
Checklist for “slow only behind the CDN”
- Is the delay close to a known timeout (CDN, load balancer, Lambda)? Write the number down.
- Time the edge and the origin separately.
- Compare response types (200 vs 3xx vs 304), not just endpoints.
- Inspect the raw headers:
Content-Length,Transfer-Encoding,Connection. - 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.