question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extremely high number of connections on MongoDB Atlas with serverless lambda infrastructure

See original GitHub issue

Prerequisites

  • I have written a descriptive issue title

Mongoose version

6.3.4

Node.js version

16.15.0

MongoDB version

6.0.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

No response

Issue

I am following the guide available at https://mongoosejs.com/docs/lambda.html to avoid multiple function calls creating new connections. In particular, I am using the following method to cache my DB connection:

const mongoose = require('mongoose');

let conn = null;

const uri = 'YOUR CONNECTION STRING HERE';

exports.connect = async function() {
  if (conn == null) {
    conn = mongoose.connect(uri, {
        connectTimeoutMS: 10000,
        maxPoolSize: 10,
        serverSelectionTimeoutMS: 5000,
        bufferCommands: false, // Disable mongoose buffering
    }).then(() => mongoose);
    
    // `await`ing connection after assigning to the `conn` variable
    // to avoid multiple function calls creating new connections
    await conn;
  }

  return conn;
};

However, I am seeing extremely high number of connections on MongoDB Atlas with this setup. A typical alert looks like:

The connections to your cluster(s) have exceeded 500, and is nearing the connection limit

I have upgraded my MongoDB Atlas instance to something with more capacity (1500 max connections). I did a load test of simulating a few operations through my APIs and the connections were reaching 1000+ for just a small load.

More details

A typical lambda handler looks like this:

export const handler = async (event: APIGatewayProxyEventV2WithJWTAuthorizer) => {
  // perform some operations
  return {
    statusCode: 200,
    body: JSON.stringify({
        message: 'Go Serverless v1.0! Your function executed successfully!',
        input: event,
    }),
  }
}

Can someone point me in the right direction as to how I can cache the connections? Do I need to modify anything in the handler or the mongoose configs?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
vkarpov15commented, Aug 24, 2022

Great suggestions from @jeanbmar . I agree that, if you get 20k concurrent requests, you’ll probably run out of connections. Each Lambda instance will create a new connection pool, so even if you reduce maxPoolSize to 1 in your example, you’ll only be able to handle 750 concurrent requests. You need some sort of queueing infrastructure to make sure you don’t go over that.

0reactions
harishv7commented, Aug 10, 2022

@jeanbmar That’s helpful, thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optimizing AWS Lambda With MongoDB Atlas & NodeJS
Learn how to leverage AWS Lambda caching capabilities and improve query performance by re-using database connections to MongoDB Atlas in ...
Read more >
MongoDB Atlas with AWS Lambdas - Connection limit issue?!
It seems like using mongoose + lambda is not a scalable option, Given the connection limit of MongoDB cluster (M10 - 1500 connections)....
Read more >
High number of connections : Serverless App / MongoDB Atlas
I am developing a website with the following stack : React.js, Next.js, Node.js; the website is hosted on Vercel (which rely on AWS...
Read more >
Solving invisible scaling issues with Serverless and MongoDB
Using Lambda with MongoDB Atlas. Here's a simple code snippet for you to check out. // db.js const mongoose = require('mongoose') const ...
Read more >
Using AWS Lambda to Create a MongoDB Connection Proxy
So, after some testing, by checking the MongoDB Atlas dashboard, we've noticed that the total number of established database connections really ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found