SYSTEMS |
JUL 12, 2024 |
By Spareek |
12 MIN READ
The Anatomy of Edge Computing
We’ve been historically bound to deploying our servers to single, central regions. If your database is in us-east-1 and your user is in Tokyo, they feel the speed of light working against them. Edge computing fundamentally breaks this barrier.
By moving computation to the outer boundary of the network—closest to the user—we reduce the round-trip latency. Static pages are cached globally on CDN nodes, but edge computing enables us to run dynamic server-side scripts at those same nodes, serving personalized, dynamic data in milliseconds.
Stateless Expansion & V8 Isolates
Edge logic executes on CDN nodes milliseconds from the client. By utilizing lightweight V8 isolates instead of heavy Docker containers, Cold Starts drop from seconds to zero.
Traditional serverless environments spin up virtual machines or containers running a Node.js runtime. This process introduces cold-start latency when a function hasn't been executed recently. V8 isolates, popularized by Cloudflare Workers and Vercel Edge, avoid this by running multiple secure JavaScript contexts inside a single process, avoiding runtime startup costs.
Node.js Serverless vs. V8 Edge Isolates
| Feature |
Node.js Serverless |
Edge V8 Isolates |
| Cold Start |
200ms - 2000ms |
Under 10ms (Often 0) |
| Memory Limit |
128MB - 10GB |
128MB - 256MB |
| Execution Limit |
Up to 15 mins |
50ms CPU time |
| APIs Available |
Full Node.js API |
Web Standard APIs (Fetch, Streams) |
The tradeoff? You can only run pure functions. No heavy Node.js standard libraries like fs or native C++ addons. Everything must comply with standard browser Web APIs.
edge-middleware.ts
export default async function middleware(req: Request) {
const url = new URL(req.url);
const country = req.headers.get('x-vercel-ip-country') || 'US';
// Geolocation routing at the edge node
if (url.pathname === '/shop') {
url.pathname = \`/shop/\$country.toLowerCase()\`;
return Response.redirect(url);
}
const response = await fetch(req);
response.headers.set('x-processed-by', 'edge-isolate-v8');
return response;
}
Edge Middleware and Performance
By moving authorization, redirects, and A/B testing logic to the edge via Middleware, you decouple the heavy lifting from your origin server. This results in a dramatic increase in time-to-first-byte (TTFB) and a smoother global user experience.
However, placing logic at the edge means you must rethink database access. If your database is in a single AWS region and your edge middleware calls it, you lose the latency benefits. To build true global systems, we must pair edge runtimes with globally distributed read-replicas, globally cached APIs, or edge-native databases like DynamoDB global tables.