
Database Connection Pooling and High-Concurrency Handling in Node.js
Under sudden traffic bursts, Node.js applications that create database connections on the fly can quickly exhaust PostgreSQL max_connections, leading to cascading gateway timeouts.
Here are the best practices we used to keep database connections stable and resilient under heavy concurrent loads.
Architecture & Configuration
-
PgBouncer in Transaction Pooling Mode: Allows thousands of client connections to share a small pool of 50–100 actual PostgreSQL backend processes.
-
Pool Tuning in Node.js:
const { Pool } = require('pg');
const pool = new Pool({
host: process.env.DB_HOST,
port: 5432,
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
max: 20, // Max clients in local process pool
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000,
});- Circuit Breakers: Implementing graceful backoff and read-replica fallbacks when primary pool latency crosses established thresholds.
Comments