(Note · · 5 min read)
Your serverless app opens a database connection it never closes
It works locally, it works under light traffic, and then a burst produces errors about too many connections. Nothing changed in the code. What changed is how many copies of it are running, and a connection pool designed for one long-lived server behaves very differently when there are two hundred short-lived ones.
By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka
Why the usual advice inverts
A pool exists so a server can reuse connections instead of opening one per request. That assumes a small number of processes, each alive for a long time. Serverless gives you the opposite: many instances, each short-lived, each creating its own pool on first use.
Multiply a modest pool size by the number of concurrent instances and you pass the database's connection limit without the traffic looking unusual. The database is not slow; it is refusing new connections because it has no room.
Pool outside the function
The fix is to put the pooling somewhere that persists: a connection pooler in front of the database, so the many short-lived callers share a bounded set of real connections. The function connects to the pooler, the pooler holds the actual sessions.
That changes what you can rely on. In the common transaction-level mode, a connection is assigned per transaction, so anything expecting state to survive between statements — a session variable, a prepared statement, a cursor held open — may not behave as it does locally. Know which mode you are on before debugging something strange.
Keep the client out of the handler
Create the client once at module scope so a reused instance reuses it, rather than inside the request handler where every invocation builds a new one. On a platform that reuses warm instances, that single change often removes the problem entirely.
Development makes this easy to get wrong, because hot reloading re-executes modules and accumulates clients until the limit is hit on a machine with no traffic at all. The usual remedy is to cache the instance on a global during development only.
Set the numbers deliberately
Give each instance a small maximum, since the aggregate is what matters. Set connection and statement timeouts so a stuck query returns a connection instead of holding it, and set an idle timeout so unused connections are given back rather than occupying slots.
Then watch the count against the limit as a metric, not as an error. Connection exhaustion is one of those failures that is obvious afterwards and invisible until the first time it happens in front of customers.
Do less work per request
Much of the pressure is self-inflicted: a page issuing a query per row, a handler running several round trips that could be one. Fewer, better queries reduce both latency and the number of connections in use at any moment, which is the cheapest fix available and the one nobody reaches for first.
Common questions
Why does my serverless app exhaust database connections?
Because each instance creates its own pool, and serverless runs many short-lived instances rather than one long-lived server. A modest pool size multiplied by concurrent instances passes the database's connection limit without the traffic appearing unusual.
How should serverless functions connect to Postgres?
Through a connection pooler that persists outside the functions, so many short-lived callers share a bounded set of real sessions. Note which pooling mode you are using, since transaction-level pooling does not preserve session state between statements.
Where should the database client be created?
At module scope, so a reused warm instance reuses the client, rather than inside the request handler where each invocation creates a new one. In development, cache it on a global so hot reloading does not accumulate clients.
What timeouts matter for serverless database access?
Connection and statement timeouts so a stuck query returns its connection rather than holding it, and an idle timeout so unused connections are released. Track the connection count against the limit as a metric rather than discovering it as an error.