Database connection limit

Hi, I’m new to serverless
I’m using Netlify Serverless Functions(which uses AWS Lambda) and my database is MongoDB. I was using Heroku express server until I was getting a lot of traffic and things are very slow so I tried to switch to serverless.
I have over 8500 users and my app gives users alerts for restocks. So regularly there isn’t as many traffic but during restock time, it spikes a lot. Some users still use the app version that uses Heroku, some use the new version that use serverless
My issue is that I have cached my connection (code attached, it seems to reuse sometimes based on the logs) but I’m still reaching MongoDB limit of 500 connections (it’s a free database tier). Without high-traffic hours, the connections vary between 250-450 connections but it doesn’t go back to lower than 100. I understand I have people sign up and use the app as well but I wonder if this is a normal behavior. Netlify Serverless function told me yesterday I had over 70,000 requests. I was surprised because of that.
So I guess my question is

  • Is serverless still the right choice if I have a small budget. I’m just a dev and I paid everything out of my pocket. I’ve attempted to upgrade MongoDB to the next tier which allows 1500 max connections but I still don’t know if my code is right/if I’m doing anything wrong that makes it end up being 500 connections. and will 1500 is enough for thousands of users.

Connection code

const mongoose = require("mongoose");

let cachedMongooseDb = null;
async function connectToDB() {
   const uri =
     process.env.NODE_ENV === "production"
      ? `mongodb+srv://${}:${}@<dbname>/<collection>?retryWrites=true&w=majority&maxIdleTimeMS=300000`
       : "mongodb://";
  if (cachedMongooseDb && cachedMongooseDb._readyState === 1) {
    console.log("db cached");
    return cachedMongooseDb;
  } else {
    console.log("----db NOT cached");
    const connect = await mongoose.connect(uri, {
      useNewUrlParser: true,
      useFindAndModify: false,
      useCreateIndex: true,
      useUnifiedTopology: true,
    cachedMongooseDb = connect.connection;

    console.log("--Connected to the db--");
    return cachedMongooseDb;

(whether I have maxIdleTimeMS=300000 or not, it still behaved the same way where it generates over 500 connections)
Hopefully, I could have some guidance. I’ve not been able to find an answer for my question, lots of articles said serverless is costly if you have a larger application. However I don’t think my application that larger and I know other bigger companies use serverless (I know they have money to scale). Which is back to my question of whether this is the right choice. Thank you very much.

Hey did you manage to solve the issue I am facing the same problem with the connection limit

We use AWS to run PostgreSQL on RDS service + serverless framework to deploy a bunch of Lambdas. A while back we faced a similar problem when multiple clients were triggering lambdas which in turn were making connections to DB and were depleting the pool.

We ended up with RDS Proxy solution managed by AWS. It looks like a 24/7 running EC2 instance (in my understanding) that works in between Lambdas and RDS and queues incoming connection requests such that it can maintain a fixed number of connections to RDS itself.

You might want to research if Netlify has a similar managed service and try using it. Alternatively, you could deploy a 24/7 running service that will do exactly that. It should be straightforward to implement connection queueing I guess.