7

I have been working on a React application that would connect to MongoDB Atlas with Mongoose. Using nodemon and webpack for hot reloading it would reconnect to Mongo within milliseconds every time there was a reload. It was working nicely like this for months, then suddenly began taking 20+ seconds to connect on every reload - which has significantly increased dev time.

I cannot figure out what caused this to start happening and how to fix it. Any advice would be greatly appreciated.

Note: My IP Address has already been whitelisted.

const db = "mongodb+srv://<username>:<password>@cluster0-omitted.mongodb.net/<dbname>?retryWrites=true&w=majority"

const connectDB = async () => {
  try {
    console.log('Attempting to connect to MongoDB...');
    await mongoose.connect(db, {
      useNewUrlParser: true,
      useCreateIndex: true,
      useFindAndModify: false,
      useUnifiedTopology: true,
    });
    console.log('MongoDB connected...');
  } catch (err) {
    console.log(err.message);
    process.exit(1); // Exit process with failure
  }
};
mschwartz
  • 188
  • 2
  • 6
  • 2
    You will need to debug. Localise the problem first. Try to connect to the atlas from mongo shell `mongo "mongodb+srv://cluster0-omitted.mongodb.net/" --username --verbose` If it is fast, the problem lies somewhere in nodejs. If the shell is slow too, please post the output here. – Alex Blex Feb 02 '21 at 10:08
  • Thanks Alex! I connected from the mongo shell and it was perfect. I still do not know why using the above connect URI is resulting in a slow connection, but I was able to get around this because when connecting through the shell it logged the specific URI it was using to connect and so I replaced the mongodb+srv:// URI in my app with the one that the shell used and now my app is connecting to MongoDB very quickly again on reloads. I am still very curious why the other URI is slow, but at least this workaround has allowed me to resolve the issue. – mschwartz Feb 04 '21 at 02:23
  • How the urls are different? – Alex Blex Feb 04 '21 at 02:28
  • 1
    It follows the standard URI connection scheme `mongodb://:@cluster0-shard-omitted.mongodb.net:27017,cluster0-shard-omitted.mongodb.net:27017,cluster0-shard-omitted.mongodb.net:27017/?authSource=admin&ssl=true&retryWrites=true&w=majority` I could be wrong but this seems to suggest the bottleneck is the step where the client connection string is retrieved from the DNS Server. I will have to do some further reading to see if I am able to modify the SRV and TXT records or if there are other steps I should take to remove whatever impediment is causing the delay. – mschwartz Feb 04 '21 at 14:01
  • I would report to Atlas then. You don't have control on SRV records. It's part of the Atlas hosted db offer. Try to exclude nodejs from the equation. If you collect connection logs using command line `mongo` with `--verbose` mode it should give Atlas support enough information to reproduce or at least investigate. – Alex Blex Feb 04 '21 at 17:22
  • Interesting update. My TV was having issues connecting to certain streaming services. Netflix/Prime were unaffected, but CraveTV and Disney+ were throwing errors. While troubleshooting this for my wife I rebooted our router and wondered if this would also resolve my MongoDB issues and sure enough it did! I'm back with the original mongodb+srv:// uri and it's as quick as before. I never thought to reboot my router because other than Mongo and the issues with CraveTV my internet connection has been perfect. I'll have to read up what changes occured during the router reboot that fixed the issue. – mschwartz Feb 04 '21 at 19:22

3 Answers3

6

I was having this same issue, MongoDB Compass taking 10+ seconds to connect, suddenly all my tests started to fail and hot reload while in local dev server was taking 10+ seconds..

I had recently moved to a new MacBook Pro M1 device, and my previous laptop was connecting almost instantly.

Eventually I figured out I was missing 8.8.8.8 in my network settings.

Mac System Preferences > Network > Wi-Fi > DNS

Liger
  • 140
  • 2
  • 4
1

As ridiculous as it sounds, reboot your router. Or more technically, you have a problem with DNS and rebooting can resolve it.

I had a similar problem after a power failure at home. When the NAS came back not all Docker containers came back up, including my pihole. DNS seemed fine elsewhere so I didn't notice, but MongoDB Atlas specifically took 20 seconds to connect. After starting the Pihole docker container, it now connects in 600ms, which is much closer to normal.

DOOManiac
  • 6,066
  • 8
  • 44
  • 67
0

I don't know if this helps you out but have you tried to change IP allowed list to all and try it again. I had the same issue where I had assigned my network IP to allowed list. After changing it to allow all IP in MongoDB dashboard it seems to have solved the problem.