Web servers disconnect frequently from Mongo cluster using Mongoose

Our setup is using Heroku as our web servers with static IPs that are whitelisted on our cluster, which is on AWS EC2 (Centos 8). The connection works and the application works (most of the time). However, the problem is that we have lots of disconnections we see in our logs (we have this in our connection code - mongoose):

conn.on("disconnected", function () {
  setTimeout(() => {
    logger.info("MongoDB disconnected, reconnecting...");
    if (mongoose.connection.readyState === 0) {
      global.dbConn = { status: "error", error: "down" };
      mongoose.connect(dbURI, options);
      global.dbConn = { status: "ok" };
    }
  }, 1000);
});

On the Mongo side logs, we can see the errors:

"Error receiving request from client. Ending connection from remote","attr":{"error":{"code":141,"codeName":"SSLHandshakeFailed","errmsg":"The server is configured to only allow SSL connections"

Versions:
mongoose 6.2.0
mongo 5.0.6
NodeJS 18

I can run mongosh commands etc… as needed, but I’m wondering if anyone has an idea or direction I can investigate? I thought it might relate to Cloudflare being in the middle, but the Mongo URIs aren’t listed in there.

Any help will be appreciated.

Hello :wave: @Moshe_Azaria,

Welcome back to the MongoDB Community forums :sparkles:

Based on the shared details, it appears that you have more than one server in your setup. Could you please confirm it?

To better understand the issue, could you provide more details about the frequency or percentage of occurrences when the disconnections happen?

The error message you shared suggests an SSL handshake failure, indicating that some of the web servers are not using TLS. To ensure that your web servers are using TLS, make sure to set the ssl option to true in the mongoose.connect() function or in your connection string. Additionally, it would be helpful to know if any recent changes were made, such as modifications in code or any differences in server configurations.

If you suspect any possible factors contributing to the issue, please let us know.

Best regards,
Kushagra

Hi @Kushagra_Kesav and thank you for helping out!

Yes, our cluster comprised of 3 servers, the primary is where we see almost all of the disconnections from (99%).

Regarding frequency: We can see disconnections happen about ~12 times an hour on average.

tls option is set to true. However, I will try to deploy with the ssl=true.

There weren’t any changes that caused this, it’s simply a new environment which is in the EU wheres we have another one in the US with the exact same configurations.

1 Like