How to avoid "too many connections" with serverless Postgres
Serverless platforms start many short-lived copies of your function, and each one can open its own Postgres connections until the server refuses more. Fix it by querying over HTTPS through a data API that pools connections for you, by capping each instance's pool at one or two connections, or both.
Last reviewed
Why does it happen?
PostgreSQL allots memory per connection and caps the total. A traffic spike can start hundreds of function instances; with a pool of 10 each, the database runs out long before your traffic does.
Three fixes
- Use the data API. Requests go over HTTPS to Quotura, which keeps a small pool per database and shares it across all your instances.
- Shrink per-instance pools. If you keep TCP, set the pool size to 1 or 2 and short idle timeouts.
- Keep long work off the request path. Batch jobs belong in a worker with its own connection, not in a request handler.
How many connections do I get?
Each plan sets a per-database connection limit, shown on the pricing page. The data API stays within it for you.
Frequently asked questions
Do I still need a connection string?
Use it for migrations and admin tools such as psql. Application traffic from serverless code can go through the data API.