Edge Functions: Running Code Close to the User
Edge functions run in many locations near the user. Learn the model, the limits, the cold start and when a serverless function is better.
By Velloreth
The model
An edge function runs in many locations, close to the user.
User in Kathmandu -> edge in Mumbai -> the response
User in London -> edge in London -> the response
The code is deployed once and replicated to every location.
The request is handled by the nearest location.
The runtime
A restricted subset of a standard runtime.
What is available:
- The standard web APIs:
fetch,Request,Response,Headers - A subset of the crypto API
- A key-value store, in some platforms
- Environment variables
What is not:
- A native module
- A raw TCP socket, in most runtimes
- The filesystem
- A long-lived process
- State between requests
The consequence: a library that depends on a Node built-in may not run.
The cold start
A serverless function may take hundreds of milliseconds to start.
An edge function takes a few milliseconds, because the isolate is small.
Why it matters: a request-time decision must be fast, and a cold start of 300ms is a visible delay.
The isolate is a lightweight sandbox, not a container, which is why it starts quickly.
The limits
| Limit | Typical |
|---|---|
| CPU time | 10–50ms per request |
| Memory | 128MB |
| Response size | a few MB |
| Request size | a few MB |
| Subrequests | 50 |
| Duration | a few seconds of wall time |
The CPU limit is the one that bites. A loop over a large array exceeds it.
The wall time can be longer than the CPU time, because a fetch wait does not consume CPU.
What to use it for
An auth check. Verify a token and reject the request before it reaches the origin.
A geo redirect. Send a user to the right locale.
An A/B assignment. Pick a variant and set a cookie.
A header rewrite. Add a security header or a cache header.
A cache key. Normalise the URL so the cache hit rate improves.
A bot check. Reject a request that fails a simple test.
A personalisation. Read a cookie and render a variant.
What not to use it for
A long CPU-bound task. The CPU limit is exceeded.
A database query in one region. The round trip can be slower than running in the region.
A file upload. The size limit and the streaming model.
A background job. There is no long-lived process.
A native dependency. The runtime does not support it.
A stateful session. There is no state between requests.
The database problem
The edge is close to the user and the database is in one region.
User in Kathmandu -> edge in Mumbai -> database in Virginia
The round trip from Mumbai to Virginia can be 200ms, which is slower than running the whole request in Virginia.
The fixes:
| Fix | How |
|---|---|
| A replicated database | read from a nearby replica |
| A key-value store at the edge | cache the data |
| A regional database | keep the data near the user |
| Move the work to the region | use a serverless function |
The rule: the edge helps when the work is local to the request, and it hurts when the work is a call to a central resource.
The platform
Cloudflare Workers, Deno Deploy, Vercel Edge Functions, Netlify Edge Functions, AWS Lambda@Edge.
The differences: the runtime, the limits, the key-value store, and the deployment model.
The common ground: a web-standard API, a fast start, and a restricted runtime.
Common mistakes
A CPU-heavy loop. The limit is exceeded.
A database call to a distant region. Slower than the origin.
A Node built-in. Not available.
A large response. The size limit.
A background job. No long-lived process.
State between requests. Not available.
A secret in the code. Use an environment variable.
No local test. The runtime differs from the local one.
Assuming the edge is always faster. It depends on the work.
A quotable summary
An edge function runs in many locations close to the user, with a near-zero cold start because the isolate is small, and a restricted runtime with tight CPU, memory and API limits. Use it for a request-time decision such as an auth check, a redirect or a header rewrite, and not for a long CPU-bound task or a call to a database in a distant region, because the round trip can be slower than running the whole request in the region.
Related reading
For the related topics, see serverless-explained and how-cdns-actually-work. For the broader context, docker-basics-guide and rest-api-rate-limiting.