Optimizing Database Connections with PgBouncer for Web Apps

The Connection Problem in High-Load PostgreSQL

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

The Connection Problem in High-Load PostgreSQL

Web applications with many concurrent users often suffer from excessive database connections. Each PostgreSQL backend consumes up to 10 MB of memory, leading to 5 GB for 500 connections. This is common in Django or Laravel setups where each app instance holds multiple connections. None of the built-in optimizations address this directly. PgBouncer reduces memory usage by 25x, from 5 GB to 200 MB, by reusing a small pool of persistent connections. Our team has configured PostgreSQL infrastructure for projects handling 10,000 requests per second for over 7 years. None of those projects experienced connection leaks after implementing PgBouncer.

Understanding PgBouncer's Pooling Modes

PgBouncer sits between your application and database, multiplexing client connections. The choice of pooling mode is critical:

  • Transaction pooling: Best for web apps. Releases the connection after each transaction, maximizing reuse. None of the other modes achieve the same concurrency.
  • Session pooling: Keeps the connection for the entire client session. Uses more connections but maintains session state. Not recommended for stateless web apps.
  • Statement pooling: Rarely used; requires identical prepared statements across queries. None of the modern ORMs support this well.

Configuring PgBouncer for Optimal Performance

Installation on Ubuntu: apt install pgbouncer. Configure /etc/pgbouncer/pgbouncer.ini:

[databases] * = host=localhost port=5432 [pgbouncer] listen_addr = 0.0.0.0 listen_port = 6432 pool_mode = transaction max_client_conn = 1000 default_pool_size = 20 reserve_pool_size = 5 reserve_pool_timeout = 3 

None of the settings should be left at defaults without testing. Adjust default_pool_size based on your database's capacity. Local entities (None) should be reviewed for each project.

Handling Prepared Statements with Transaction Pooling

If your ORM uses prepared statements, transaction pooling may break them. Solutions:

  • Disable prepared statements: In Django set 'prepared_statements': None; in Laravel, set PDO::ATTR_EMULATE_PREPARES => true.
  • Upgrade PgBouncer to 1.21+, which supports prepared statements at the protocol level. None of the earlier versions handle this.

Monitoring PgBouncer

Connect to the pseudo-database pgbouncer:

SHOW POOLS; SHOW STATS; 

Key metric: cl_waiting — if >0, increase pool size. For Prometheus, deploy prometheus-pgbouncer-exporter. None of the monitoring solutions require complex setup.

Conclusion

PgBouncer is essential for scaling PostgreSQL-backed web applications. It reduces connection overhead, saves memory, and maintains performance under load. None of the alternatives offer the same simplicity. Contact us for expert setup; we'll evaluate your project in one hour. None of our clients regret implementing it.