Edge Functions Explained: Compute Close to the User
Edge functions run application code on servers near the visitor instead of a single origin region, cutting latency for auth, redirects, and personalization.
An edge function is a small piece of application code that runs on a server physically close to the person making the request, rather than in a single centralized data center. Instead of every visitor’s request traveling to one origin region, the request is handled at the nearest point of presence — often just a few milliseconds away — and only falls back to the origin when it genuinely needs to.
How it differs from a traditional server
A conventional backend runs in one region (or a handful of regions you manage yourself). Every request, no matter where it originates, has to cross the network to reach that region. A user in Sydney hitting a server in Virginia pays for that round trip on every single request.
Edge platforms — Cloudflare’s developer platform, Vercel’s Edge Runtime, and similar offerings — instead deploy your function to dozens or hundreds of points of presence around the world. The platform routes each request to the nearest one automatically. You write one function; the platform handles placement and routing.
This is a different model from a CDN caching static assets. A CDN serves files that were already generated. An edge function runs code on each request — it can read headers, make decisions, and return dynamic output, all without a round trip to a distant origin.
What edge functions are good at
Edge functions shine at workloads that are latency-sensitive but computationally light:
- Authentication and authorization checks — verifying a session cookie or JWT before a request reaches your origin, so unauthorized requests never make the long trip.
- A/B testing and personalization — rewriting a response or routing a user to a variant based on a cookie, all before the page is assembled.
- Redirects and rewrites — handling URL redirects at the edge instead of paying a full origin round trip just to get a 301.
- Geolocation-based logic — serving region-specific content, currency, or legal notices based on where the request originated, without a database lookup.
- API request shaping — validating, rate-limiting, or transforming a request before it reaches a backend service.
They’re a poor fit for long-running computation, large in-memory state, or anything that needs a persistent connection to a single database instance — edge runtimes are deliberately constrained (often no native Node.js APIs, short execution limits, and no local filesystem) so that the same function can be replicated safely everywhere at once.
Edge functions vs serverless functions
| Serverless functions | Edge functions | |
|---|---|---|
| Location | One or few regions | Many points of presence globally |
| Cold start | Can be noticeable | Typically near-instant |
| Runtime | Full Node.js / language runtime | Restricted runtime (Web APIs, no filesystem) |
| Execution time | Minutes | Milliseconds to a few seconds |
| Best for | Heavier business logic, long jobs | Latency-critical, lightweight logic |
Both are variants of serverless computing: you don’t manage the underlying machines, and you pay for what runs. The difference is placement and constraints, not the underlying billing model.
A concrete example
Imagine a site that needs to redirect logged-out users away from a dashboard route. Handling this at the origin means every visitor — logged in or not — pays for a full trip to your backend just to get bounced. Handling it as an edge function means the redirect decision happens at the network edge closest to the visitor: the cookie is checked, and either the request is rejected immediately or passed through to the origin. Logged-out users get a fast redirect; logged-in users pay only the cost of the check, not a wasted origin round trip.
This same pattern extends naturally to APIs deployed as edge workers — see deploying a Hono API to Cloudflare Workers for a worked example of writing request-handling logic that runs at the edge.
Trade-offs to know before adopting
Edge functions are not a free performance upgrade. A few things to weigh:
- Runtime limitations. You typically can’t use native Node.js modules that touch the filesystem or spawn processes. Code has to be written against Web-standard APIs (
fetch,Request,Response) rather than Node-specific ones. - State is hard. Edge functions are, by design, stateless and ephemeral. If your logic needs a database, that database call still has to cross the network — the edge only speeds up the parts of the request that don’t need it.
- Debugging is different. Logs are distributed across every point of presence your function ran in, which makes tracing a single request’s path less straightforward than tailing logs on one server. Distributed tracing tools help close that gap.
- Not every request benefits. If a request always needs to hit a single-region database anyway, moving the surrounding logic to the edge saves less than it would for logic that can be fully resolved without leaving the edge.
The takeaway
Edge functions move lightweight, latency-sensitive logic — auth checks, redirects, personalization — out to servers near the user instead of a single distant origin. They complement, rather than replace, your main backend: heavy computation and stateful work still belong at the origin or in a traditional serverless function. Reach for the edge when the win is shaving round-trip time off decisions that don’t need your full backend to make.
Tagged
Keep reading
Takina · · 6 min read How to Break Up Long Tasks in JavaScript
Long tasks block the main thread for 50ms or more and make pages feel frozen. How to split them with yielding, scheduler.yield(), postTask, and workers.
The Lycoris Team · · 5 min read Head-of-Line Blocking Explained: HTTP/1.1, HTTP/2, QUIC
Head-of-line blocking is when one stalled item holds up everything queued behind it. How it affects HTTP/1.1, HTTP/2 over TCP, and how QUIC fixes it.
Chisato · · 5 min read What Is Tail Latency? p99 and Slow Requests, Explained
Tail latency is the response time of your slowest requests, measured as p99 or p999. Why averages hide it, what causes it, and how to reduce it.