Serverless Data Stores

Serverless and Edge Data Stores

Serverless functions break an assumption every database driver makes: that a process lives long enough to be worth a connection pool. An instance may serve one request and vanish, and a thousand may start at once. MongoDB 1,815 's guidance for AWS Lambda 24 is explicit — create the MongoClient at module scope, not inside the handler, so a warm instance reuses its connections; set maxIdleTimeMS to 60000 so connections from dead instances retire; and on a large sharded cluster cap srvMaxHosts.

Module-scope client reuse in a serverless handlerJavaScript
import { MongoClient } from 'mongodb';
const client = new MongoClient(process.env.MONGODB_URI, {
  maxIdleTimeMS: 60000, maxPoolSize: 10, srvMaxHosts: 3
});
export const handler = async (event) => {
  const db = client.db('shop');            // connects lazily, reused when warm
  const doc = await db.collection('products').findOne({ sku: event.sku });
  return { statusCode: doc ? 200 : 404, body: JSON.stringify(doc ?? {}) };
};

Edge runtimes are stricter: most offer no raw TCP sockets, so the MongoDB wire protocol is unavailable and you reach data over HTTP instead. Cloudflare D1 2 is a SQL database with a 10 GB per-database cap on Workers Paid (500 MB free), up to 50,000 databases per account, and a 30-second query ceiling — sized for many small databases, one per tenant. Neon and PlanetScale 104,806 put pooling and an HTTP endpoint in front of PostgreSQL 1,289 and MySQL 524 , Upstash 120,363 does the same for Redis 2,763 , and Turso 211,902 distributes SQLite 4,756 replicas close to users.

These stores trade query power and size for reachability from a function that starts in ten milliseconds and holds no state. Reading a session, a feature flag or one tenant's configuration at the edge is an excellent trade. Running the aggregation from MongoDB vs PostgreSQL there is not: put that behind a Node.js 2,131 service near the database and let the edge call it.