Edge Computing: Running Code Close to Users
Learn what running code at the edge is good for, where it adds delay instead, and how to decide which parts of an app belong there.
Key takeaways
- Edge functions run at distributed locations and can respond without a long round trip to a central server.
- Redirects, header changes, access checks and light personalization are typical good uses.
- Where your data lives usually decides whether moving code to the edge makes a page faster or slower.
- Edge runtimes impose limits on execution time, memory and available libraries that differ by provider.
- Distributed storage often trades immediate consistency for fast local reads.
Edge computing means running your code at locations near users or devices instead of only in a central data center. On the web, that usually means small functions executing at a content delivery network's sites, where they can answer a request, change it or decide where it goes before it ever travels to your main servers.
It's a strong fit for quick decisions that need little data. It's a poor fit for work that has to consult a faraway database several times, because the code ends up close to the user and far from what it needs.
What "the edge" refers to
The term covers several places. For websites, it's the provider's points of presence, described in edge locations and points of presence. In mobile networks, it can mean computing placed close to the radio network, often called multi-access edge computing. In factories, shops and vehicles, it means a computer on site that processes sensor or camera data locally so that it keeps working with a poor connection and sends only results upstream.
All share one idea: move processing toward where data is produced or consumed. The rest of this article concentrates on the web.
How edge functions differ from a server
With a traditional server, you run a process on a machine in a region you chose. With an edge platform, you upload code, and the provider runs it at many locations as requests arrive. You don't manage the machines. It's a distributed relative of the model in serverless hosting.
Many platforms run each function in a lightweight isolated sandbox, often built on the same kind of JavaScript engine that browsers use, or on WebAssembly. These start very quickly. Others use containers or small virtual machines, with different startup behavior and fewer restrictions.
A typical handler looks something like this, using the web-standard Request and Response objects that several platforms support:
export default {
async fetch(request) {
const url = new URL(request.url);
if (url.pathname === '/old-pricing') {
return Response.redirect(new URL('/pricing', url), 301);
}
return fetch(request);
}
}; The exact entry point and deployment steps vary, so follow your provider's documentation.
Why distance still matters
Every exchange between a visitor and a server costs at least one round trip, and the speed of light in fiber puts a floor under it. Real network paths add detours, queues and processing. A response generated nearby avoids most of that, which people notice on pages that depend on several requests in sequence.
Caching already solves this for content that is the same for everyone. Edge code extends the benefit to cases where a small decision stands between the request and the cached answer. Without it, a site might send every visitor to a central server just to decide which of two stored pages to show. With it, the decision and the page both come from nearby.
Be realistic about the size of the gain. If most of your visitors are near your server, or a page spends its time in the browser running scripts, moving a function to the edge changes little. Measure where the delay is first, as described in why websites load slowly.
Jobs that suit the edge
- Redirects and URL rewrites. Answer immediately, with no origin involved.
- Request and response headers. Add security headers or normalize inputs.
- Access checks. Verify a signed token and reject bad requests early. The signature can be checked locally with a public key; see JSON Web Tokens. The application should still enforce its own authorization.
- Experiments and rollouts. Assign a visitor to a variant and serve the matching cached page.
- Light localization. Choose a language or currency from request details, while letting people override the guess.
- Assembling pages. Combine a cached shell with a small fresh fragment.
- Filtering and rate limiting. Turn away unwanted traffic before it costs you anything, alongside a web application firewall.
A worked example: protecting downloads
Say you sell digital files and want only paying customers to download them, while still serving the large files from cache. Without edge code, every download passes through your application, which checks the account and then streams the file.
With edge code, your application checks the account once and issues a link containing the file name, an expiry time and a signature created with a secret key. The edge function verifies the signature and expiry on each request. If both are valid, it serves the cached file. If not, it returns 403 Forbidden.
The function needs no database lookup, so it works equally fast everywhere. The weaknesses are worth stating: anyone holding the link can use it until it expires, so keep lifetimes short, and revoking one early requires extra state that the edge has to look up. Use the platform's cryptography functions instead of writing your own, and make sure the cache key doesn't treat every signature as a different file.
The data gravity problem
Say your database is in one region and a visitor is on another continent. A central application makes the long trip once with the request, then runs five queries over a fast local network. Move the application to the edge near the visitor, and each of those five queries now makes the long trip.
Where the data is matters more than where the code is. Hypothetical example: the numbers are made up to show the method, not a quote or an average.
Ways around it include caching the data at the edge, using read replicas near users, batching queries into one call, or leaving that endpoint in the region next to the database. Some platforms can automatically place a function nearer its data.
State and consistency
Edge platforms offer various kinds of storage. A globally replicated key-value store gives quick local reads, but a write may take a while to appear everywhere. That's called eventual consistency. It's fine for feature flags or a redirect table, and wrong for the count of remaining concert tickets.
Where you need strict ordering, some platforms offer objects or databases that have a single authoritative location per item. They're consistent, but requests from far away pay the distance.
Don't rely on memory inside a function instance. Instances come and go, and different requests from one user may land in different places.
Limits to expect
| Area | Typical constraint |
|---|---|
| Processor time | A short budget per request |
| Memory | Smaller than a normal server |
| Libraries | Some server APIs and native modules unavailable |
| Code size | A cap on the deployed bundle |
| Debugging | Logs scattered across locations |
| Portability | Storage and configuration differ between providers |
The numbers change and depend on your plan, so check current documentation before designing around them. Pricing is often per request and per unit of compute, which makes it worth estimating before you route every static file through a function.
Deciding what to move
Start with one narrow task, such as redirects. Measure first-byte time from several places before and after.
Developing and observing edge code
Most providers offer a local emulator, which is handy but never identical to production. Keep logic in plain functions that you can unit test without the platform, and keep the handler thin. Test with missing headers, unusual paths, oversized inputs and a failing origin.
Observability needs forethought. A request may run in one of many locations, so include a request identifier and pass it on to your origin, letting you join the two sets of logs. Record which location handled the request, the time spent in your function and the time spent waiting on others. Sample if the volume is large, and avoid logging personal data.
Set a timeout on every outbound call. A function that waits indefinitely on a slow service ties up the request and makes the edge slower than having no edge logic at all.
What can go wrong
- A bug everywhere at once. A bad deployment reaches all locations quickly. Use staged rollouts and keep a fast rollback.
- Failing open or closed by accident. Decide what happens if the function errors: pass the request on, or refuse it. Security checks and cosmetic features deserve different answers.
- Cache confusion. Personalized output stored under a shared key leaks between users. Review the cache key whenever logic changes responses.
- Secrets in code. Use the platform's secret storage, as described in environment variables and secrets.
- Data location rules. Processing personal data in many countries can raise legal questions. Check your obligations and the provider's regional controls.
- Lock-in. Keep core business logic separate from provider-specific glue so you can move it.
Common questions
Is edge computing replacing central data centers?
No. Databases, large computations and systems of record generally stay central or regional. The edge takes on the parts that benefit from proximity, in front of the rest.
Do I need it for a simple website?
Usually not. A static site behind a content delivery network already gets the main benefit of the edge. Add functions when you have a specific need, such as logic that has to run before a cached page is served.