Skip to main content
Drizzle ORM is designed to work seamlessly in edge runtime environments, enabling you to run database queries close to your users anywhere in the world.

What is Edge Runtime?

Edge runtimes are JavaScript environments that run on globally distributed networks (CDNs), providing:
  • Low latency: Code executes geographically close to users
  • Global distribution: Deploy to 200+ locations worldwide
  • Instant cold starts: Sub-50ms initialization times
  • Cost efficiency: Pay only for actual execution time
  • Auto-scaling: Handle millions of requests automatically

Supported Edge Platforms

Drizzle works with all major edge platforms:

Cloudflare Workers

Deploy to 300+ cities with D1 SQLite database

Vercel Edge Functions

Serverless functions with edge caching

Deno Deploy

Edge runtime with native TypeScript support

Netlify Edge Functions

Edge functions powered by Deno

Edge Runtime Constraints

Edge environments have specific limitations you must understand:
No TCP Connections: Traditional database drivers using TCP (like pg, mysql2) don’t work in edge runtimes. Use HTTP-based drivers instead.
Limited APIs: Node.js APIs like fs, net, and child_process are not available. Drizzle’s edge-compatible drivers avoid these dependencies.

Compatible Database Drivers

Cloudflare Workers

Cloudflare Workers run on one of the world’s largest edge networks with 300+ data centers.

Cloudflare D1 (SQLite)

D1 is Cloudflare’s serverless SQLite database, globally replicated to all edge locations.
1

Install dependencies

D1 types are included in @cloudflare/workers-types
2

Define your schema

schema.ts
3

Configure Drizzle Kit

drizzle.config.ts
4

Create your Worker

src/index.ts
5

Configure wrangler.toml

wrangler.toml
6

Generate and apply migrations

D1 Batch Operations

D1 supports efficient batch queries to reduce latency:

D1 Transactions

Connecting to External Databases

For PostgreSQL or MySQL, use HTTP-based drivers:

Vercel Edge Functions

Vercel Edge Functions run on the Vercel Edge Network using the V8 runtime.

Setup with Vercel Postgres

1

Create Vercel Postgres database

In your Vercel project dashboard:
  1. Go to Storage tab
  2. Create new Postgres database
  3. Environment variables are auto-injected
2

Install dependencies

3

Create edge API route

app/api/users/route.ts

Edge Middleware Example

middleware.ts

Using with Neon

app/api/posts/route.ts

Deno Deploy

Deno Deploy is a globally distributed edge runtime with native TypeScript support.

PostgreSQL with Neon

main.ts

SQLite with Turso

Netlify Edge Functions

Netlify Edge Functions use Deno runtime and work similarly to Deno Deploy.
netlify/edge-functions/users.ts

Performance Optimization

Minimize Round Trips

Use batch queries when possible:

Use Relational Queries

Drizzle’s relational queries can fetch nested data efficiently:

Cache Aggressively

Connection Initialization

Initialize database connections outside request handlers when possible:

Error Handling

Implement proper error handling for network issues:

TypeScript Configuration

Ensure your TypeScript config works with edge runtimes:
tsconfig.json

Deployment Checklist

1

Verify edge compatibility

  • Use HTTP-based database drivers only
  • Avoid Node.js-specific APIs
  • Test locally with edge runtime emulators
2

Optimize bundle size

  • Import only needed Drizzle modules
  • Use tree-shaking friendly imports
  • Check bundle size with your platform’s CLI
3

Configure environment variables

  • Set DATABASE_URL in platform dashboard
  • Use platform-specific secrets management
  • Never commit credentials to git
4

Test in staging

  • Deploy to preview/staging environment first
  • Verify queries work correctly
  • Check latency and performance
5

Monitor production

  • Set up error tracking (Sentry, LogRocket, etc.)
  • Monitor query performance
  • Track edge function execution time

Troubleshooting

You’re using a Node.js-specific driver. Switch to an edge-compatible driver:
  • pgdrizzle-orm/neon-http
  • mysql2drizzle-orm/planetscale-serverless
  • better-sqlite3drizzle-orm/d1 or drizzle-orm/libsql
Edge functions have strict execution time limits (usually 10-30 seconds):
  • Optimize slow queries with indexes
  • Use batch operations to reduce round trips
  • Consider caching frequently accessed data
Reduce bundle size:
  • Import specific Drizzle modules: import { eq } from 'drizzle-orm'
  • Avoid importing entire schema in edge functions
  • Use platform-specific bundling optimizations
Verify:
  • Environment variables are set correctly
  • Connection string includes proper authentication
  • Database allows connections from edge IP ranges
  • SSL/TLS is configured if required

Next Steps

Serverless Databases

Learn about serverless database platforms

Best Practices

Optimize your Drizzle implementation

Schema Design

Design efficient database schemas

Query Performance

Write fast, optimized queries