Getaddrinfo EAI_AGAIN pinliv-lb.inemg.mongodb.net

Hey,

I’m using MongoDB Serverless with for my Node.js hosted on a lightsail server. Rarely like once in a week or two, I find errors in the logs which look like this
getaddrinfo EAI_AGAIN xxxx-yy.zzzzz.mongodb.net
Looks like the connection to the db has been lost, I’ve been tackling w this issue for a while now, so I added listeners on disconnect and error events to try to reconnect in 2secs, but I can see it wasn’t of much help as its the same error for another 10 tries and eventually the server crashes.
Restarting the server and everything works pretty smooth again

1 Like

Having the same issue. Similar setup.

This error typically indicates a DNS resolution problem. Here are a few steps you can try:

  1. Increase Timeout and Retry Logic: Implement a more robust retry mechanism with exponential backoff and increase the timeout duration between retries.
  2. Use a DNS Cache: Consider using a DNS caching service like dnsmasq to reduce DNS lookup times.
  3. Check DNS Configuration: Verify your server’s DNS settings. You might try using Google’s DNS (8.8.8.8 and 8.8.4.4) or Cloudflare’s (1.1.1.1).
  4. Network Stability: Ensure your server’s network connection is stable. Occasionally, intermittent network issues can cause these errors.
  5. Update MongoDB Driver: Ensure you’re using the latest version of the MongoDB Node.js driver, as updates might include bug fixes or improvements.
  6. Monitor Resource Usage: Check if your server is running out of memory or CPU, which might affect DNS resolution.
  7. Contact Hosting Support: If the issue persists, reaching out to your hosting provider might help identify if there are any network-level issues.

Implementing these strategies could help reduce or eliminate the occurrence of this error.