Computing at the Edge
Traditional cloud architectures deploy servers in a central region (e.g., US-East-1 in Virginia). If a user in Tokyo requests a dynamic page, the request travels thousands of miles across undersea cables, introducing 150ms+ of physical network latency. Edge Computing resolves this by running code directly on CDN edge nodes located globally.
Writing an Edge Middleware Function
Modern platforms like Vercel and Cloudflare use Edge Middleware to inspect and modify requests before they reach the main origin server:
// Vercel Edge Middleware Example
import { NextResponse } from 'next/server';
export const config = {
matcher: '/api/v1/:path*',
};
export function middleware(request) {
const geo = request.geo || {};
const country = geo.country || 'US';
// Geolocation-based routing directly at the edge node
const response = NextResponse.next();
response.headers.set('x-user-country', country);
return response;
}
Edge Data Access Strategies
Because code runs globally, queries to a central database will re-introduce latency. To scale data at the edge, engineers use:
- Edge Caching: Storing read-heavy APIs in regional CDN memory with stale-while-revalidate headers.
- Distributed Key-Value Stores: Storing sessions and config parameters in globally replicated databases (like Cloudflare KV or Upstash Redis).
- Serverless DB Proxies: Using WebSocket pooling proxies (like Prisma Accelerate or Neon serverless driver) to keep connections open.