What is PgBouncer and why do you need it?
PgBouncer is a lightweight connection pooler for PostgreSQL. It acts as a middleman between the application and the database.
When an application opens a new connection to PostgreSQL, it costs significant resources: process initialization, authorization, memory allocation. With a large number of clients, this becomes a bottleneck.
How PgBouncer solves this
- It keeps a limited number of active connections to PostgreSQL.
- When the application requests a connection, PgBouncer hands out one of the existing ones instead of creating a new one.
- After the query finishes, the connection goes back into the pool and can be reused.
Advantages
- significantly reduces load on the database server;
- speeds up application response time with many clients;
- lets you manage connection limits and timeout policy centrally;
- improves resilience, brief PostgreSQL outages can be recovered from without restarting applications.
In short, PgBouncer is a "buffer" that makes working with PostgreSQL faster, more stable, and more resource-efficient.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.