Edge functions: where to use them, where not to
Edge computing sounds fast by default. It isn't. Here's how to think about where edge functions actually help.
Edge functions run close to the user: distributed across a CDN rather than in a single region. The pitch is lower latency. That's true, but it comes with trade-offs that aren't always obvious until you've hit them in production.
What edge is good at
Edge shines for work that's cheap to compute and dependent on the request itself. The canonical examples are:
- Geolocation-based redirects: send
/envisitors to/en-gbwithout a round-trip to a central server - A/B test cookie assignment: read or set a cookie before the page renders
- Auth header validation: check a JWT, redirect if it's missing, before serving anything
These tasks are stateless, fast, and benefit from running at the network edge, close to the browser, not close to your database.
// next.config middleware, runs at the edge
export function middleware(request: NextRequest) {
const country = request.geo?.country ?? "GB";
if (country !== "GB") {
return NextResponse.redirect(new URL("/international", request.url));
}
}
export const config = { matcher: "/" };What edge is bad at
The edge runtime is deliberately constrained. You don't have access to the full Node.js API. Database connections are limited: most ORMs don't work at the edge without specific adapters. Anything that needs a persistent connection or heavy computation belongs in a standard serverless function or a dedicated server.
If you're reaching for an edge function because "it's faster", stop and ask what's actually slow. Usually the answer is the database query, not the compute. Running your query handler at the edge doesn't help when the database is still in a single region.
The mental model that helps
Think of edge as middleware, not as your application logic. It's the layer between the CDN and your actual server, good for routing, redirecting, and lightweight header work. The real work still happens server-side.
Edge is a tool with a specific job. Used for that job, it improves response times noticeably. Used for everything, it adds complexity without the payoff.
In Next.js, the default is server functions in a single region. Move something to the edge only once you have a concrete reason: a measurable latency problem, a geolocation requirement, something that genuinely fits the model.
Got a project in mind?
I build fast, production-grade sites for freelance clients. Let’s talk.