logo
Parslinks
Back to blog
Guides4 min read

Connection pooling: why your database falls over at 100 concurrent users

"Too many connections" rarely means you need bigger hardware. It means nobody is managing connections.

Alireza Tadi

Co-founder & Head of Development and Communications, Parslinks

A fantasy-football app gets its biggest traffic of the week in the 30 minutes before the Saturday deadline. One season, the team scales from 2 to 8 instances to handle the rush. Within minutes, everything starts failing with FATAL: sorry, too many clients already.

Database CPU: 30%. Memory: plenty. Adding instances made it worse.

Connections are expensive

In PostgreSQL, each connection is a separate server process using several megabytes of memory. The default max_connections is usually 100. Now do the math:

  • Each app instance opens a pool of 15 connections.
  • 8 instances × 15 = 120 connections.
  • Plus the background worker, the scheduler and the monitoring agent.

You're over the limit before a single slow query runs.

What a pool does

Instead of opening and closing a connection for each request, the app keeps a few connections open and lends them out. When a request finishes, its connection goes back to the pool. That removes the cost of connecting and caps the total.

A connection budget: 8 web instances + 1 worker

Size the pool, don't guess

A rule of thumb that holds up well:

Sum of all pools across all processes ≤ 80% of max_connections

The remaining 20% is headroom for migrations, an admin console and monitoring. Then split that budget. With 8 web instances and one worker sharing 80 connections, that's roughly 8 per instance and 16 for the worker.

Eight feels small. But most queries in a web app finish in a few milliseconds, so eight connections can serve hundreds of requests per second, as long as no request holds a connection while waiting on something else.

When you need PgBouncer

When instance counts are large or change constantly (autoscaling, or serverless functions where every invocation wants its own connection), manual math stops working. PgBouncer sits between your app and the database, accepts hundreds of lightweight client connections and multiplexes them onto a few dozen real ones.

In transaction mode, a server connection is assigned to a client only for the duration of one transaction. It gives the biggest savings, but session-level features such as some prepared statements or SET commands need care. If you use an ORM, check its notes on PgBouncer compatibility.

The outcome

The team cut each instance's pool from 15 to 8, moved the push-notification call out of the transaction and put PgBouncer in front of the database. The next Saturday, with 12 instances running, the database never went above 60 real connections.

Frequently asked questions

Join the discussion

No comments yet. Be the first to share your thoughts.