Steven's Knowledge

Edge Functions

Code running at the CDN edge - Cloudflare Workers, Vercel Edge, Deno Deploy, Fastly Compute@Edge, and AWS Lambda@Edge

Edge Functions

Edge functions run at the CDN's POPs, geographically near the user — typically with cold-start measured in milliseconds and execution measured in microseconds. They sit inside the CDN itself, intercepting requests before they ever reach your origin.

Why Edge?

Without edge functionsWith edge functions
Region-based serverless (Lambda) — 100-500 ms cold start< 5 ms cold start
100+ ms RTT to a single-region origin< 50 ms for the user nearest the POP
Static asset rewrites at build timeDynamic per-request rewrites at the edge
Auth checked at originAuth at the edge — origin never sees unauthorized traffic
A/B test logic in client JSDecide variant at the edge, serve cached HTML accordingly
Per-user personalization causes cache missesInject personalization while serving cached shell

Edge functions are also vastly cheaper per invocation than container-based serverless. Pricing is typically per million requests, often with generous free tiers.

The Players

PlatformRuntimeNotes
Cloudflare WorkersV8 isolates (JS / WASM)The mature standard; KV / Durable Objects / D1 / R2 / Queues
Vercel Edge FunctionsBuilt on Cloudflare Workers infrastructureTight Next.js integration; auto-deploy
Deno DeployV8 isolates running DenoTypeScript-first; standard Web APIs
Netlify Edge FunctionsDeno Deploy under the hoodTight integration with Netlify sites
Fastly Compute@EdgeWASM (Rust, AssemblyScript, Go, JS)Spectacularly fast; harder dev experience
AWS Lambda@EdgeNode.js / Python"Edge" but heavier (~100 ms cold start); CloudFront integration
AWS CloudFront FunctionsJS only, no I/OTiny scope; runs in CloudFront; sub-ms execution
Akamai EdgeWorkersJS isolatesAkamai's edge compute

Cloudflare Workers and Vercel Edge Functions are the two most common starting points. Fastly Compute@Edge is heavier-duty (WASM) but faster. Deno Deploy is the strong Web-standard alternative.

The Programming Model

Edge functions are HTTP-shaped: a request comes in, you return a response. The platforms standardize on Web standard APIsRequest, Response, fetch — not Node.js-specific APIs.

// Same code runs on Cloudflare Workers, Deno Deploy, Vercel Edge,
// Netlify Edge Functions, and the browser
export default {
  async fetch(request, env, ctx) {
    const url = new URL(request.url);
    if (url.pathname === '/health') {
      return new Response('ok');
    }
    return fetch(request);   // pass through to origin
  },
};

The catch: no fs, no crypto.createHash (use Web Crypto), no Express middleware, no long-running processes. You're closer to the browser's JS environment than to Node.js.

When Edge Wins

Use caseWhy edge
Auth check before hitting originReject 401 in 5 ms; protect origin from bot traffic
A/B testing at the edgeBucket users without origin round-trip
Geographic redirects"EU users → /eu" — at the edge, instantly
Header rewrites / response transformsNo code change at origin
Personalization while cachingCached shell + edge-injected personalized fragments
Image optimizationResize / convert on the fly, cache the result
Rate limiting per IPReject abuse at the edge before it costs you anything
Authentication middleware for static sitesStatic site + edge auth = dynamic app feel
Cookie-based routingSend canary users to a different origin

When Edge Loses

Use caseWhy not edge
Heavy database queriesEdge functions are network-far from your DB
Long-running computationStrict CPU and wall-clock limits
Large dependenciesBundle size limits (typically 1-25 MB)
Stateful sessions in-memoryIsolates are ephemeral; use KV / DO / external store
Anything needing fsNo filesystem
Complex business logicBetter at your application server

A common architecture: edge handles the request shape (routing, auth, simple personalization, transforms), your origin handles business logic and data access.

Learning Path

Edge vs Other Compute Platforms

Edge functionsContainer serverless (Lambda)Containers (K8s)VMs
Cold start< 5 ms100-1000 ms0 (always on)0
Network locationAt the CDN POPSingle regionWhere your cluster isWhere your cluster is
LanguagesJS, WASMMany (Node, Python, Go, etc)AnythingAnything
StatefulNo (use KV / DO)NoYes (StatefulSets)Yes
Max execution10s-30s15 minUnlimitedUnlimited
Bundle size1-25 MB250 MBImage-sizedDisk
Best forRequest-shape workStateless compute on demandFull appsLong-running workloads

They compose. An app commonly has edge functions in front of (or in tandem with) a container backend in front of a database.

Edge functions are an evolution of CDN features. The CDN was always doing some request manipulation (cache rules, redirects, header rewrites). Edge functions are "give that capability a programming model." If you're using a modern CDN, you already have an edge runtime; the question is whether to use it.

On this page