perf hacks
Edge & APIsEdge Functions

Edge vs Serverless

Two runtimes, different tradeoffs. Pick wrong and you pay for it.

Choose Wisely

Two runtimes, very different tradeoffs. Using edge for database queries or serverless for simple routing decisions costs you money unnecessarily.

The Difference

ServerlessEdge
RuntimeNode.jsEdge (limited)
RegionsOne (US East)30+ globally
DB connectionsDirect (pooled)HTTP only
Cold start200-500ms0-50ms
BillingActive CPU timePer invocation

Quick Decision

Your NeedUse
Cookie check ("logged in?")Edge
Database queryServerless
Geo redirectEdge
Payment webhookServerless
A/B routingEdge
PDF generationServerless
Decision tree for choosing between Edge Runtime and Node.js Serverless based on your requirements

Cost Comparison

ScenarioEdgeServerless
Auth token check$0.50/1M$2-5/1M
A/B routing$0.50/1M$3-8/1M
DB queryN/A$1-3/1M
Heavy computeTimeout$5-20/1M

When Edge Wins

Routing, cookie checks, bot blocking - anything that reads headers/cookies and makes a quick decision:

middleware.ts
export function middleware(request: NextRequest) {
  const country = request.geo?.country || 'US';

  if (['DE', 'AT', 'CH'].includes(country)) {
    return NextResponse.rewrite(new URL('/de' + request.nextUrl.pathname, request.url));
  }
  return NextResponse.next();
}

When Serverless Wins

Database operations benefit from connection pooling:

app/api/users/route.ts
export async function GET() {
  const users = await db.query('SELECT * FROM users');
  return Response.json(users);
}

With Active CPU pricing, I/O wait time is free. Keep DB work in serverless.

Don't use export const runtime = 'edge' for database routes. HTTP connections from edge = high latency.


See Middleware Patterns for the biggest edge cost trap.

On this page