Connection pooling — reuses database connections instead of creating new ones for each request.
Why it matters:
PostgreSQL with pg:
1const { Pool } = require("pg");23const pool = new Pool({4 host: "localhost",5 port: 5432,6 database: "mydb",7 user: "admin",8 password: "secret",9 max: 20, // max connections10 idleTimeoutMillis: 30000,11 connectionTimeoutMillis: 2000,12});1314// Use pool15const result = await pool.query("SELECT * FROM users");1617// Get a client for transactions18const client = await pool.connect();19try {20 await client.query("BEGIN");21 await client.query("INSERT INTO users VALUES ($1)", [name]);22 await client.query("COMMIT");23} catch (err) {24 await client.query("ROLLBACK");25 throw err;26} finally {27 client.release();28}
Key points: