How We Cut API Latency by 60% With Edge Functions
Eighteen months ago, every request to our API travelled to a single region in Virginia. For customers in Frankfurt or Singapore, that meant 300–400ms before our code even started running. This is the story of how we moved the hot path to the edge — and what we would do differently.
Where the time was going
We started by tracing a week of production traffic. Three things stood out:
- Network round trips accounted for 58% of P95 latency outside North America.
- Auth checks hit the primary database on every request, even for cached reads.
- Cold starts on our container fleet added 900ms to roughly 1 in 200 requests.
Moving the hot path
We split each endpoint into two layers: a thin edge function that validates tokens and serves cached reads, and the origin service that handles writes. Token validation moved from a database lookup to a signed JWT check, which runs in under a millisecond.
export default async function handler(request) {
const session = await verifyToken(request.headers.get('authorization'));
if (!session) return new Response('Unauthorized', { status: 401 });
const cached = await cache.match(request);
return cached ?? fetch(originFor(request), request);
}
Writes still go to the origin, but they now arrive already authenticated, which let us drop a full query from every mutation.
The fastest request is the one that never reaches your database. The second fastest is the one that reaches it exactly once.
The results
After a six-week rollout behind a feature flag, P95 latency fell from 410ms to 162ms globally, and from 380ms to 96ms in Europe. Database load dropped by a third, which postponed a costly scaling project by a year.
What we would do differently
We underestimated cache invalidation. Our first version served stale project settings for up to a minute after an edit. Today every write publishes an invalidation event, and the edge layer honours it within two seconds. If you are planning a similar migration, design invalidation first — not last.