How Edge Computing Is Changing API Architecture
How Edge Computing Is Changing API Architecture
Edge computing moves your API logic closer to users — from a single data center to 300+ locations worldwide. The result: sub-50ms response times everywhere. But it changes how you think about data, state, and architecture.
What Edge Computing Means for APIs
Traditional API Architecture
User (Tokyo) → CDN (static) → Origin server (US-East) → Database (US-East)
Latency: ~200ms
Edge API Architecture
User (Tokyo) → Edge function (Tokyo) → Edge database (Tokyo) → Response
Latency: ~20ms
The API logic runs at the edge location closest to the user. No round-trip to a central data center.
The Edge Platform Landscape
| Platform | Runtime | Locations | Cold Start |
|---|---|---|---|
| Cloudflare Workers | V8 Isolates | 300+ | <5ms |
| Vercel Edge Functions | V8 Isolates | 30+ | <25ms |
| Deno Deploy | Deno/V8 | 35+ | <10ms |
| Fastly Compute | Wasm | 90+ | <5ms |
| AWS Lambda@Edge | Node.js | 30+ | ~100ms |
| Netlify Edge Functions | Deno | 30+ | <25ms |
Why V8 Isolates Win
Traditional serverless (Lambda) uses containers. Each container takes 100-500ms to cold start. V8 isolates share the same process, creating new execution contexts in <5ms.
Container (Lambda): Pull image → Start runtime → Load code → Execute
[~200ms cold start]
V8 Isolate (Workers): Create isolate → Execute
[~5ms cold start]
Edge API Patterns
1. API Gateway at the Edge
Route, authenticate, and rate-limit at the edge before hitting your origin:
// Cloudflare Worker as API gateway
export default {
async fetch(request: Request) {
const url = new URL(request.url);
// Auth check at edge (fast)
const apiKey = request.headers.get('Authorization')?.replace('Bearer ', '');
if (!apiKey || !await validateKey(apiKey)) {
return new Response('Unauthorized', { status: 401 });
}
// Rate limit at edge
const { success } = await env.RATE_LIMITER.limit({ key: apiKey });
if (!success) {
return new Response('Rate limited', { status: 429 });
}
// Route to appropriate origin
if (url.pathname.startsWith('/api/v1')) {
return fetch(`https://api-origin.example.com${url.pathname}`, request);
}
return new Response('Not found', { status: 404 });
},
};
Benefits: Authentication and rate limiting happen in <5ms at the edge, before the request ever reaches your origin server.
2. Full API at the Edge
For simple APIs, run the entire stack at the edge:
// Full CRUD API on Cloudflare Workers + D1
export default {
async fetch(request: Request, env: Env) {
const url = new URL(request.url);
if (url.pathname === '/api/products' && request.method === 'GET') {
const { results } = await env.DB.prepare(
'SELECT * FROM products WHERE active = 1 ORDER BY created_at DESC LIMIT 50'
).all();
return Response.json(results);
}
if (url.pathname === '/api/products' && request.method === 'POST') {
const body = await request.json();
const result = await env.DB.prepare(
'INSERT INTO products (name, price, description) VALUES (?, ?, ?)'
).bind(body.name, body.price, body.description).run();
return Response.json({ id: result.meta.last_row_id }, { status: 201 });
}
return new Response('Not found', { status: 404 });
},
};
3. Edge Caching Layer
Cache API responses at the edge with intelligent invalidation:
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
const cacheKey = new Request(request.url, request);
const cache = caches.default;
// Check edge cache
let response = await cache.match(cacheKey);
if (response) {
return response; // Cache hit — ~1ms
}
// Cache miss — fetch from origin
response = await fetch(request);
response = new Response(response.body, response);
response.headers.set('Cache-Control', 'public, max-age=60');
// Store in edge cache (async, doesn't block response)
ctx.waitUntil(cache.put(cacheKey, response.clone()));
return response;
},
};
4. Edge Data Patterns
| Pattern | How | Best For |
|---|---|---|
| KV Store | Cloudflare KV, Vercel KV | Config, feature flags, cached data |
| Edge Database | D1, Turso, Neon | Full SQL at the edge |
| Durable Objects | Cloudflare Durable Objects | Stateful edge logic, coordination |
| Edge Cache | Cache API | HTTP response caching |
| Read replica | Origin DB → edge replicas | Read-heavy, write-rare |
5. Geo-Routed APIs
Return different results based on user location:
export default {
async fetch(request: Request) {
const country = request.headers.get('cf-ipcountry') || 'US';
const city = request.cf?.city || 'Unknown';
// Pricing by region
const pricing = getPricingForRegion(country);
// Content by region
const content = await getLocalizedContent(country);
// Compliance by region
const features = getAvailableFeatures(country);
return Response.json({
pricing,
content,
features,
meta: { country, city, edge_location: request.cf?.colo },
});
},
};
When to Use Edge vs Origin
| Use Case | Edge | Origin | Why |
|---|---|---|---|
| Auth/rate limiting | ✅ | Block bad traffic before it reaches origin | |
| Static API responses | ✅ | Cacheable, no origin needed | |
| Read-heavy data | ✅ | Edge replicas serve reads fast | |
| Full-text search | ✅ | Search indexes don't distribute well | |
| Complex transactions | ✅ | Needs single database for consistency | |
| AI inference | ⚠️ | ✅ | GPU availability (Cloudflare Workers AI is an exception) |
| Write-heavy | ✅ | Writes need to go to primary database | |
| Real-time WebSockets | ✅ | Durable Objects handle WebSocket state | |
| Geolocation routing | ✅ | Decision made before hitting origin | |
| A/B testing | ✅ | Feature flags at the edge |
Performance Impact
Real-World Latency
| Architecture | P50 Latency | P99 Latency |
|---|---|---|
| US-East origin (global users) | 150ms | 400ms |
| Multi-region origins (3 regions) | 80ms | 200ms |
| Edge functions + origin | 50ms | 150ms |
| Full edge (no origin) | 15ms | 50ms |
The Edge Tax
Edge has constraints:
| Constraint | Origin | Edge |
|---|---|---|
| CPU time per request | Minutes | 10-50ms |
| Memory | GBs | 128MB |
| Request body size | GBs | 100MB |
| Database options | Any | Limited (KV, D1, Turso) |
| Node.js APIs | Full | Partial (no fs, net, etc.) |
| NPM packages | All | Web-compatible only |
| Cold start | 100-500ms | <5ms |
Common Mistakes
| Mistake | Impact | Fix |
|---|---|---|
| Moving everything to the edge | Some workloads need origin | Use edge for what benefits from it |
| Ignoring cold starts on Lambda@Edge | 200ms+ on first request | Use V8 isolate platforms instead |
| Treating edge KV as a database | KV is eventually consistent | Use D1/Turso for strong consistency |
| Not measuring latency by region | Fast in US, slow in Asia | Test from multiple regions |
| Ignoring edge compute limits | Worker killed at 50ms CPU | Offload heavy work to origin |
Compare edge platforms and their APIs on APIScout — Cloudflare Workers vs Vercel Edge vs Deno Deploy, with benchmarks, pricing, and DX ratings.