NODEJS Contents

Connection Pooling

Connection pooling is mandatory in production Node.js apps; it protects your database from overload and stabilizes latency under concurrency.

On this page

Why Connection Pooling Exists

Creating a new database connection per request is expensive and dangerous. Connection setup involves network handshakes, authentication, and resource allocation. Under load, this can overwhelm the database and exhaust file descriptors.

What a Pool Does

A connection pool maintains a limited number of open connections and reuses them across requests. This stabilizes throughput and prevents connection storms during traffic spikes.

Example with a Pool

import { Pool } from 'pg';

const pool = new Pool({
  max: 10,
  idleTimeoutMillis: 30000,
  connectionTimeoutMillis: 2000
});

const result = await pool.query('SELECT 1');

Production Tuning

  • Set max connections based on DB capacity
  • Monitor pool saturation and wait times
  • Avoid unbounded pools

Poor pool configuration is a common root cause of cascading failures.