Serverless Driver
Leverage SerenDB's serverless driver for ultra-low latency database queries over HTTP from serverless and edge environments.
Overview
The SerenDB Serverless Driver enables database queries from serverless functions and edge runtimes using HTTP, eliminating the need for traditional TCP connections. This is perfect for environments where TCP connection pooling isn't available or connection limits are restrictive.
Why Serverless Driver?
Traditional Connection Challenges
Serverless platforms present unique challenges:
Cold starts: Creating TCP connections adds latency
Connection limits: Functions can exhaust connection pools
Short execution time: Connections may not be reused
Edge deployment: TCP isn't available in some edge runtimes
Serverless Driver Benefits
HTTP-based: Works in any environment with HTTP fetch
No connection pooling needed: Each query is stateless
Low latency: Optimized for quick queries
Works everywhere: Serverless functions, edge workers, browsers
Automatic scaling: No connection limit concerns
Simple to use: Familiar SQL interface
Supported Platforms
Serverless Platforms
Vercel Functions: Seamless integration
Netlify Functions: Edge-ready
AWS Lambda: Works out of the box
Cloudflare Workers: Perfect for edge compute
Deno Deploy: Native support
Edge Runtimes
Vercel Edge Functions
Cloudflare Workers
Deno Deploy Edge
Next.js Edge Runtime
Frontend
Browser: Direct queries from client (use with caution and RLS)
React: Perfect for server components
Static site generators: Query at build time
Key Features
HTTP-based Queries
import { neon } from '@serenorg/serverless';
const sql = neon(process.env.DATABASE_URL);
const result = await sql`SELECT * FROM users WHERE id = ${userId}`;Transaction Support
Execute multiple queries in a single transaction over HTTP.
Type Safety
Full TypeScript support with type inference.
Automatic Retries
Built-in retry logic for transient errors.
Connection String Compatible
Use the same DATABASE_URL as traditional drivers.
Use Cases
Perfect For
API routes: Quick database queries in API endpoints
Edge functions: Minimal latency requirements
Webhooks: Process incoming webhook events
Scheduled tasks: Cron jobs and periodic functions
Build-time queries: Generate static sites with data
Not Ideal For
Long-running queries: Use traditional connection for queries > 30s
High throughput: Pooled connections may be more efficient
Connection-based features: LISTEN/NOTIFY, prepared statements
Performance
Latency
Typical query: 10-50ms
Optimized for short, frequent queries
Lower cold start impact than TCP connections
Throughput
Excellent for concurrent requests
No connection pool exhaustion
Scales with your serverless platform
Best Practices
Security
Never expose credentials: Use environment variables
Implement RLS: For client-side queries
Validate input: Always sanitize user input
Use API routes: Avoid direct client queries when possible
Performance
Keep queries simple: Complex queries may timeout
Use indexes: Ensure fast query execution
Cache when possible: Reduce database load
Batch operations: Group related queries
Error Handling
Handle timeouts: Set appropriate timeout values
Retry logic: For transient failures
Log errors: Monitor for patterns
Graceful degradation: Have fallback strategies
Getting Started
Install the package:
npm install @serenorg/serverlessConfigure your connection:
import { neon } from '@serenorg/serverless'; const sql = neon(process.env.DATABASE_URL);Execute queries:
const result = await sql`SELECT version()`;
Learn More
Comparison with Traditional Drivers
Connection
HTTP
TCP
Environment
Any with fetch
Node.js, Deno
Connection Pool
Not needed
Required
Cold Start
Faster
Slower
Long Queries
Limited
Supported
Transactions
HTTP-based
Full support
Best For
Edge/Serverless
Long-running apps
Migration from Traditional Drivers
If you're moving from pg or postgres.js:
Replace imports
Update connection initialization
Change query syntax to template literals
Test edge cases and error handling
Adjust timeout configurations
The serverless driver is designed to make SerenDB the best database for modern serverless and edge computing.
Last updated