Cloud regions and the edge
In cloud computing, your application runs in a provider's data centers in one or a few regions you choose, close to your database. Edge computing runs small functions in many locations near your users, usually on a CDN's network. The edge shortens the trip for work that needs only the request and nearby data, while work that reads and writes your database belongs in the region.
Edge computing is different from edge caching. A cache stores whole responses so repeated requests skip the origin. An edge function runs code on each request, so it can respond to requests a cache has never seen.
Regions, availability zones and the origin
A region is a geographic area where a cloud provider runs data centers, with names such as us-east-1 or europe-west1. Each region is split into availability zones: one or more data centers with separate power, cooling and networking, linked to each other by low-latency connections.
Running instances in two or more zones keeps an application up when one data center fails. Running in several regions also survives a regional outage, but it costs more and makes data harder to keep consistent, because every write has to cross long distances.
The origin is the server or service that holds the authoritative copy of your content and runs your application logic. CDNs and edge functions sit in front of it and handle what they can without contacting it.
Distance sets a floor on latency. Light in optical fiber covers about 200 kilometers per millisecond. So a request that crosses an ocean and comes back spends tens of milliseconds in transit before your code runs. A page that needs several round trips in sequence pays that cost each time.
What edge functions are good at
An edge function is a small piece of code deployed to many locations of an edge network and run on each matching request. Many platforms run JavaScript or WebAssembly in lightweight sandboxes, such as V8 isolates, instead of containers, so a function starts much faster than a container would.
Edge functions suit work that can finish using only the request and data already at the edge:
- Redirects, URL rewrites, and adding or removing response headers.
- Routing by country, language or device, using location data the platform attaches to the request.
- Checking the signature on a token, such as a JWT, before the request reaches the origin.
- Assigning visitors to an A/B test variant and choosing which cached page to serve.
- Blocking requests that match simple rules, such as known bad paths or missing headers.
- Small changes to a cached page, such as inserting a region name or a currency.
The limits of edge functions
Edge functions are more constrained than code running in a region. The exact limits vary by platform, but the same constraints come up on most of them.
- Execution time is short. CPU time per request is capped, often at milliseconds or a few seconds, so report generation, video processing and other long jobs do not fit.
- The APIs are limited. Many edge runtimes offer web-standard APIs such as fetch and Web Crypto instead of a full Node.js environment. Packages that need the filesystem or native modules will not run.
- Memory and code size are capped, which rules out large dependencies.
- Your data is far away. The function runs near the user, but your primary database still lives in one region. Every query crosses that distance, so a function that makes five queries in sequence can be slower at the edge than in the region.
- Edge storage is usually eventually consistent. Edge key-value stores copy data to each location with a delay, so a write may not be visible everywhere at once.
- Debugging is harder when the same code runs in many locations, and each platform has its own APIs, which makes moving between providers more work.
Deciding what belongs at the edge
Put a step at the edge when it finishes quickly using only the request and data already copied near the user. Keep it in a region, next to the database, when it needs primary data more than once or needs a transaction. Long-running work and code that depends on libraries the edge runtime lacks belong there too.
A typical split for an online store: the edge handles redirects, checks the session token, picks the A/B variant and serves cached product pages. Checkout, account changes and search run in the region, where each database query is a short local hop.
Some platforms can run the same function either at the edge or in a region near your database. You can then start in the region and move a step outward once it no longer needs the database.
Measure before and after a move. Time to first byte from several locations shows whether the edge removed distance or added round trips to the database.