Senior LevelPHP 8+
Why does PHP-FPM struggle with database connection exhaustion, and how do you implement connection pooling using PgBouncer?
❓ Interview Prompt: Explain PHP-FPM connection exhaustion, PgBouncer / ProxySQL transaction pooling, multiplexing, and preventing database max_connections errors.
Because PHP-FPM uses a shared-nothing lifecycle, 500 active worker processes open 500 separate database TCP connections, quickly exhausting PostgreSQL's 'max_connections' limit and causing database crashes.
Database Proxy Connection Pooling:
Deploy PgBouncer (for PostgreSQL) or ProxySQL (for MySQL) between PHP-FPM workers and the database server:
•
Transaction Pooling: PHP-FPM workers connect to PgBouncer. PgBouncer multiplexes 500 PHP worker connections over just 20 physical database connections, assigning DB sockets only for the exact duration of an active transaction!
Code Implementation Example
ini
; PgBouncer Transaction Pooling Configuration (pgbouncer.ini)
[databases]
app_db = host=postgres-primary.internal port=5432 dbname=app_db
[pgbouncer]
listen_addr = 0.0.0.0
listen_port = 6432
auth_type = md5
auth_file = /etc/pgbouncer/userlist.txt
; Transaction Pooling mode multiplexes connections per transaction!
pool_mode = transaction
max_client_conn = 1000
default_pool_size = 20💡PgBouncer configuration file for transaction-level PostgreSQL connection pooling.
🎯 Interview Key Takeaway
Deploy PgBouncer or ProxySQL in transaction pooling mode to multiplex hundreds of PHP-FPM workers over a small pool of database connections.
#PgBouncer#ProxySQL#Connection Pooling#Databases#Senior
Back to all PHP 8+ Questions→