What is the meaning of the message "Connections % of configured limit has gone above 80"?

Hello
I want to know the meaning of this message

Connections % of configured limit has gone above 80

Hello @offers_awtar ,

Welcome to The MongoDB Community Forums! :wave:

Connections % of configured limit is an alert that occurs if the number of open connections to the host exceeds the specified percentage. In your case, this limit seems to be 80%.

For example: If we have set the maximum number of allowable connections to a MongoDB process to 100 and this connection percentage is set to 80, then, once the limit of 80 connections is reached, this alert will be triggered and once the connection limit of 100 is reached then no new connections can be opened until the number of open connections drops down below the limit.

To fix the problem immediately, one can restart the application which will terminates all existing connections opened by the application and allow your cluster to resume normal operations. In general, connection alerts could be a symptom of many things which includes but are not limited to

  • There are too many connections made by the applications and you might need to either upgrade your cluster or limit the connections made by the application.
  • Flaw in connection code in which connections are opened but never closed which could result in old connections to pile up and eventually exceed the connection limit. Additionally, you may need to implement some form of connection pooling.

Please go through Fix Connection Issues to learn more about this issue.

Regards,
Tarun

Thank you very much for the reply

Now I understand the problem, but I’m using MongoDB 5.0 and tire M10 and I think it’s 1500 max connections and the number of users is low.

Does that mean I have Flaw in connection code in which connections are opened but never closed ?

in this code? And how do I correct it?

import { MongoClient } from "mongodb";

/**
 * Global is used here to maintain a cached connection across hot reloads
 * in development. This prevents connections growing exponentiatlly
 * during API Route usage.
 * https://github.com/vercel/next.js/pull/17666
 */
global.mongo = global.mongo || {};
export async function getMongoClient() {
  if (!global.mongo.client) {
    global.mongo.client = new MongoClient(process.env.MONGODB_URI);
  }
  // It is okay to call connect() even if it is connected
  // using node-mongodb-native v4 (it will be no-op)
  // See: https://github.com/mongodb/node-mongodb-native/blob/4.0/docs/CHANGES_4.0.0.md
  await global.mongo.client.connect();
  return global.mongo.client;
}

export default async function database(req, res, next) {
  if (!global.mongo.client) {
    global.mongo.client = new MongoClient(process.env.MONGODB_URI);
  }
  req.dbClient = await getMongoClient();
  req.db = req.dbClient.db(); // this use the database specified in the MONGODB_URI (after the "/")
  return next();
}

You have to close connection using method provided in link with .close() method.

1 Like

As I am not a javascript expert, so I cannot confirm if this piece of code is or is not the root cause of the connection issue you are facing. However, it is possible that repeatedly running the script may lead to multiple copies of the script to open connections to the database and thus trigger the warning. Are you seeing any kind of pattern when this warning was triggered, e.g. an especially busy time, during certain development phase of the app, or other patterns that may coincide with the warning?

Also, for MongoDB, we generally recommend not to open/close connections after every use instead use connection pooling to maintain a cache of open, ready-to-use database connections maintained by the driver. Your application can seamlessly get connections from the pool, perform operations, and return connections back to the pool. Connection pools are thread-safe.

Please refer this documentation for more information on connection pooling.

Tarun

Thank you very much for your help

I think the problem was really in the connection string
After I changed it to the following method, I no longer received that alert

import { MongoClient } from "mongodb";

export async function getMongoClient() {
  /**
   * Global is used here to maintain a cached connection across hot reloads
   * in development. This prevents connections growing exponentiatlly
   * during API Route usage.
   * https://github.com/vercel/next.js/pull/17666
   */
  if (!global.mongoClientPromise) {
    const client = new MongoClient(process.env.MONGODB_URI);
    // client.connect() returns an instance of MongoClient when resolved
    global.mongoClientPromise = client.connect()
  }
  return global.mongoClientPromise;
}

export async function getMongoDb() {
  const mongoClient = await getMongoClient();
  return mongoClient.db();
}

Thanks

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.