Next.js Course
Next.js
/
Advanced

Edge Functions

Definition

Serverless functions that explicitly execute on the Edge Runtime network, providing lower latency and faster cold boots than traditional Serverless functions.

Explain Like I'm New

A traditional serverless function takes ~1 second to wake up (Cold Start) and lives in one city. An Edge function wakes up in 10 milliseconds and lives in every city on Earth.

Real World Example

Building an API that serves dynamic pricing data. Because it runs on the Edge, a user in India and a user in Brazil both get the API response in under 50ms, because the function executes in a data center blocks from their house.

Common Use Cases

  • •Personalization APIs
  • •Bot protection
  • •Header manipulation

Terminal Output

bash / terminal
/* Comparing Architectures: 1. Node.js Server (AWS EC2) - Location: 1 City (e.g. New York) - Speed: Slow for global users - Capabilities: Full OS access, heavy tasks 2. Serverless Function (AWS Lambda) - Location: 1 City (e.g. New York) - Speed: Has "Cold Starts" (1-3s delay) - Capabilities: Good for API routes, database connections 3. Edge Function (Vercel Edge) - Location: 35+ Cities Globally (Everywhere) - Speed: 0ms Cold Start. Instant execution. - Capabilities: Limited. Web APIs only. No direct DB sockets. */

Interview Questions

basic

  • Which is faster to wake up from sleep: A traditional Serverless function or an Edge function?

intermediate

  • Why can't you easily connect an Edge function directly to a standard PostgreSQL database?

Flash Cards

Question

Which is faster?

Click to reveal answer
Answer

An Edge function. It has virtually zero cold start time.

Question

Why not direct to DB?

Click to reveal answer
Answer

Most standard database drivers (like `pg` for Postgres) require native Node.js TCP socket connections, which do not exist in the lightweight Edge runtime. You must use HTTP-based connections (like Prisma Accelerate or REST APIs) to talk to a DB from the Edge.