Connection refused because there are too many open connections

I am currently seeing the below issue in my M20 Atlas cluster, anyone experienced this before what how can i stop this

{“t”:{"$date":“2022-06-13T03:19:27.485+00:00”},“s”:“I”, “c”:“NETWORK”, “id”:22942, “ctx”:“listener”,“msg”:“Connection refused because there are too many open connections”,“attr”:{“connectionCount”:3365}}
{“t”:{"$date":“2022-06-13T03:19:27.533+00:00”},“s”:“I”, “c”:“NETWORK”, “id”:22942, “ctx”:“listener”,“msg”:“Connection refused because there are too many open connections”,“attr”:{“connectionCount”:3365}}
{“t”:{"$date":“2022-06-13T03:19:27.533+00:00”},“s”:“I”, “c”:“NETWORK”, “id”:22942, “ctx”:“listener”,“msg”:“Connection refused because there are too many open connections”,“attr”:{“connectionCount”:3365}}
{“t”:{"$date":“2022-06-13T03:19:27.621+00:00”},“s”:“I”, “c”:“NETWORK”, “id”:22942, “ctx”:“listener”,“msg”:“Connection refused because there are too many open connections”,“attr”:{“connectionCount”:3365}}
{“t”:{"$date":“2022-06-13T03:19:27.670+00:00”},“s”:“I”, “c”:“NETWORK”, “id”:22942, “ctx”:“listener”,“msg”:“Connection refused because there are too many open connections”,“attr”:{“connectionCount”:3365}}

Hi @Maanda_Ambani and welcome in the MongoDB Community :muscle: !

M20 can only handle 3000 connections per node and apparently you have reached this limit. Upgrading to an M40 would upgrade the support to 6000 connections per nodes.

But if you don’t expect that many connections, there may be something wrong with the way you are handling your connections (opening too many or not closing them properly).

Usually I see this error when people use serverless functions and they don’t cache the MongoDB connection so each function calls are re-using the same cached connection pool. In this scenario, it’s a bad idea to create a new MongoClient for each serverless function calls.