High traffic on Netlify Serverless/Mongodb reached max connections

  • Netlify site name: hardcore-wiles-fbe1f0.netlify.app
  • Custom domain: https://app.squishalert.com/ , Native IOS/Android App
  • Issue: My mobile app is getting around a few thousands of requests (I have 8000+ users) at some point and I notice my Mongodb connection (I’m using a free cluster right now) is reaching 500 connections which causes timeout issue.
    I’m new to serverless. I’m just here to ask whether this a normal behavior that Netlify serverless is generating over 500 database connections. When there isn’t a lot of traffic, I still get over 400 open connections. I do cache my mongo connection (code attached) but I understand that I’m getting thousands of requests so I wonder if it’s the right behavior that Netlify serverless is generating a lot of connections or my caching/logic for Netlify serverless is incorrect at some point. What is the maximum request can one Netlify function container handle. I’m using Netlify function free tier (level 0)

Thank you very much

const mongoose = require("mongoose");
let cachedMongooseDb = null;

async function connectToDB() {
  const uri = "mongodb://";

  if (cachedMongooseDb && cachedMongooseDb._readyState === 1) {
    return cachedMongooseDb;
  } else {
    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;

Hi there, @babyrusa :wave:

Thanks so much for reaching out! It looks like this thread has been quiet for a few days-- thanks for your patience. Are you still encountering this number of connections? Has anything changed in the past 9 days? Please share a brief update if you are able to.

Hi Hillary,
I wasn’t able to solve it and I was running live so I decided to move off netlify serverless functions so that didn’t impact the users while working on finding a solution.