Lesson 12 / 32
Databases for Serverless Workloads
Avoid connection storms, reuse clients across invocations, and choose data stores and placement that suit functions.
By Rahul Agarwal · 15 years in web development
Databases when compute scales out
Traditional relational databases assume a few long-lived app servers holding pooled connections. Serverless breaks that assumption: a traffic spike can create hundreds of function instances, each opening its own connection, until the database hits its connection limit and starts refusing clients. The options: put a connection pooler in between (Amazon RDS Proxy, PgBouncer) so many functions share a few database connections; create clients outside the handler so warm invocations reuse them; cap function concurrency so it never exceeds what the database can take; or pick data stores built for this pattern, such as DynamoDB-style key-value stores reached over HTTP, or managed Postgres and MySQL services that offer HTTP or WebSocket drivers suited to short-lived and edge runtimes. Also respect data gravity: a function should normally run in the same region as its primary database, because every query pays the network round trip.
A client created once per environment
The pool lives at module scope, so warm invocations reuse its connection. Since a standard Lambda environment handles one event at a time, one connection per environment is enough.
import pg from 'pg';
// Created during Init, reused by every warm invocation.
const pool = new pg.Pool({
connectionString: process.env.DATABASE_URL, // ideally a pooler or proxy endpoint
max: 1, // one event at a time per environment
idleTimeoutMillis: 60_000,
});
export const handler = async (event) => {
const { rows } = await pool.query(
'SELECT name, branch FROM students WHERE roll_no = $1',
[event.rollNo],
);
return rows[0] ?? null;
};Parameterised queries, always
The $1 placeholder in the example is not decoration. Building SQL with string concatenation from event fields invites SQL injection, and event data is untrusted input, exactly like a form on a website.
Quick check: A spike creates 800 concurrent Lambda environments, each opening one Postgres connection, and the database allows 200. What is the most direct fix?
- Increase the function memory
- Route connections through a pooler such as RDS Proxy and limit function concurrency
- Move the function to another region
- Open a fresh connection inside the handler on every call
Answer
Route connections through a pooler such as RDS Proxy and limit function concurrency — A pooler multiplexes many clients onto few connections, and a concurrency cap keeps the function from overrunning the database. Opening per-call connections makes it worse.