π§ What is PgBouncer?
PgBouncer is a lightweight PostgreSQL connection pooler.
It sits between your application and the PostgreSQL database, managing and reusing connections efficiently instead of creating new ones for every request.
βοΈ Why We Need PgBouncer
By default, PostgreSQL:
- Creates a new process for every client connection.
- Each connection consumes significant memory and CPU.
- Thousands of short-lived connections can easily overload PostgreSQL.
π This is where PgBouncer helps.
It keeps a pool of connections open to the database and reuses them for incoming client requests β saving overhead and improving performance.
π§© How PgBouncer Works
Architecture:
[Your App] <----> [PgBouncer] <----> [PostgreSQL Database]
- Your app connects to PgBouncer (instead of PostgreSQL directly).
- PgBouncer maintains a pool of active DB connections.
-
When the app requests a connection:
- PgBouncer gives it a pooled connection (if available).
- When done, PgBouncer returns it to the pool for reuse.
π Pooling Modes
PgBouncer supports three pooling modes:
| Mode | Description | Use Case |
|---|---|---|
| Session pooling | A client gets one DB connection for its entire session. | Default mode, good for most apps. |
| Transaction pooling | Connection is returned to the pool after each transaction. | Best for web apps with short transactions. |
| Statement pooling | Connection is returned after each SQL statement. | High concurrency systems, but not all queries are compatible. |
π Benefits of PgBouncer
β Faster connection handling (no need to fork a new PostgreSQL process per connection) β Lower memory usage on PostgreSQL β Better scalability β can handle thousands of concurrent app connections β Failover and load balancing (in some setups) β Supports connection limits and timeouts β Can handle database restarts gracefully
π§° Common Use Cases
- Web apps (like Spring Boot, Django, Node.js) with short, frequent DB queries
- Microservices architecture where each service opens multiple DB connections
- Serverless apps where connection churn is very high
- High-traffic APIs needing stable, low-latency DB access
π» Basic Example
Step 1: Install PgBouncer
sudo apt install pgbouncer
Step 2: Edit the configuration (/etc/pgbouncer/pgbouncer.ini)
[databases]
mydb = host=127.0.0.1 port=5432 dbname=mydb user=myuser password=mypassword
[pgbouncer]
listen_addr = 0.0.0.0
listen_port = 6432
auth_type = md5
auth_file = /etc/pgbouncer/userlist.txt
pool_mode = transaction
max_client_conn = 1000
default_pool_size = 100
Step 3: Start PgBouncer
sudo service pgbouncer start
Step 4: Connect your app
Instead of connecting to PostgreSQL on port 5432, connect to PgBouncer on port 6432.
π§ Example in Java (JDBC)
String url = "jdbc:postgresql://localhost:6432/mydb";
String username = "myuser";
String password = "mypassword";
Connection connection = DriverManager.getConnection(url, username, password);
Thatβs it β PgBouncer will handle the pooling behind the scenes.
π¨ Things to Watch Out For
-
In transaction pooling mode, you cannot use session-level features like:
- Temporary tables
- Session variables
- Prepared statements that persist beyond a single transaction
-
PgBouncer does not replace an ORMβs application-level connection pool (like HikariCP). π It sits below it β at the network/database layer.
π§© Analogy
Think of PostgreSQL as a restaurant kitchen π³ Each βconnectionβ is a waiter. If every customer brought their own waiter β chaos! PgBouncer is the restaurant manager who reuses a small team of waiters efficiently between customers π