Slow connection to the server using NodeJS driver

Hello mongo gurus,

I recently signed up for the mongo atlas to eliminate some of the operational challenges of my own hosted/self managed community edition.

My app seems operational after changing the driver and connection string in uri (mongo+srv) format.

However, it takes close to 25 seconds to establish the initial connection. **That's really slow.**

Any help to improve the performance of the initial connection would be greatly appreciated.

Here my stack.

Node.js - v10.18.1

driver - “mongodb”: "^3.6

connection code

const MongoClient = require('mongodb').MongoClient;
url =  `mongodb+srv://${process.env.mongo_user}:${process.env.mongo_password}@${process.env.mongo_host}/${process.env.mongo_db}?retryWrites=true&w=majority`

 MongoClient.connect(url,
    {
      useUnifiedTopology: true,
      useNewUrlParser: true
    }).then((client) => {

      console.log("mongo db conection success");
    
    }).catch(err => {
      console.log("connection failure.... ", url);
      console.log("connection errored ", err);
    })

thank you,
Jag

Hi Jag,

I am having the same issue. However for me this started happening suddenly with no clear explanation. I am using nodemon for hot reloading. For months I have been working on an application that would connect to MongoDB using mongoose and every time there was a reload it would reconnect to Mongo within milliseconds. Now it has consistently been taking 20+ seconds to connect on every reload.

I cannot figure out what caused this to start happening and how to fix it. If anyone has any advice, it would be greatly appreciated.

Same here, connection to database is very slow and await doc.save() is not finishing

Can you test the connection with Compass and the shell to see if there are similar slow downs to the Node.js driver? This will eliminate the driver as the point of failure?

I’m having the same issue. I have 2 pc, on the first one the connection take less than 1s, one the 2nd one, the connection take 20s … they are connected to the same network and using the same version of node. 2 days ago I had no issue.
I have try to connect with mongoDB compass on windows 10 after I enter the url, the app is loading forever … (I’ve try to connect several times)

Hi all,

Hopefully this will help:

1 Like

Thanks, indeed using the url given when using the mongo shell seems to work.

Try rebooting your router. This is what fixed the issue for me.

When using SRV the driver initiates a lookup that resolves a single hostname to the actual names of the hosts, this is the step that was causing the issue for me. I still do not know why my router was interfering with the SRV lookup specifically - my internet connection was otherwise perfect. I would love if someone could elaborate on why rebooting my router resolved this issue?

1 Like

As ridiculous as it sounds, rebooting my router did fix the issue for me. More specifically, DNS was messed up. After restarting my router and my Pihole docker container, DNS issues resolved (I hadn’t noticed having any anyway), and it no longer takes me 20 seconds to connect to a MongoDB Atlas instance.

I tried different things:

  • Restarting my router
  • Troubleshooting by connection via mongo shell
    Connection via mongo shell, it was slightly faster, but still over 10 seconds.

What worked for me, was that I changed my network settings use Google Public DNS, instead of my ISP’s DNS servers (I have a cheap ASUS router). After that I am now connection to my MongoDB within 2-4 seconds, which is about 10 times faster than before.

Here is how to do it: Get Started  |  Public DNS  |  Google Developers

You don’t need to reboot your router none of that, you just need a VPN im using Urban VPN Desktop its free and its an easy fix

Every query, that I run in my application it is taking an extra overload of 250-300ms don’t know why! I have my mongodb and server both hosted on AWS. There are security groups monitoring the connection. Our both hosted ones are t2.medium. The query when I am running on Mongodb Compass, is taking 0ms, but the same query through application, 300ms. Please help resolve it.

Adding the DNS servers, as described in the StackOverflow answer you referenced, also solved the issue for me. Thanks, it sure was an annoying problem.