While using AWS Lambda with Node and Mongoose 5.x, we are experiencing randomly (=a group or errors every 10-15 minutes) the following error. Sometimes connection establishes just fine, but other times throws a 'replica set no primary' error.
The DocDB service is in the same VPC with the Lambdas.
Have tried with Mongoose 6.x as well. It performs less well.
As far as I can tell this cannot be a firewall issue (since it works most of the time). Profiler / audit logs do not seem to offer any hints either. Any ideas how to troubleshoot this?
ReplicaSetNoPrimary
MongooseServerSelectionError: Server selection timed out after 5000 ms
at NativeConnection.Connection.openUri (/opt/nodejs/node_modules/mongoose/lib/connection.js:847:32)
at /opt/nodejs/node_modules/mongoose/lib/index.js:351:10
at /opt/nodejs/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (/opt/nodejs/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (/opt/nodejs/node_modules/mongoose/lib/index.js:1149:10)
at Mongoose.connect (/opt/nodejs/node_modules/mongoose/lib/index.js:350:20)
at connectToMongoDB (/var/task/app/init/db.js:68:20)
at Object.<anonymous> (/var/task/app/init/db.js:109:26)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Module.require (internal/modules/cjs/loader.js:974:19)
at require (internal/modules/cjs/helpers.js:93:18)
at Object.<anonymous> (/var/task/app/init/init.js:7:26)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Module.require (internal/modules/cjs/loader.js:974:19)
at require (internal/modules/cjs/helpers.js:93:18)
at Object.<anonymous> (/var/task/app/init/index.js:1:18)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Module.require (internal/modules/cjs/loader.js:974:19)
Our configuration looks like this:
url: 'mongodb://**********.cluster-*************.********.docdb.amazonaws.com:27017/',
opts: {
dbName: '************',
user: '***************',
pass: '************',
tls: true,
tlsCAFile: caPemFile,
useNewUrlParser: true,
useUnifiedTopology: true,
replicaSet: 'rs0',
readPreference: 'secondaryPreferred',
retryWrites: false,
monitorCommands: true,
maxPoolSize: 5,
minPoolSize: 1,
serverSelectionTimeoutMS: 5000,
connectTimeoutMS: 5000,
bufferCommands: false,
autoCreate: false,
autoIndex: false,
authSource: 'admin',
},
If you have a multi-node cluster, are you noticing any failovers, or connection drops/memory contention from the metrics around the time you notice these errors?