Skip to content
Velloreth
Technology 4 min read

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

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

LimitTypical
CPU time10–50ms per request
Memory128MB
Response sizea few MB
Request sizea few MB
Subrequests50
Durationa 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:

FixHow
A replicated databaseread from a nearby replica
A key-value store at the edgecache the data
A regional databasekeep the data near the user
Move the work to the regionuse 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.

For the related topics, see serverless-explained and how-cdns-actually-work. For the broader context, docker-basics-guide and rest-api-rate-limiting.

Frequently asked questions

What is an edge function?
It is a small piece of code that runs in a network of locations close to the user, rather than in one region. It is used for a request-time decision such as an auth check, a redirect, a personalisation or a header change. The runtime is a restricted subset of a standard runtime, and the limits are tighter than a serverless function's.
How is an edge function different from a serverless function?
A serverless function runs in one region and has a longer cold start and a larger runtime. An edge function runs in many locations, starts in milliseconds, and has a smaller runtime and tighter limits. Use the edge for a latency-sensitive decision and a serverless function for a longer task.
What is a cold start?
The delay when a new instance is created to handle a request. A serverless function may take hundreds of milliseconds, and an edge function takes a few milliseconds because the isolate is small. The difference is the reason the edge is used for a request-time decision.
What can an edge function not do?
It cannot run a long CPU-bound task, open a raw TCP connection in most runtimes, use a native module, or hold state between requests. The limits are a CPU time budget, a memory limit and a restricted API surface. A task that needs any of those belongs in a serverless function or a server.
When should I use an edge function?
For a decision that must be made per request and is latency-sensitive: an auth check, a geo redirect, an A/B assignment, a header rewrite, a cache key, a bot check. It is also useful for a personalisation that would otherwise require a round trip to a region.
Is the edge always faster?
No. If the function must call a database in one region, the round trip from the edge to the region can be slower than running the whole thing in the region. The edge helps when the work is local to the request, and it hurts when the work is a call to a central resource.