Edge & MiddlewareEdge vs Serverless
Edge vs Serverless
Two runtimes, different tradeoffs. Pick wrong and you pay for it.
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
| Serverless | Edge | |
|---|---|---|
| Runtime | Node.js | Edge (limited) |
| Regions | One (US East) | 30+ globally |
| DB connections | Direct (pooled) | HTTP only |
| Cold start | 200-500ms | 0-50ms |
| Billing | Active CPU time | Per invocation |
Quick Decision
| Your Need | Use |
|---|---|
| Cookie check ("logged in?") | Edge |
| Database query | Serverless |
| Geo redirect | Edge |
| Payment webhook | Serverless |
| A/B routing | Edge |
| PDF generation | Serverless |
Cost Comparison
| Scenario | Edge | Serverless |
|---|---|---|
| Auth token check | $0.50/1M | $2-5/1M |
| A/B routing | $0.50/1M | $3-8/1M |
| DB query | N/A | $1-3/1M |
| Heavy compute | Timeout | $5-20/1M |
When Edge Wins
Routing, cookie checks, bot blocking - anything that reads headers/cookies and makes a quick decision:
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:
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.