The Edge Computing Engineer moves application logic from a centralised server to edge nodes distributed globally. The key benefit is latency: code runs within 10–50ms of the user rather than 100–200ms from a distant data centre. Edge functions excel at request transformation, auth checks, personalisation, and A/B testing - tasks where you need the result before serving any content.
Cloudflare Worker
// wrangler.toml
name = "my-edge-api"
main = "src/index.ts"
compatibility_date = "2026-01-01"
[[kv_namespaces]]
binding = "CACHE"
id = "your-kv-namespace-id"
// src/index.ts
export interface Env {
CACHE: KVNamespace;
API_SECRET: string; // Set in wrangler.toml [vars] or Cloudflare dashboard secrets
}
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const url = new URL(request.url);
// Route: /api/user/profile
if (url.pathname.startsWith('/api/')) {
return handleApi(request, env, ctx);
}
// Middleware: add security headers to all responses
const response = await fetch(request);
const newResponse = new Response(response.body, response);
newResponse.headers.set('X-Content-Type-Options', 'nosniff');
newResponse.headers.set('X-Frame-Options', 'DENY');
return newResponse;
},
};
async function handleApi(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const cacheKey = request.url;
// Check KV cache first (<1ms read)
const cached = await env.CACHE.get(cacheKey);
if (cached) {
return new Response(cached, { headers: { 'Content-Type': 'application/json', 'X-Cache': 'HIT' } });
}
// Fetch from origin
const data = await fetch('https://api.internal.example.com' + new URL(request.url).pathname).then(r => r.json());
const json = JSON.stringify(data);
// Cache in KV with TTL
ctx.waitUntil(env.CACHE.put(cacheKey, json, { expirationTtl: 60 }));
return new Response(json, { headers: { 'Content-Type': 'application/json', 'X-Cache': 'MISS' } });
}
Durable Objects (Stateful Edge)
// Durable Objects: single-threaded state at the edge
// Use for: rate limiting, session state, WebSocket coordination
export class RateLimiter implements DurableObject {
private state: DurableObjectState;
private requests = 0;
private windowStart = Date.now();
constructor(state: DurableObjectState) {
this.state = state;
}
async fetch(request: Request): Promise<Response> {
const now = Date.now();
if (now - this.windowStart > 60_000) { // Reset each minute
this.requests = 0;
this.windowStart = now;
}
this.requests++;
if (this.requests > 100) { // 100 requests per minute per user
return new Response('Rate limit exceeded', { status: 429 });
}
return new Response(JSON.stringify({ remaining: 100 - this.requests }));
}
}
// Using Durable Object from a Worker
const id = env.RATE_LIMITER.idFromName(clientIp);
const stub = env.RATE_LIMITER.get(id);
const res = await stub.fetch(request);
When to Use Edge vs Origin
- Use edge: Auth checks, redirects, A/B testing, geolocation-based routing, cache serving, header manipulation, bot detection
- Stay at origin: Database reads/writes, complex business logic, file processing, anything requiring a persistent runtime >50ms CPU time
- Edge workers have a 10ms (free) or 50ms (paid) CPU time limit - they are for fast transformation, not heavy computation
- Global state at edge requires Durable Objects - KV is eventually consistent (good for caches), not for user sessions requiring consistency