Edge Runtime
Edge Runtime
A lightweight runtime that runs at the CDN edge. Low latency, globally distributed.
Edge Runtime
A lightweight runtime that runs at the CDN edge. Low latency, globally distributed.
Edge Runtime is a lightweight JavaScript/WASM runtime that executes code on CDN edge servers around the world. Because code runs on the server closest to the user, it can reduce latency by 50β200ms compared to the traditional centralized server approach. Vercel Edge Functions, Cloudflare Workers, and Deno Deploy are representative examples.
Edge Runtime uses V8 Isolates technology to cold start in milliseconds. This contrasts with conventional serverless (Lambda), which has a cold start of 100β500ms. However, it does not support the full Node.js API, and the use of fs (file system) and native modules is restricted.
Major use cases include A/B testing, localization (i18n), authentication verification, API response caching, and bot detection. It is especially effective when you want to deliver personalized content while still keeping the benefits of CDN caching. Next.js Middleware also runs on Edge Runtime.
Constraints include execution time limits (Cloudflare 50ms CPU, Vercel 30 seconds), a memory limit (128MB), and lack of support for some Web APIs. Tasks that require heavy computation or long execution times are still better suited to serverless functions or traditional servers.
// Next.js Edge Middleware μμ
// middleware.ts
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export const config = {
matcher: ['/api/:path*', '/dashboard/:path*'],
};
export function middleware(request: NextRequest) {
// 1. μ§μ κΈ°λ° λΌμ°ν
const country = request.geo?.country || 'US';
if (request.nextUrl.pathname === '/') {
return NextResponse.redirect(
new URL(`/${country.toLowerCase()}`, request.url)
);
}
// 2. μΈμ¦ ν ν° κ²μ¦ (μ£μ§μμ μ¦μ μ²λ¦¬)
const token = request.cookies.get('auth-token');
if (request.nextUrl.pathname.startsWith('/dashboard')) {
if (!token) {
return NextResponse.redirect(new URL('/login', request.url));
}
// ν ν° μ ν¨μ± κ°λ¨ κ²μ¦ (λ¬΄κ±°μ΄ κ²μ¦μ APIμμ)
try {
const payload = JSON.parse(atob(token.value.split('.')[1]));
if (payload.exp < Date.now() / 1000) {
return NextResponse.redirect(new URL('/login', request.url));
}
} catch {
return NextResponse.redirect(new URL('/login', request.url));
}
}
// 3. A/B ν
μ€νΈ μΏ ν€ μ€μ
const response = NextResponse.next();
if (!request.cookies.has('ab-variant')) {
response.cookies.set('ab-variant', Math.random() > 0.5 ? 'A' : 'B');
}
return response;
}
"It's a global service, but even users in the US have their requests routed to the Asian server, so latency is high. If we move simple logic like authentication verification to Edge Runtime, it's handled right at the user's location, so we can cut TTFB by more than 200ms."
"Interviewer: What is the difference between Edge Runtime and general serverless?
Candidate: Edge Runtime is based on V8 Isolates, so there is almost no cold start, and it runs on 300+ edge locations worldwide. Serverless like Lambda, on the other hand, is deployed per region, so there is latency depending on distance, and because it is container-based, a cold start can take 100ms or more."
"This middleware is doing a DB query, but Edge Runtime has difficulty maintaining TCP connections, so connection overhead occurs on every request. It would be better to move the DB query to an API Route and have the middleware do only pure computation such as token decoding."
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free