Solving connections, not requests for performance
For a server-side rendered page, the bottleneck is often network
requests. The slowest one sets the floor for time to first byte (TTFB),
and this has a knock-on effect on Web Vitals metrics. Even with requests
running in parallel via Promise.all(...), render time is at
least max(...requests).
A consideration that is often missed is measuring TTFB from where infrastructure resides, not the edge location of customers. A good global percentile is a falsehood that all customers have a performant experience, and the higher percentiles are accepted as transient issues when often a high percentile can ladder up to a cluster of customers within a certain location. In simple terms, a P90 average latency can be the same as a P50 recording for customers accessing from certain countries or continents.
On an AWS serverless stack, high percentiles are mostly driven by cold starts and customers furthest from the origin. This post explores one of them: a dynamic request from Australia to a European origin.
The problem
Not every service may run in every region. On a serverless stack
that's typically a choice of data sovereignty, co-location near your
customer base, and services dependencies. A service that depends on an
external SaaS product that isn't multi-region should be co-located in
the same region as that product for reliability. If the SaaS is hosted
in Europe, the service should run in a European or neighbouring region
relative to customers. An AWS Lambda in ap-southeast-2
calling a European region pays at minimum one ~265ms
round trip per call, and three on a cold connection.
Timeouts and retry behaviour are set by the vendor, so every millisecond
spent in transit comes out of a fixed budget. A short hop leaves more of
that budget for the dependency itself.
This is problematic where the data is both dynamic and specific to a user.
One round trip isn't the problem
Sydney to Europe and back is roughly ~265-300ms. That's one round trip (RTT).
One round trip describes a request on a connection that already exists. A cold connection has to be set up first:
- TCP handshake SYN out, SYN-ACK back
- TLS 1.3 handshake, ClientHello out, ServerHello and certificate back. TLS 1.2 needs two
- The request for data itself - 1 RTT.
That's 3 × min(265ms), ~800ms before the first useful byte.
Most of that is setup before any data is sent. Often, a slow request would immediately prompt engineers and teams to address their code which resides in the compute layer. This is sensible, however often modern services are fast, and diminishing returns set in quickly with frankly little improvement. Thinking outside the service is often more important. Who is calling this service, and where is the request coming from, is crucial to understand what a customer experiences whilst using a client.
A request enters AWS CloudFront at an edge location via the point of presence (PoP) closest to the customer, and that hop is short. The expensive one is CloudFront's own connection from that edge back to the origin, which could be across the globe.
Keeping connections warm
CloudFront pools its connections to a origin and reuses one if it's open. A warm connection skips TCP and TLS, so the request costs 1 RTT, not 3.
The obvious lever is the origin keep-alive timeout setting, which is how long CloudFront keeps an idle origin connection open. The default is 5 seconds, and the origin's own idle timeout caps it.
Will raising it solve the problem? Sure, it helps, but it won't produce a noticeable percentile drop, there are also strong considerations around stale connections which will cause downstream connection failures if this number is too high.
There isn't one connection pool. CloudFront consists of many PoPs, each holding its own connections to the origin. A connection only stays warm if another request arrives on it before the idle timer runs out, so what matters is how often requests reach each pool. Keep-alive doesn't change that.
For a route that sends one request every 2 seconds through a single pool with a 5 second timeout, the connection never goes cold. Spread across more pools, the chance of cold connections quickly increases.
So it's still a roll of the dice. Is there a way to fix the distribution itself? Yes, but it's an unusual one, and one I experimented with.
AWS CloudFront Origin Shield
Origin Shield adds one more CloudFront cache layer, in a single chosen region, between every edge location and the origin:
without: caller → edge location → origin
with: caller → edge location → Origin Shield → origin
It has two primary features:
- Caching. Edge locations around the world don't share a cache. With Origin Shield, a miss at any edge can be a hit at the shared layer, so the origin does less work.
- Request collapsing. When many edge locations ask for the same object at once, Origin Shield sends one request to the origin and shares the response.
Neither helps a dynamic request. The response is different for every customer, so there's nothing to cache or collapse, and AWS's guidance is that Origin Shield may not suit dynamic content that's just proxied to the origin.
The hypothesis
Both features depend on every origin-bound request, from every edge location, passing through the same place. If the requests converge, so do the connections. The edge locations stop holding their own connections to the origin. Origin Shield holds them, in one region.
So the hypothesis: if origin connections are pooled centrally and all of the traffic flows through them, they should stay warm. A request from Australia wouldn't need a TCP or TLS handshake with the origin at all.
It isn't literally one pool. Origin Shield is a fleet across at least three availability zones, so fewer, hotter pools is the accurate way to put it. And it sits in the same region as the origin, so when it does need a new connection, each round trip of the handshake takes a millisecond or two.
Using a warm connection would go from a sporadic gamble to the default way of working.
Results
Configuring the shield layer against a single region, Australian customers saw p75 TTFB drop by 300-380ms on public pages. Values are scaled and not exact:
| Page | p75 TTFB before | p75 TTFB after | Change |
|---|---|---|---|
| Homepage | 1.95s | 1.65s | -0.30s |
| Product details | 2.48s | 2.10s | -0.38s |
It isn't free. Origin Shield bills per request for dynamic content, which includes any GET with caching disabled. For most regions the cost is under $1 per million requests. Ingress regional traffic incurs no additional cost.
Origin Shield earned its place purely as somewhere for connections to converge, and AWS Solutions Architects were surprised, pleasantly of course, by the result. One AWS post on dynamic workloads does note in passing that Origin Shield "can also increase the connection reuse rate", but the main guidance presents it as another cache layer.
CloudFront's Server-Timing response header includes
cdn-upstream-connect. Zero means an existing origin
connection was reused, and anything else is the time spent on TCP and
TLS setup. With Origin Shield enabled, the measured reuse rate was
81%.