Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Why do PostgreSQL deployments often put PgBouncer between applications and the database even when every Spring Boot service already has a HikariCP pool?

Because each PostgreSQL connection is a separate server process with its own memory, and with many services and replicas the HikariCP pools add up: 50 pods with 20 connections each is 1,000 server connections, far more than the database should run. PgBouncer accepts all those client connections and multiplexes them onto a small number of real server connections. In transaction mode it gives the best reuse, but it breaks anything that relies on session state, such as SET, session advisory locks, LISTEN and, in older versions, server-side prepared statements.

Why this question is asked

Many teams are surprised to find PgBouncer in front of their database when every service already has a connection pool. It looks like two pools doing the same job. Interviewers ask about it because the answer shows we understand what a PostgreSQL connection costs on the server, why application pools do not solve that at fleet scale, and what we give up in each PgBouncer mode. In this answer, we will recap the process-per-connection model, do the arithmetic for a fleet, see how PgBouncer multiplexes, compare its three pool modes, and look at what transaction mode breaks.

1A Quick Recap: One Process per Connection

PostgreSQL serves each client connection with its own operating system process, called a backend. Starting one takes a fork and some setup, and each backend keeps private memory: caches of catalog data, prepared plans, and buffers for its queries. An idle backend may use a few megabytes; a backend that has touched many tables often uses 10 MB or more.

Memory is only part of the cost. Every backend takes part in some shared work, for example building a snapshot means looking at all active transactions, and with thousands of connections, lock and snapshot overhead grows even when most connections are idle. In practice, a server with 16 cores runs best with perhaps a few dozen connections doing work at the same time, not thousands.

2Why Application Pools Are Not Enough

HikariCP solves the problem inside one application instance: it keeps a set of open connections and lends them to threads, so the service does not open a new connection per request. But each instance has its own pool, and pools do not know about each other.

SQL
-- One service, 50 pods, HikariCP maximumPoolSize = 20
-- 50 x 20 = 1,000 server connections
-- Four such services: 4,000
SELECT count(*), state FROM pg_stat_activity GROUP BY state;
-- 3720 | idle
-- 140 | active
-- 40 | idle in transaction

Notice the pattern: thousands of connections, only about a hundred busy at any moment. Each pod keeps its pool open in case it needs it, so the server pays for thousands of processes that mostly wait. Shrinking every pool to 3 would help the server but starve pods during bursts. What we need is a shared layer that lets a busy pod use a server connection that another pod is not using right now.

3How PgBouncer Multiplexes Connections

PgBouncer is a lightweight proxy that speaks the PostgreSQL protocol. Applications connect to it as if it were the database, and it keeps a small pool of real server connections per database and user pair. When a client needs to run something, PgBouncer lends it a server connection, and takes it back afterwards. Thousands of client connections cost PgBouncer very little, because it is a single event-driven process, not a process per client.

Step throughTransaction mode: many clients, few server connections
Pod 1Pod 2PgBouncerPostgreSQL
BEGIN; UPDATE ...; COMMIT
runs on server connection 7
COMMIT done: connection 7 back in the pool
BEGIN; SELECT ...; COMMIT
runs on server connection 7 again
results
results

7 messages in order. Play them, or step through them one at a time.

Server connection 7 served two pods, one transaction each.

So in our example, the 4,000 HikariCP connections go to PgBouncer, and PgBouncer runs perhaps 100 server connections, set by default_pool_size and max_db_connections. The database sees 100 processes instead of 4,000.

4The Three Pool Modes

PgBouncer decides when to take the server connection back according to its pool mode. In session mode, a client keeps the same server connection from connect to disconnect. That is fully compatible with everything PostgreSQL offers, but it gives little benefit when HikariCP holds connections open all day. In transaction mode, the client gets a server connection only for the duration of a transaction and returns it at COMMIT or ROLLBACK. That gives the best reuse, because pods hold server connections only while they actually work. In statement mode, the connection is returned after every statement, which forbids multi-statement transactions and is rarely used.

PgBouncer pool modes
ModeServer connection held forSession featuresReuse
sessionthe whole client connectioneverything workslow with long-lived pools
transactionone transactionsession state breakshigh, the usual choice
statementone statementno multi-statement transactionshighest, rarely usable

5What Transaction Mode Breaks

In transaction mode, two transactions from the same client can run on different server connections. Anything that belongs to the session rather than the transaction therefore leaks or disappears. A plain SET search_path or SET timezone changes one server connection, which another client may get next, while our next transaction runs somewhere else. Session-level advisory locks taken with pg_advisory_lock stay on a server connection that is then handed to someone else. LISTEN registers on one connection, so notifications never reach us. Temporary tables vanish from our point of view between transactions.

The fixes are to keep such state inside the transaction, with SET LOCAL and transaction-level advisory locks (pg_advisory_xact_lock), or to send the few clients that need session features, such as a LISTEN consumer, through a separate pool in session mode.

Server-side prepared statements were the biggest problem for Java services, because the PostgreSQL JDBC driver prepares named statements on a connection after a statement has been used several times. A named statement prepared on one server connection does not exist on another.

SQL
BEGIN; SET LOCAL statement_timeout = '5s'; SELECT ...; COMMIT; -- safe: lasts one transaction
SELECT pg_advisory_xact_lock(42); -- safe: released at commit
SET search_path = tenant_7; -- unsafe in transaction mode

6Follow-up questions

What does PgBouncer 1.21 and later change for prepared statements?

PgBouncer 1.21 added support for protocol-level named prepared statements in transaction and statement mode, controlled by max_prepared_statements. PgBouncer tracks the statements each client prepares and prepares them on whichever server connection the client gets, under its own names, so the JDBC driver's server-side prepares keep working. It only covers prepares sent through the protocol, which is what JDBC uses, not SQL PREPARE commands, and the default value has changed between releases, so check your version. Before 1.21 the usual fix was prepareThreshold=0 in the JDBC URL.

Should HikariCP pools be smaller with PgBouncer?

They can stay moderate, because client connections to PgBouncer are cheap, but there is no reason to make them large. The real limit is PgBouncer's server pool size, which should be set from what the database can handle, for example a few times the number of CPU cores across all clients.

Are there alternatives to PgBouncer?

Yes. Managed proxies such as RDS Proxy do a similar job, and newer poolers exist. The trade-offs are the same: multiplexing gives reuse and costs session state.

7Conclusion

In this answer, we saw that every PostgreSQL connection is a process with its own memory and shared overhead, so the sum of HikariCP pools across a fleet, such as 50 pods with 20 connections each, quickly exceeds what the server should run, while most of those connections sit idle. PgBouncer multiplexes many client connections onto a small set of server connections. Session mode keeps everything working with little reuse, transaction mode gives the best reuse but breaks session state such as SET, session advisory locks, LISTEN and, before version 1.21, server-side prepared statements, and statement mode is rarely usable.

What to say in the interviewAbout 90 seconds aloud

Open with

PostgreSQL runs one process per connection, and each one has its own memory and adds to shared overhead like snapshot building, so the server is happiest with a few dozen busy connections, not thousands.

Then, if they want more

HikariCP pools solve that inside one instance, but they do not know about each other. If a service has 50 pods with a pool of 20, that is 1,000 connections, and with several services it is thousands, most of them idle at any moment.

PgBouncer sits in front and multiplexes: the pods connect to PgBouncer, which is cheap, and PgBouncer keeps maybe 100 real server connections and lends them out. The pool mode decides when it takes a connection back. Session mode keeps the same server connection for the whole client session, so everything works but reuse is low.

Transaction mode lends it for one transaction, which gives the best reuse and is what we usually use. The catch is session state: a plain SET, session advisory locks, LISTEN and temp tables do not carry across transactions, because the next transaction can run on another server connection. So we use SET LOCAL and pg_advisory_xact_lock instead.

Server-side prepared statements from the JDBC driver used to break too; PgBouncer 1.21 and later can track them with max_prepared_statements, and before that we set prepareThreshold to 0. Statement mode exists but forbids multi-statement transactions.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
PostgreSQL documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Why do PostgreSQL deployments often put PgBouncer between applications and the database...": https://scrivio-interview-library.netlify.app/databases/postgresql/6c599fd21b/