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

  1. Never expose credentials: Use environment variables

  2. Implement RLS: For client-side queries

  3. Validate input: Always sanitize user input

  4. Use API routes: Avoid direct client queries when possible

Performance

  1. Keep queries simple: Complex queries may timeout

  2. Use indexes: Ensure fast query execution

  3. Cache when possible: Reduce database load

  4. Batch operations: Group related queries

Error Handling

  1. Handle timeouts: Set appropriate timeout values

  2. Retry logic: For transient failures

  3. Log errors: Monitor for patterns

  4. Graceful degradation: Have fallback strategies

Getting Started

  1. Install the package:

    npm install @serenorg/serverless
  2. Configure your connection:

    import { neon } from '@serenorg/serverless';
    const sql = neon(process.env.DATABASE_URL);
  3. Execute queries:

    const result = await sql`SELECT version()`;

Learn More

Comparison with Traditional Drivers

Feature
Serverless Driver
Traditional (pg/postgres)

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:

  1. Replace imports

  2. Update connection initialization

  3. Change query syntax to template literals

  4. Test edge cases and error handling

  5. Adjust timeout configurations

The serverless driver is designed to make SerenDB the best database for modern serverless and edge computing.

Last updated