N
Neon5mo ago
fascinating-indigo

Best Method for Connecting to Neon from Serverless Functions Using Drizzle ORM

I'm building a Remix app hosted on Vercel and also running some AWS Lambda functions. I'm trying to determine the best way to connect to my Neon database using Drizzle ORM in a serverless environment. Currently, I'm evaluating two approaches: Here are the options I am currently looking at. Option 1: WebSocket Connection
import { neonConfig, Pool } from '@neondatabase/serverless';
import { drizzle } from 'drizzle-orm/neon-serverless';
import { WebSocket } from 'ws';
import * as schema from '~/.server/db/schema';

neonConfig.webSocketConstructor = WebSocket;

const pool = new Pool({ connectionString: process.env.DATABASE_URL });

const db = drizzle(pool, { schema });

export default db;
import { neonConfig, Pool } from '@neondatabase/serverless';
import { drizzle } from 'drizzle-orm/neon-serverless';
import { WebSocket } from 'ws';
import * as schema from '~/.server/db/schema';

neonConfig.webSocketConstructor = WebSocket;

const pool = new Pool({ connectionString: process.env.DATABASE_URL });

const db = drizzle(pool, { schema });

export default db;
Option 2: HTTP Connection
import { drizzle } from 'drizzle-orm/neon-http';
import * as schema from '~/.server/db/schema';

const db = drizzle(process.env.DATABASE_URL, { schema });

export default db;
import { drizzle } from 'drizzle-orm/neon-http';
import * as schema from '~/.server/db/schema';

const db = drizzle(process.env.DATABASE_URL, { schema });

export default db;
I'm looking for advice on the pros and cons of each approach, particularly in the context of high-concurrency workloads. For example, in a scenario where many AWS Lambda functions are triggered simultaneously to handle a high volume of incoming webhooks—each requiring a DB query—is there a recommended approach? Any input or experience with either method would be really appreciated!
2 Replies
equal-aqua
equal-aqua5mo ago
@Jake https://github.com/neondatabase/examples/tree/main/with-nextjs-drizzle-edge here's an example that you'd want to use with Serverless Functions.
GitHub
examples/with-nextjs-drizzle-edge at main · neondatabase/examples
Examples and code snippets demonstrating common ways of integrating Neon with various frameworks and languages. - neondatabase/examples
equal-aqua
equal-aqua5mo ago
TLDR: the http method

Did you find this page helpful?