billing hacks
Edge & MiddlewareEdge vs Serverless

Edge vs Serverless

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

EDGE vs SERVERLESSUserEdge30+ regions0-50ms cold startServerlessNode.js runtimeServerless1 region (US East)200-500ms cold startDatabaseNo direct DBAuth • Geo • A/BDB • Heavy compute

TL;DR

Use Edge for quick decisions (auth checks, geo routing, A/B tests). Use Serverless for database queries and heavy compute. Edge is cheaper per-request but can't connect directly to databases.

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
EDGE vs SERVERLESS DECISIONNeed direct database connection?NoYesQuick decision? (auth, geo, A/B)ServerlessNode.js, full APIsYesNoEdgeFast, cheap, globalHeavy compute needed?NoYes→ Edge→ ServerlessEdge: 0.50/1M</text><textx="640"y="275"fill="currentColor"fontFamily="system−ui"fontSize="10"textAnchor="middle"opacity="0.4">Serverless:0.50/1M</text> <text x="640" y="275" fill="currentColor" fontFamily="system-ui" fontSize="10" textAnchor="middle" opacity="0.4">Serverless: 2-5/1M4-10x cheaper for quick ops

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