Connection pooling with PgBouncer and ProxySQL for web application

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.

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.

Showing 1 of 1 servicesAll 2065 services
Connection pooling with PgBouncer and ProxySQL for web application
Medium
~2-3 business days
FAQ

Our competencies:

Development stages

Latest works

  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1171
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1094
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    831
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    879
  • image_website-sbh_0.png
    Website development for SBH Partners
    999
  • image_website-_0.png
    Website development for Red Pear
    453

Connection Pooling: PgBouncer and ProxySQL

Database connections are expensive resources: each costs RAM (thread stack, buffers), requires authentication, opens file descriptor. Web application with 100 concurrent users × 10 requests per user = 1000 concurrent connections × 100-200 KB per connection = 100-200 MB just for connection overhead.

Connection pooling sits between application and database: application connects to pool, pool maintains 50 real connections to database. Result: 1000 app connections become 50 DB connections.

PgBouncer: PostgreSQL Connection Pool

Installation:

apt-get install pgbouncer

Configuration /etc/pgbouncer/pgbouncer.ini:

[databases]
mydb = host=localhost port=5432 dbname=mydb

[pgbouncer]
listen_port = 6432
listen_addr = 127.0.0.1

# Pool modes
pool_mode = transaction

# How many connections to keep to backend
min_pool_size = 10
default_pool_size = 25
max_pool_size = 100

# Connections to application (clients)
max_client_conn = 1000

# Cleanup idle connections
idle_in_transaction_session_timeout = 300000  # 5 min
server_idle_timeout = 600  # 10 min to backend

# Logging
logfile = /var/log/pgbouncer/pgbouncer.log
loglevel = info

# Stats
stats_period = 15

Pool modes:

  • session — connection to backend lasts as long as client connection
  • transaction — connection back to pool after each transaction (recommended for web)
  • statement — connection back after each statement (strictest, rarely used)

Monitoring:

# Connect to admin console
psql -U pgbouncer -d pgbouncer -p 6432 -h localhost

# View stats
SHOW stats;
SHOW clients;
SHOW servers;

# Reload config without restart
RELOAD;

ProxySQL: MySQL Connection Pool and Query Routing

Installation:

apt-get install proxysql

Configuration via admin interface:

-- Connect to admin interface
mysql -u admin -p 'admin' -P 6032 -h 127.0.0.1

-- Add backend servers
INSERT INTO mysql_servers (hostgroup_id, hostname, port, weight, max_connections) VALUES
  (0, 'primary-db-1', 3306, 1000, 100),
  (1, 'replica-1', 3306, 1000, 100),
  (1, 'replica-2', 3306, 1000, 100);

-- Query rules: route SELECT to replicas
INSERT INTO mysql_query_rules (rule_id, match_pattern, destination_hostgroup, active) VALUES
  (1, '^SELECT ', 1, 1),  -- hostgroup 1 = replicas
  (2, '.*', 0, 1);        -- everything else = primary (hostgroup 0)

-- Apply
LOAD MYSQL SERVERS TO RUNTIME;
LOAD MYSQL QUERY RULES TO RUNTIME;
SAVE MYSQL SERVERS TO DISK;
SAVE MYSQL QUERY RULES TO DISK;

-- Monitor
SHOW STATS;

Application connects via:

$pdo = new PDO(
    'mysql:host=127.0.0.1;port=6033;dbname=mydb',
    'app_user',
    'password'
);