MongoDB Keeps Timing out on connect wihich is causing our server to crash

I have a NodeJs API deployed to cyclic.sh that makes use of MongoDB Atlas Database. I have over 30k users and at least 1 million API Reqs per day. But have been getting this error


So for 6 secs my entire server is down and no request can coming in and my users can’t interact with my server. Thought it was from my hosting provider but after talking to their support it was pointed out that since connecting to mongodb was the only thing i was doing before server initialization the problem was definitely from mongodb.

This how I’m connecting to my DB

const connectDB = async () => {
  try {
    const conn = await mongoose.connect(process.env.MONGO_URL, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      // ssl: true, // Use SSL
      // tlsAllowInvalidCertificates: true, // Be cautious about using this in production
      // maxPoolSize: 1000, // Use a reasonable pool size
      // serverSelectionTimeoutMS: 60000, // Timeout for server selection
      // socketTimeoutMS: 60000, // Socket timeout
    });

    console.log(`MongoDB Connected: ${conn.connection.host}`);
  } catch (error) {
    console.error("MongoDB connection error:", error);
  }
};

connectDB().then(() => {
  app.listen(PORT, () => {
    console.log(`App listening on port ${PORT}`);
  });
});

I’m using a M10 cluster, I thought that would be able to handle my application workload. I’m at my wits end, any help would be appreciated.

“I encountered a similar situation, and I succeeded by adding 0.0.0.0/0 to the network access in MongoDB. Wish you success!”