"Too many clients" in Azure Postgres

System Information
  • Strapi Version: 4.0.8
  • Operating System: Azure Linux web app and Windows 10
  • Database: Azure Postgres Flexible
  • Node Version: 16.13.1

We’re using Strapi as the CMS for our new website with a Postgres database, but we’ve been having some issues with there being too many active connections to the database. The error message we typically get is “error: too many clients”, or “error: remaining connection slots are reserved for non-replication superuser connections”. It happens both when developing locally (using one of two developer databases on the Postgres server), and when attempting to deploy to Azure (using a pre-prod database on the same server).

The database server in question has 4 vCores, 16GiB memory, and 6400 max iops. Whenever these errors have occured, there has been up to 53 active connections to the database, which is far more than there should be considering we only have 2 developers and 3 editors at most.

We tried adding the pooling object to our /config/database.js, but it hasn’t worked:

module.exports = ({ env }) => ({
  connection: {
    client: "postgres",
    connection: {
      host: env("DB_HOST"),
      port: env("DB_PORT"),
      database: env("DB_NAME"),
      user: env("DB_USERNAME"),
      password: env("DB_PASSWORD"),
      timezone: "Europe/Oslo",
      ssl: {
        rejectUnauthorized: false,
      },
    },
    pool: {
      min: 0,
      max: 50,
      acquireTimeoutMillis: 90000,
    },
  },
});

The issue eventually resolves itself, but we’re worried it will happen again when we go live with our new website since we don’t know what causes it in the first place. We’ve tried increasing the pool.acquireTimeoutMillis, and pool.max, and enabled pg_bouncer on the Postgres server. What other steps can we take to ensure it doesn’t happen again? Is it related to how many users are accessing the frontend website that fetches from Strapi?