Connecting to MongoDB while utilizing the connection within AWS Lambda functions

27 views Asked by At

I am currently working on optimizing the performance of an application implemented using AWS Lambda, Serverless, Node.js, and MongoDB. It's important to note that this system has nearly 40 Lambda handlers. Currently, we're using the following approach. However, this method doesn't align well with serverless architecture, resulting in MongoDB connection limits being reached when 15 to 20 users concurrently access the system:

    export async function connectToDatabase(connectionString) {
    try {
        const client = await MongoClient.connect(connectionString, {
            useNewUrlParser: true,
            useUnifiedTopology: true,
            maxIdleTimeMS: 450000,
            minPoolSize: 10,
            maxPoolSize: 100
        });

        // Select the database through the connection,
        // using the database path of the connection string
        const db = await client.db(new URL(connectionString).pathname.substr(1));
        
        return db;
    } catch (error) {
        console.error(error);
    }
}

After investigating, I found the cacheDB approach by referring to the following document: Serverless Development with MongoDB Atlas. This approach performed well compared to the previous one, allowing the system to handle 70 to 80 concurrent users:

let cachedDb = null;
export async function connectToDatabase(connectionString) {
    try {
        if (cachedDb) {
            return cachedDb;
        }   
        const client = await MongoClient.connect(connectionString);
        const db = await client.db();
        cachedDb = db;
 
        return db;
    } catch (error) {
        console.error('Error connecting to the database:', error);
        throw error;
    }
}

However, after further investigation, I found this alternative approach: Manage Connections in AWS Lambda with MongoDB.

const { MongoClient } = require('mongodb');
// MongoClient now auto-connects so no need to store the connect()
// promise anywhere and reference it.
const client = new MongoClient(process.env.MONGODB_URI);
module.exports.handler = async function () {
  const databases = await client.db('admin').command({ listDatabases: 1 });
  return {
    statusCode: 200,
    databases: databases
  };
};

In the 3rd approach I am getting the following error

"error": {
        "name": "MongoNotConnectedError",
        "location": "/var/task/src/getBankAccounts/webpack:/mongodb/lib/utils.js:391",
        "message": "MongoClient must be connected to perform this operation",
        "stack": "MongoNotConnectedError: MongoClient must be connected to perform this operation\n    at Object.j [as getTopology] 
}

I'd like to share the findings of my analysis after weeks of investigation to determine the most recommended method for connecting to MongoDB within AWS Lambda functions.

If anyone can assist me in identifying the most suitable approach, or if there's any other approach I haven't tried, please let me know. Your help would be greatly appreciated.

Thanks and Regards.

0

There are 0 answers