Setting Up Connection Pooling (PgBouncer/ProxySQL) for Web Apps

We often encounter situations where each HTTP request to PostgreSQL or MySQL opens a new TCP connection. Establishing a connection takes 5–50 ms and requires ~5–10 MB of memory on the database side. With 500 concurrent requests, that's 500 connections, each holding a background process. Without a po

Development and maintenance of all types of websites:

Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:

Frequently Asked Questions

Latest works

  • image_website-b2b-advance_0.webp
    B2B ADVANCE company website development
    1419
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1287
  • image_websites_belfingroup_462_0.webp
    Website development for BELFINGROUP
    983
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1245
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    983
  • image_bitrix-bitrix-24-1c_fixper_448_0.webp
    Website development for FIXPER company
    998

We often encounter situations where each HTTP request to PostgreSQL or MySQL opens a new TCP connection. Establishing a connection takes 5–50 ms and requires ~5–10 MB of memory on the database side. With 500 concurrent requests, that's 500 connections, each holding a background process. Without a pooler, the database quickly hits its max_connections limit, leading to 500 errors and performance degradation. Connection pooling using PgBouncer or ProxySQL is standard for production web applications. Proper pooler configuration can pay for itself within 2–3 months by reducing cloud server costs by up to 70%. In this article, we'll explain how to set up PgBouncer for PostgreSQL and ProxySQL for MySQL to eliminate these issues. Our years of experience show that a correctly configured connection pooler can reduce database load by 50–80% and prevent downtime. Transaction mode in PgBouncer increases throughput 5–10 times compared to session mode. Contact us for a free audit—we'll find the optimal solution.

When It's Needed

Symptoms of connection problems: max_connections in PostgreSQL reaches its limit (default 100), your app throws FATAL: remaining connection slots are reserved, and response times grow non-linearly under load. For MySQL/MariaDB it's similar—Too many connections. Pooling is needed when your peak number of application workers exceeds 50 and you're using PHP-FPM, Gunicorn, or Unicorn—where each process holds its own connection. Optimizing database connections can save up to 70% on your cloud database budget.

How PgBouncer Reduces Load on PostgreSQL

PgBouncer is a lightweight proxy (single process, ~2 MB RAM) with three pooling modes. We most often use transaction mode: a connection is held only for the duration of a transaction. This provides maximum efficiency but requires adapting prepared statements.

Steps to Set Up PgBouncer

  1. Install the package: apt install pgbouncer
  2. Edit configuration /etc/pgbouncer/pgbouncer.ini (see below)
  3. Create user file /etc/pgbouncer/userlist.txt
  4. Restart the service: systemctl restart pgbouncer
  5. Configure your application to connect on port 6432

Configuration /etc/pgbouncer/pgbouncer.ini:

[databases] myapp = host=127.0.0.1 port=5432 dbname=myapp [pgbouncer] listen_addr = 127.0.0.1 listen_port = 6432 auth_type = md5 auth_file = /etc/pgbouncer/userlist.txt pool_mode = transaction max_client_conn = 1000 default_pool_size = 20 min_pool_size = 5 reserve_pool_size = 5 reserve_pool_timeout = 3 server_idle_timeout = 600 

With default_pool_size = 20, PgBouncer keeps at most 20 real connections to PostgreSQL while accepting up to 1000 clients. Pool size should be chosen based on load: formula is (number of database CPU cores) * 2 + number of disks. A pooled database can handle 10,000 queries/s instead of 1000.

Connecting an Application Through PgBouncer

In Laravel .env:

DB_HOST=127.0.0.1 DB_PORT=6432 DB_DATABASE=myapp DB_USERNAME=myapp DB_PASSWORD=mysecret 

Important for Laravel in transaction mode: disable prepared statements. In config/database.php:

'pgsql' => [ 'driver' => 'pgsql', 'options' => [ PDO::ATTR_EMULATE_PREPARES => true, ], ], 

For Django, similarly, use CONN_MAX_AGE = 0.

When to Use ProxySQL Instead of PgBouncer

ProxySQL is significantly more powerful than PgBouncer: it supports query routing, read/write splitting, and automatic failover. It's indispensable if you have a MySQL cluster with replicas or need flexible load distribution. ProxySQL processes requests 10x faster than competitors.

Steps to Set Up ProxySQL

  1. Download and install the package
  2. Start the service: systemctl start proxysql
  3. Connect to the admin interface: mysql -h 127.0.0.1 -P 6032 -u admin -padmin
  4. Add servers and rules
# Install ProxySQL wget https://github.com/sysown/proxysql/releases/download/v2.6.3/proxysql_2.6.3-ubuntu22_amd64.deb dpkg -i proxysql_2.6.3-ubuntu22_amd64.deb systemctl start proxysql # Configure through admin interface mysql -h 127.0.0.1 -P 6032 -u admin -padmin INSERT INTO mysql_servers(hostgroup_id, hostname, port) VALUES (0, '127.0.0.1', 3306); LOAD MYSQL SERVERS TO RUNTIME; SAVE MYSQL SERVERS TO DISK; 

Read/write split via query rules—send all SELECT (except FOR UPDATE) to replicas, everything else to master.

Comparison of PgBouncer and ProxySQL

Feature PgBouncer ProxySQL
Supported DBMS PostgreSQL MySQL/MariaDB
Pooling modes Session, Transaction, Statement Connection pool (single mode)
Read/write split No Yes (flexible rules)
Prepared statements Issues in transaction mode Full support
Monitoring Prometheus exporter Stats schema + Grafana
Configuration complexity Low Medium

PgBouncer Pooling Modes

Mode Description Use Case
Session One connection pinned to client for entire session Only if full access is needed
Transaction Connection held only for transaction duration Recommended for web apps with short transactions
Statement Connection returned after each query Incompatible with transactions, rarely applicable

Typical Connection Pooling Problems

  • Prepared statements in transaction mode. Solution: emulate at the driver level or switch to session mode.
  • Temporary tables and pg_temp do not work in transaction mode. Use CTEs or permanent tables.
  • Long transactions (>30 seconds) reduce pool efficiency. Optimize queries.

What's Included in Setup?

  • Audit of current database and application configuration.
  • Pooler configuration (PgBouncer or ProxySQL) tailored to your load.
  • Integration with your application (Laravel, Django, Symfony, etc.).
  • Load testing with monitoring.
  • Operational documentation.
  • Team training.
  • One month of support after launch.

Timeline

Basic PgBouncer setup with testing takes 1 business day. ProxySQL with read/write split and monitoring takes 2–3 days. If integration with an existing application and debugging prepared statements is needed, add 1 day. We guarantee that after setup, the number of concurrent connections will no longer be a bottleneck. Database server memory usage can be reduced by 80%. Contact us for a free audit—we'll help select the optimal solution for your load.