Maximum connections threshold for MongoDB with Netlify Functions

Hi I am building this API called https://labsensorapi.netlify.app

It is a api that is going into the sensor network that will receive requests from the website (adding alarms, deleting devices, configuring devices) as well as recieve requests form the localNode.js that will request data to be added to the database and other calls

Additionally, we are working with an Atlas MongoDB database to store all of the sensor data and device information.

While testing the network with one sensor, we quickly got this error from mongodb that says: “[Alert] You’re nearing the maximum connections threshold.” Basically this means too many programs are trying to connect to the database.

One option is to upgrade our database but the second option is to somehow improve our API to create less connections with the database.

I was looking at this post on MongDB about a solution but post was directly about AWS lambda functions, rather than netlify functions.

For more information about database connections I found this link to be helpful

If anyone has any suggestions about how to limit the amount of connections that netlify functions make with the database, please let me know.

The ideal solution for you would be to host your backend elsewhere that can run a Node.js server indefinitely instead of having to spin-up these micro-services every time a request is made This way, you can ensure:

  • You get a single IP to allow connections to your database
  • You can control the number of connections as only the server would be connected to the database, so it should always be just 1.

I’m not sure about your setup, but basically when you’d invoke a Lambda function, unless you’ve initialized the client outsite the handler() function, it would create a new client for every invocation. This is how the flow would be:

  • Request is made to your backend and the function is invoked
  • If the function was recently invoked, it might be “warm” and thus AWS would not re-initialize it. It would simply re-run the contents of the handler() function and return the response.
  • If the function was not invoked for some time and the container has been terminated, it would be a “cold” invocation and AWS will run the entire function including the initialization again.

This is basically what the MongoDB guide for AWS that you linked to, says. It is therefore recommended to initialize your db connection outside the handler function. This is not a fool-proof solution and might or might not help your case. If you’re already doing this and still running into the problem, I’m afraid there’s not much we can offer from our end to solve it.

You can consider upgrading the database or follow the first part of my response.