The connection ceiling, and why pgbouncer exists
Tier 4 is about to teach you that the application tier scales horizontally: add servers, serve more users. There is a limit hiding underneath that promise, and it is one of the most common ways a system that "scaled" falls over anyway.
A connection is not free
In PostgreSQL, every client connection is a separate operating-system process. It costs several megabytes of memory before it does any work, a slot in shared memory, and a share of the scheduler's attention. MySQL uses a thread rather than a process, which is cheaper, but not free. A typical max_connections is somewhere around 100–200, and that is not a licensing decision — it is the point past which the database spends more effort coordinating than working.
Because the deeper limit is not memory. It is that a database with sixteen cores can genuinely execute about sixteen things at once. Beyond that, extra concurrent queries do not run in parallel — they contend for locks, for buffer pool space, and for the same disk queue, and they all slow down together. Concurrency past the useful limit does not add throughput; it subtracts it.
The arithmetic that catches people
Now put that next to a horizontally scaled application tier:
20 app servers × pool of 20 each = 400 connections wanted
max_connections = 100
↑ the ceiling
And notice the cruel part: the fix for load — add app servers — makes this worse. Autoscaling under traffic multiplies the demand on the one component that cannot scale out. The stateless tier grows, its connection appetite grows with it, and the database does not care how many servers you added. Serverless functions are the extreme version: each concurrent invocation is its own runtime with its own connection, so a burst of 500 invocations is a request for 500 connections.
This is the connection ceiling, and it is why a database can be at 20% CPU while your application reports that the database is down. Nothing is overloaded. Everything is queued behind a connection it cannot get.
The pooler
Lesson 13.27's dojo built a pool inside one process. That helps one app server and does nothing about twenty of them, because each has its own. What is needed is a pool outside the application, shared: an external connection pooler — pgbouncer, pgcat, RDS Proxy — sitting between the fleet and the database.
500 app connections ──▶ [ pgbouncer ] ──▶ 20 database connections
It works because your application connections are mostly idle. A connection held for the duration of a request spends the vast majority of that request not talking to the database. The pooler multiplexes the busy moments of hundreds of clients onto a handful of real server connections, and the ratio is routinely 20:1 or better.
Three modes, and the middle one is the one that matters:
- Session pooling — a server connection is assigned for the whole client session. Saves the handshake and nothing else; the ceiling is unchanged. Safe, and mostly pointless.
- Transaction pooling — the server connection is handed back at every
COMMIT. This is the mode that produces the 20:1 ratio, and the one people mean when they say "we put pgbouncer in front". - Statement pooling — returned after every statement. Maximum sharing, and multi-statement transactions become impossible. Rarely the right answer.
What transaction pooling quietly breaks
This is the part that ships broken, because it works perfectly in staging. Under transaction pooling, two consecutive queries from the same client may run on different server connections. Anything that lives in session state therefore evaporates:
- Server-side prepared statements — and many drivers use them by default, so you get errors that appear only under concurrency
SETat session scope, session-level variables, and search_path changes- Advisory locks,
LISTEN/NOTIFY, and temporary tables WITH HOLDcursors that outlive their transaction
None of these fail in development, where you are the only client and the pooler hands you the same connection every time. They fail in production, intermittently, under load — which is the worst possible failure signature and the reason this lesson exists.
Sizing, which is counter-intuitive
How many server connections should the pooler hold? Far fewer than instinct suggests. Throughput times duration gives you the concurrency actually in use: 1000 queries per second at 5 ms each is five connections busy on average. Even allowing generous headroom for spikes and slow queries, the answer is tens, not hundreds.
The point of a small pool is not thrift. It is that the queue has to live somewhere, and the pooler is a far better place for it than the database. In the pooler, waiting is cheap, visible, and measurable — you can watch the wait time and alert on it. Inside the database, each waiting connection is a process consuming memory and scheduler time, making the congestion it is waiting on slightly worse.
Responding to connection exhaustion by raising max_connections. It makes the error go away, which is why it is so popular, and what it actually does is move the queue from a place designed for queueing into a place that pays several megabytes and a process for each waiter. Throughput drops, latency rises, and the next exhaustion arrives at a higher number with less warning. The ceiling is a property of the database, not a setting you were too timid to raise — pooling is how you live comfortably underneath it.