Sub topic 14
Where to Run PgBouncer and How to Keep It Available
Assume you know how applications connect to PostgreSQL, how Linux services are managed, and what max_connections controls. This chapter focuses on deployment choices. It does not make PostgreSQL itself highly available.
PgBouncer's position in the network
PgBouncer sits between application clients and PostgreSQL. Clients connect to PgBouncer's listening address and port, often 6432. PgBouncer then opens and reuses server connections to PostgreSQL, usually on port 5432.
The simplest topology runs PgBouncer on the same host as PostgreSQL:
| Topology | Strength | Main risk |
|---|---|---|
| Same host | Low network latency and simple operations | Database host failure also removes the pooler |
| Application host or sidecar | Keeps pooling close to each application | More pooler processes to configure and monitor |
| Dedicated pooler hosts | Centralized management and independent scaling | Adds network hops and creates a pooler tier to make highly available |
Choosing a topology
Same host
A local PgBouncer is suitable for a small service or a single PostgreSQL instance. Bind it to 127.0.0.1 when only local applications should connect. This reduces network exposure and avoids a separate network failure between the application and pooler.
This arrangement does not protect against a host failure. If the host stops, both PostgreSQL and PgBouncer stop.
Application host or sidecar
Running PgBouncer beside an application keeps traffic local to that application host. A sidecar is a separate process or container deployed with the application. This can isolate tenants or workloads, but every sidecar needs configuration, credentials, logging, and upgrades.
Do not assume that multiple sidecars create one shared pool. Each sidecar maintains its own server connections. The total number of connections can therefore be larger than expected.
Dedicated pooler tier
A dedicated tier works well when many application instances need the same database. Run at least two PgBouncer instances and place a TCP load balancer or service discovery mechanism in front of them. Each PgBouncer instance has its own pool, so size the PostgreSQL connection budget across all instances.
For example, three PgBouncer instances with default_pool_size = 20 can open about 60 server connections for one user and database pair, before reserve or other pools are counted.
Availability and failure behavior
PgBouncer is a connection proxy, not a database failover system. It does not automatically turn a standby into a writable primary. A failover design needs a PostgreSQL-aware system, such as a managed database endpoint, a service-discovery update, or an external failover manager.
When a PgBouncer process fails, its client TCP connections fail too. Applications must reconnect with bounded retries and backoff. A load balancer can direct new connections to another healthy PgBouncer instance, but it cannot preserve transactions that were using the failed instance.
Use a process manager such as systemd, a container orchestrator, or a managed service to restart PgBouncer. Keep configuration and credentials consistent across instances. Apply changes with RELOAD where possible rather than restarting every pooler.
A practical availability checklist
- Keep PgBouncer close enough to the application to avoid unnecessary latency.
- Bind to a private address and restrict access with firewall rules or security groups.
- Run more than one instance when the application cannot tolerate pooler failure.
- Divide the PostgreSQL connection budget among all pooler instances.
- Configure application reconnects and request timeouts.
- Monitor both PgBouncer health and PostgreSQL health.
- Test database failover separately from PgBouncer failover.
Exercise
Three PgBouncer instances each allow 20 server connections for the same database and user. What connection budget should you reserve before adding other pools?
Show answer
Reserve about 60 server connections, because each instance can maintain its own pool of 20. Add capacity for reserve pools, other users and databases, maintenance connections, and PostgreSQL's other workloads.