✅ Why Connection Pooling?

Without pooling:

Every DB query → New connection → Handshake → Auth → Query → Close → EXPENSIVE.

With pooling:

A fixed number of persistent DB connections are reused.

This:

Benefit Result
Avoids DB overload Prevents crashing DB
Faster queries No handshake each time
Scales predictably Stable under high traffic

✅ Configure Connection Pooling in Sequelize

src/Db/db.index.js

import { Sequelize } from "sequelize";
import logger from "../utils/logger.js";

const sequelize = new Sequelize({
  dialect: "postgres",
  host: process.env.DB_HOST,
  username: process.env.DB_USERNAME,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_DATABASE,
  port: Number(process.env.DB_PORT),
  logging: process.env.NODE_ENV === "production" ? false : (msg) => logger.debug(msg),
  benchmark: true,
  pool: {
    max: 10,         // Maximum connections
    min: 2,          // Minimum connections
    acquire: 30000,  // Max wait time for free connection (ms)
    idle: 10000,     // Time before releasing idle connection
  },
});

export { sequelize };


🧠 What Pool Values Mean?

Key Meaning Example
max Highest number of open DB connections 10 concurrent queries
min Keep these connections open always 2 idle standby
acquire Timeout before failing with DB overloaded error 30s
idle Close idle connections after this duration 10s

✅ How to Set Optimal Pool Values?

Environment Max Min
Local dev 3 1
Staging 6 2
Production (Single Node server) 10–15 4
Horizontal Scaling / Kubernetes Divide max by replicas Example below
If you have 3 backend replicas in K8s & DB max-connections = 30:
Each server gets pool.max = 10

This prevents DB overload.