Node.js and PostgreSQL: Connection Pooling implementing pg-pool on High Load
I'm having a hard time understanding I just started working with I'm trying to configure I've tried everything I can think of but I'm experiencing unexpected behavior when using the `pg-pool` from the 'pg' library to manage connections to my PostgreSQL database in a Node.js application... The app is built using Node.js v16.13.0 and the 'pg' library version 8.7.1. I have set up a connection pool with a maximum of 10 connections, but under heavy load, I start to notice that requests occasionally time out with the behavior: ``` behavior: timeout of 5000ms exceeded ``` Iโve tried adjusting the `connectionTimeoutMillis` and `idleTimeoutMillis` settings in the pool configuration, but it hasnโt resolved the scenario. Hereโs how I set up the pool: ```javascript const { Pool } = require('pg'); const pool = new Pool({ user: 'myuser', host: 'localhost', database: 'mydb', password: 'mypassword', port: 5432, max: 10, connectionTimeoutMillis: 2000, idleTimeoutMillis: 30000 }); ``` I am making multiple concurrent database queries with the following code: ```javascript async function fetchData(queryText) { const client = await pool.connect(); try { const res = await client.query(queryText); return res.rows; } catch (err) { console.behavior('Query failed', err); throw err; } finally { client.release(); } } ``` When the load increases, some of the queries are timing out while others succeed. I verified that the database is not under heavy load and can handle more connections. I also implemented logging for each request, and they seem to queue up when the pool hits the limit. Could this be an scenario with how I'm managing the connections, or is there something specific I need to consider when dealing with high concurrency? Any best practices or configurations that might help alleviate these timeout issues would be greatly appreciated. I've been using Javascript for about a year now. What's the best practice here? This is my first time working with Javascript LTS. For context: I'm using Javascript on Ubuntu 20.04. Has anyone else encountered this? The project is a microservice built with Javascript. What's the best practice here?