Serverless Edge Architecture
Deploy compute at the network edge for ultra-low latency. Covers edge functions, regional data replication, edge caching strategies, and the patterns that bring computation within 50ms of every user on Earth.
Traditional cloud architecture serves all users from one or two regions. A user in Tokyo hitting a server in Virginia experiences 150-200ms of network latency — before the server even starts processing. Edge computing moves code execution to Points of Presence (PoPs) around the world, reducing latency to 10-50ms for the majority of global users.
Edge Architecture Layers
Edge Computing Spectrum:
CDN Edge (Cache only):
What: Static content cached at 200+ PoPs
Latency: 5-20ms to nearest PoP
Use: Images, CSS, JS, HTML
Examples: Cloudflare CDN, CloudFront, Fastly
Limitation: Cannot execute custom logic
Edge Functions (Code at the edge):
What: Lightweight functions at 200+ PoPs
Latency: 10-50ms including execution
Use: Auth, routing, personalization, A/B testing
Examples: Cloudflare Workers, Vercel Edge, Lambda@Edge
Limitation: Limited runtime (CPU time, memory)
Regional Compute (Near-user):
What: Full compute in 20-40 regions
Latency: 20-100ms to nearest region
Use: API servers, application logic
Examples: Fly.io, AWS regions, Google Cloud regions
Limitation: Data consistency across regions
Origin (Centralized):
What: Primary database and application
Latency: 50-200ms from some users
Use: Writes, transactions, batch processing
Examples: Your primary cloud region
Limitation: Far from some users
Edge Function Patterns
// Cloudflare Worker: Edge-level A/B testing
export default {
async fetch(request) {
const url = new URL(request.url);
// Determine user bucket at the edge
const userId = request.headers.get('cookie')?.match(/user_id=(\w+)/)?.[1];
const bucket = userId
? hashToPercent(userId)
: Math.random() * 100;
// Route to variant
if (url.pathname === '/pricing') {
const variant = bucket < 50 ? 'control' : 'experiment';
// Rewrite to variant page
const variantUrl = new URL(request.url);
variantUrl.pathname = `/pricing/${variant}`;
const response = await fetch(variantUrl);
const modifiedResponse = new Response(response.body, response);
// Set variant header for analytics
modifiedResponse.headers.set('X-Experiment-Variant', variant);
return modifiedResponse;
}
return fetch(request);
}
};
// Edge-level geolocation and personalization
export default {
async fetch(request) {
const country = request.cf?.country || 'US';
const city = request.cf?.city || 'Unknown';
// Currency and pricing at the edge
const currency = COUNTRY_CURRENCY_MAP[country] || 'USD';
const priceMultiplier = PPP_MULTIPLIERS[country] || 1.0;
// Modify response with localized data
const response = await fetch(request);
const html = await response.text();
const localized = html
.replace('{{currency}}', currency)
.replace('{{price_multiplier}}', priceMultiplier);
return new Response(localized, {
headers: {
...response.headers,
'X-Edge-Location': `${city}, ${country}`,
},
});
}
};
Anti-Patterns
| Anti-Pattern | Consequence | Fix |
|---|---|---|
| Put everything at the edge | Edge has limited CPU, memory, storage | Edge for routing/personalization, origin for heavy compute |
| Edge writes to central database | High latency negates edge benefit | Read from edge cache, write to nearest region |
| No edge caching strategy | Edge functions fetch from origin every time | Cache aggressively at edge, invalidate on change |
| Ignore cold start at edge | First request to a PoP is slow | Prewarming, keep-alive, or choose zero-cold-start platform |
| Same logic at edge and origin | Duplicated code, inconsistency | Edge for lightweight transforms, origin for business logic |
Edge computing is about moving the right work to the right location. Static content belongs at CDN edges. Personalization and routing belong in edge functions. Business logic and data transactions belong at the origin. The art is knowing which tier each operation belongs to.