Edge-First

Edge computing · Funciones en el edge

Escrito por Darjus Kazakevic · revisado el

Answer each request near the user, and send home only what must be central.

Edge-first means running code at a CDN's points of presence, the hundreds of data centres that already cache your static files, and not only in one central region. Cloudflare Workers, Fastly Compute, Vercel and Netlify edge functions, and Lambda@Edge work this way. A request from Sydney is handled in Sydney: checked, routed, personalised and often answered without crossing an ocean.

The win is latency. A round trip from Sydney to the US East Coast takes about 200 ms, and a page that needs several round trips pays it each time. The catch is data. The edge holds copies, not the truth, so reads there can be stale, and every write still travels to the origin. Edge-first works when you decide, per request, what can be answered locally and what must go home.

En esencia — Put stateless, read-mostly work at edge locations near users and keep writes and strongly consistent data at one origin; the edge is only fast when the data it needs is there too.

Cuándo recurrir a él

Recurre a él cuando

  • Users are spread across continents and round trips to one region dominate latency
  • Much of each request is stateless: token checks, redirects, variants, headers
  • Read-mostly data can be cached or replicated: catalogues, content, config, flags

Reconsidéralo cuando

  • Most users are in one region near your servers, so the gain is a few milliseconds
  • Requests are mostly writes or need the latest data, so they all go to the origin anyway
  • Handlers need heavy CPU, long runs or native libraries the edge runtime does not allow

How it works

Edge functions
Small request handlers deployed to every point of presence. They see a request before the cache and the origin do. Many run in lightweight isolates, not containers, so they start in milliseconds but offer a restricted runtime.
Edge vs origin
The edge suits work that needs no central truth: checking a signed token, picking a variant, redirecting by country, serving cached or replicated reads. The origin keeps writes, transactions and anything that must see the latest data.
Data locality
Code near the user helps only if its data is near too. Cache origin responses at the edge, replicate read-mostly data such as config, flags or a catalogue to an edge store, or put read replicas in a few regions.
Cold starts and runtime limits
Isolate-based runtimes start in a few milliseconds; container-based ones can take hundreds. All of them cap CPU time, memory and code size per request, and some restrict native modules or raw database connections.
The consistency cost
Each location reads its own copy of edge data, which is updated some time after the origin changes, from seconds to about a minute. A user who writes and then reads at the edge before the copy catches up sees old data.

Reglas de diseño

Keep the edge stateless and read-only
Edge code verifies, routes, transforms and reads copies. Every write goes to the origin, where one database decides.
Move the data with the code, or leave the code at home
Before moving a handler, list what it reads. If that data cannot be cached or replicated near users, keep the handler at the origin. One round trip from the client to the origin beats several from the edge.
Check tokens at the edge, authorise at the origin
A token's signature and expiry need only a public key, so they fit the edge. Decisions that depend on current data, such as a revoked session or a changed role, still belong at the origin.
Expect every location to be slightly different
Deploys, cache contents and replicated data reach locations at different times. Version your data and keep old and new edge code compatible during a rollout.

Errores habituales

  • Moving a handler to the edge while it still makes three queries in a row to a database in one region, so it gets slower.
  • Opening a new database connection per edge request, so a traffic spike uses up the database's connections.
  • Personalising a response and caching it as public, so one user's page is served to others.
  • Keeping counters or other writes in an eventually consistent edge store, and losing updates when two locations write at once.