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 functions | With 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 time | Dynamic per-request rewrites at the edge |
| Auth checked at origin | Auth at the edge — origin never sees unauthorized traffic |
| A/B test logic in client JS | Decide variant at the edge, serve cached HTML accordingly |
| Per-user personalization causes cache misses | Inject 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
| Platform | Runtime | Notes |
|---|---|---|
| Cloudflare Workers | V8 isolates (JS / WASM) | The mature standard; KV / Durable Objects / D1 / R2 / Queues |
| Vercel Edge Functions | Built on Cloudflare Workers infrastructure | Tight Next.js integration; auto-deploy |
| Deno Deploy | V8 isolates running Deno | TypeScript-first; standard Web APIs |
| Netlify Edge Functions | Deno Deploy under the hood | Tight integration with Netlify sites |
| Fastly Compute@Edge | WASM (Rust, AssemblyScript, Go, JS) | Spectacularly fast; harder dev experience |
| AWS Lambda@Edge | Node.js / Python | "Edge" but heavier (~100 ms cold start); CloudFront integration |
| AWS CloudFront Functions | JS only, no I/O | Tiny scope; runs in CloudFront; sub-ms execution |
| Akamai EdgeWorkers | JS isolates | Akamai'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 APIs — Request, 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 case | Why edge |
|---|---|
| Auth check before hitting origin | Reject 401 in 5 ms; protect origin from bot traffic |
| A/B testing at the edge | Bucket users without origin round-trip |
| Geographic redirects | "EU users → /eu" — at the edge, instantly |
| Header rewrites / response transforms | No code change at origin |
| Personalization while caching | Cached shell + edge-injected personalized fragments |
| Image optimization | Resize / convert on the fly, cache the result |
| Rate limiting per IP | Reject abuse at the edge before it costs you anything |
| Authentication middleware for static sites | Static site + edge auth = dynamic app feel |
| Cookie-based routing | Send canary users to a different origin |
When Edge Loses
| Use case | Why not edge |
|---|---|
| Heavy database queries | Edge functions are network-far from your DB |
| Long-running computation | Strict CPU and wall-clock limits |
| Large dependencies | Bundle size limits (typically 1-25 MB) |
| Stateful sessions in-memory | Isolates are ephemeral; use KV / DO / external store |
Anything needing fs | No filesystem |
| Complex business logic | Better 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
1. Getting Started
Deploy your first Cloudflare Worker, write a request handler, deploy it globally in 30 seconds
2. Patterns
Auth middleware, A/B testing, geo-routing, edge-side rendering, KV storage
3. Best Practices
Limits, cold starts, observability, secret management, dev workflow, cost
Edge vs Other Compute Platforms
| Edge functions | Container serverless (Lambda) | Containers (K8s) | VMs | |
|---|---|---|---|---|
| Cold start | < 5 ms | 100-1000 ms | 0 (always on) | 0 |
| Network location | At the CDN POP | Single region | Where your cluster is | Where your cluster is |
| Languages | JS, WASM | Many (Node, Python, Go, etc) | Anything | Anything |
| Stateful | No (use KV / DO) | No | Yes (StatefulSets) | Yes |
| Max execution | 10s-30s | 15 min | Unlimited | Unlimited |
| Bundle size | 1-25 MB | 250 MB | Image-sized | Disk |
| Best for | Request-shape work | Stateless compute on demand | Full apps | Long-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.