We are experiencing high connection count alerts in our M30 MongoDB cluster, often reaching 95% of the max connections of 3000. Our setup uses AWS Lambda functions with Node.js and Mongoose as the ORM.
Key observations:
No global DB connection exists in Lambda; each invocation opens a new connection.
Connections are closed automatically if idle for 30s (maxIdleTimeMS) or after Lambda execution ends.
High spikes occur when multiple Lambda instances execute concurrently, leading to a rapid increase in connections.
We increased the cluster size to M40 but number of connections is still continues to rise over 3000.
const db = await getMainConnection(); // call the global connection function
// … do database operations
};
Despite following this pattern, we are still observing a very high number of concurrent connections (3–4.5K) at peak times. We are trying to understand why connections aren’t being fully reused or cleaned up as expected.
Would you have insights into why thousands of connections still accumulate even with the recommended global scope pattern?
Without knowing more about the workload itself and how many concurrent requests are in flight it’s difficult to answer this question. I wrote about how many connections MongoDB drivers may establish at How Many Connections is My Application Establishing to My MongoDB Cluster? | ALEX BEVILACQUA, which might provide some insight as there will always be at least 3 connections per Lambda instance to service a database operation (1 for the operation, 1 monitoring connection and 1 RTT connection)