AWS Lambda Invocations Trigger MongoDB Connection Spikes

Hi,

We use AWS Lambda Functions to connect Mongo cluster with these settings:

const defaultConnectionOptions: mongoose.ConnectOptions = {
  // socketTimeoutMS: 30_000,
  maxIdleTimeMS: 30_000,
  minPoolSize: 0,
  maxPoolSize: 5,
};

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:

  1. No global DB connection exists in Lambda; each invocation opens a new connection.
  2. Connections are closed automatically if idle for 30s (maxIdleTimeMS) or after Lambda execution ends.
  3. 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.

Could you please advise or help me on this?

@Alparslan_Ozkan, just a quick note but have you seen the guidance in Manage Connections with AWS Lambda - Atlas - MongoDB Docs? Are you creating the MongoClient instance outside the function handler?

Hi, thanks for pointing that out @Alex Bevilacqua !

Yes, I’ve reviewed the MongoDB guidance on “managing connections with AWS Lambda”. In our setup is like this:

let mainConnection: mongoose.Connection | null = null;
const clientConnectionCache = new Map<string, CachedConnection>();

const defaultConnectionOptions: mongoose.ConnectOptions = {
  maxIdleTimeMS: 30_000,
  minPoolSize: 0,
  maxPoolSize: 5,
};

And as you see :

  • The connection objects (mainConnection and clientConnectionCache) are declared in the global scope**, outside the Lambda handler.

  • Our getMainConnection and getClientConnection functions check for existing connections and reuse them if available.

  • Inside the handler, we “just call these functions” ; we don’t create new connections there.

  • context.callbackWaitsForEmptyEventLoop is also set to false to allow reuse between invocations as below in our db connection source code:

exports.handler = async function(event, context) {
context.callbackWaitsForEmptyEventLoop = false;

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?

Continuing the discussion from AWS Lambda Invocations Trigger MongoDB Connection Spikes:

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)