Sobes.tech
Middle

Deploying code, the database is fine, the processor is not loaded, but requests via REST API are executed sequentially as if one after another. What could be wrong with the database connection configuration?

sobes.tech AI

Answer from AI

If REST API requests are executed sequentially, even though the CPU is not loaded and the database is fine, the likely reason is an insufficient number of connections in the database connection pool.

In Node.js, a connection pool is often used for database operations. If the pool is configured with a minimum number of connections (for example, 1), requests will be processed sequentially, waiting for a connection to be released.

What to check and fix:

  • Increase the size of the connection pool.
  • Ensure that connections are not blocked by long transactions.
  • Check that database requests are executed asynchronously and do not block the event loop.

Example of configuring a pool for PostgreSQL using pg:

const { Pool } = require('pg');
const pool = new Pool({
  max: 20, // maximum number of connections in the pool
  idleTimeoutMillis: 30000,
  connectionTimeoutMillis: 2000,
});

If the pool is too small, requests will wait in line, leading to sequential execution.