🌐Web Development

Edge Runtime

Edge Runtime

A lightweight runtime that runs at the CDN edge. Low latency, globally distributed.

πŸ“–Detailed Explanation

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.

πŸ’»Code Example

// 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;
}

πŸ—£οΈExample Conversations in Practice

πŸ’ΌIn an Architecture Design Meeting

"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."

🎯In a Technical Interview

"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."

πŸ”In a Code Review

"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

⚠️Cautions

πŸ”—Related Terms

πŸ“šLearn More