1

My application runs on NodeJS 4.4.7 and uses MongoDB driver 2.2.31 (not Mongoose) to connect to Azure CosmosDB. This is how I connect to the DB:

var connectionString = 'mongodb://USERNAME:PASSWORD@yyy.documents.azure.com:10255/DB_NAME?ssl=true'
var options = {
    db: { j: false },
    server: { autoReconnect: true, socketOptions: { connectTimeoutMS: 300000 } },
};
require('mongodb').MongoClient.connect(connectionString, options, callback);

And I recently started experiencing the following error:

MongoError: connection X to http://yyy.documents.azure.com:10255 timed out

where X is a small integer (I've seen 8, 10, 29, and so on).


Some background info:

  • Had no errors (for 30+ days) when I didn't have geo replication set up.
  • Recently set up geo replication from the Azure portal (West US as a write region, East US as a read region).
  • Started experiencing "no primary server available" error.
  • Updated my mongodb driver from 2.0.49 to 2.2.31 and the error was gone, however immediately started experiencing "pool destroyed" error.
  • Following this workaround solved the issue (i.e. remove &replicaSet=globaldb)
  • After 3 days of zero issues, I started experiencing the above "connection timed out" error. First reported error was at around Aug 16 8:24 PM EST, and last error reported was at around Aug 17 6:40 AM EST.


I'm not sure if it is a temporary issue from Azure or there is something wrong in the way I connect to CosmosDB. Any suggestion would be appreciated!

BH.Q
  • 11
  • 1
  • 2

1 Answers1

1

This issue might be related to not setting client-side connection parameters properly. Could you please try to set these and see if it resolves the timeout issue?

MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder();
optionsBuilder.socketTimeout(10000);
optionsBuilder.maxConnectionIdleTime(60000);
optionsBuilder.heartbeatConnectTimeout(5000);
MongoClientURI mongoClientURI = new MongoClientURI(props.getMongoDbConnection(), optionsBuilder);
alekseys
  • 321
  • 1
  • 6
  • Thanks @alekseys, I would like to try your suggestion but I am having hard time finding equivalent variables in the MongoDB NodeJS driver. Could you please help me find `maxConnectionIdleTime` and `heartbeatConnectTimeout` equivalents? – BH.Q Aug 18 '17 at 17:03
  • there are no strict equivalents in NodeJS driver. You might want to check out keepAlive option and possibly combination of others listed on http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect – alekseys Aug 18 '17 at 17:13
  • Thanks. I am using `keepAlive`, `socketTimeoutMS` and `connectTimeoutMS` and will be monitoring if the same error occurs over the weekend. – BH.Q Aug 18 '17 at 18:20
  • @BH.Q Did setting those parameters help fix the error? – rj2700 Feb 01 '20 at 06:33