CloudJod

Fixing itStartup founder

Postgres says "too many connections" every time we scale up. What do we do?

The answer

Each copy of the app opens its own pool of connections, so doubling the copies doubles the connections until the database's limit is hit. Put a connection pooler between them: the apps open as many connections as they like to it, and it keeps a small, fixed number open to the database.

Why doing it by hand hurts

Running PgBouncer yourself means another process to deploy, configure (pool mode, sizes, prepared statements), monitor and keep the passwords of in sync. Get the pool mode wrong and some queries break in ways that only show under load.

How CloudJod handles it

Turn on Pooler beside a PostgreSQL database: PgBouncer runs next to it in transaction mode, and every app attached gets DATABASE_POOL_URL beside its DATABASE_URL at its next deploy. The pool is sized from the database's memory, prepared statements work through it, and turning it on or off restarts the database once. The trade-offs: session features such as LISTEN and NOTIFY or advisory locks need the direct URL, and the pooler's share, an eighth of the database's processor and 32 MiB, counts against your plan.

web web web web PgBouncer PostgreSQL one per transaction, then given back
Many app connections share a small pool of database connections.
Under the hood

in transaction mode a database connection belongs to an app only for the length of one transaction. Why does that let thirty clients share ten connections?

Read more in the documentation