0

I keep on getting the below error in an AWS Lambda with Node.js 16 + MongoDB v4, this usually happens for a lambda that has high traffic, other lambdas seem fine with the current setup.

MongoNetworkError: connection 6 to xx.x.xx.xx:xxxxx closed at Connection.onClose (/var/task/node_modules/mongodb/lib/cmap/connection.js:135:19)

MongoDB connection inside the lambda:

const MongoClient = require('mongodb').MongoClient;
const logger = require(''); const log = logger(__filename);

const getDbClient = async (uri) => {
  try {
    log.info('Connecting to Mongo client...');
    const dbClient = await MongoClient.connect(uri);
    log.info('Connected to Mongo client');
    return dbClient;
  }
  catch (err) {
    log.error('Error encountered connecting to database: ', err);
    throw err;
  }
};

module.exports = {
  getDbClient
};

The mongodb uri has an option of maxPoolSize=10 since I recently did an upgrade from MongoDB v3 to v4, and v4 has a maxPoolSize of 100 by default and v3 had it to 10. https://github.com/mongodb/node-mongodb-native/blob/HEAD/etc/notes/CHANGES_4.0.0.md#connection-pool-options

MongoDB hardware:

3 x M4.XLarge(4Core/16GB RAM)

This issue started happening after I upgraded MongoDB driver from v3 to v4 and stopped checking inside the lambda if there is an existing connection so I can use it because in v4 apparently this is done automatically.

I used to use: MongoClient.isConnected() from MongoDB v3.

Do you guys have any idea what could be the cause of this?

Nicolae Maties
  • 2,476
  • 1
  • 16
  • 26

1 Answers1

0

I don't have enough reputation to comment, so I'll just post an answer :D

I think your Lambda function is on extremely high traffic now, so it's even over the pool size of mongodb

Mink
  • 71
  • 7
  • By increasing the `maxPoolSize` value to 20 or even more causes more errors. Not really sure if this is the issue. – Nicolae Maties Aug 30 '22 at 09:05
  • obviously your errors come from the Mongo connection. I think the main problem is that your function is on very high traffic and making too many connections to Mongo – Mink Aug 30 '22 at 09:08
  • What if you increase maxPoolSize to 20 but your function is creating 40 connections to mongo? I think you will still get the same errors – Mink Aug 30 '22 at 09:15
  • I am using this at a big level, think about thousands of connections at a time. – Nicolae Maties Aug 30 '22 at 09:23
  • @NicolaeMaties you have thousand of connections while the maximum connections that mongo supports just 100... Lambda function has some mechanisms to reuse the database connection, have you tried that? – Mink Aug 30 '22 at 09:30
  • I am doing that already, reusing the connections otherwise I would have more errors and more frequently than now. – Nicolae Maties Aug 30 '22 at 09:32
  • Ok so your function is reusing the connections. Maybe your functions are simultaneously executed. "if 100 Lambda functions are simultaneously executed, 100 connections will be opened since each Lambda concurrency means different Containers respectively, having different memory area." as I found from this topic https://stackoverflow.com/questions/53974548/aws-lambda-and-database. I hope it will help – Mink Aug 30 '22 at 09:55
  • Btw, forgot to mention: "This issue started happening after I upgraded MongoDB driver from v3 to v4 and stopped checking inside the lambda if there is an existing connection so I can use it because in v4 apparently this is done automatically." – Nicolae Maties Aug 30 '22 at 10:23
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 04 '22 at 06:52