Amareswar
Amareswar

Reputation: 2064

MongoDB+Nodejs reads are not distributed across replicaset members with readpreference as NEAREST

I had a replica set with primary/secondary and read preference as NEAREST. Through MongoStat, I can see that requests are going to the replica set member where the mongod instance and nodejs is running and not to the other member on a different aws instance. Pinging is not enabled between the two. I can also verify that the other instance is accessible by changing the read preference. Working fine with my java client. Should the other instance be ping-able for read sharing? SecondaryAcceptableLatencyMS is set at 500. Here is the sample code snippet I am trying to execute.

var replSet = new ReplSetServers( [
    new Server( "localhost", 27017, { auto_reconnect: true, native_parser: true, slaveOk: true } ),
    new Server( "<replmem1>", 27017, { auto_reconnect: true, native_parser: true, slaveOk: true } ),
    new Server( "<replmem2>", 27018, { auto_reconnect: true, native_parser: true, slaveOk: true } )
  ],
  {read_secondary:true, secondaryAcceptableLatencyMS:500 }
);


var dbCon = new Db("emails_" + postParams["campaign_id"], replSet, {safe: true, readPreference: mongo.ReadPreference.NEAREST});
    dbCon.open(function(err, db) {

Upvotes: 0

Views: 1237

Answers (1)

aaronheckmann
aaronheckmann

Reputation: 10790

Its not documented well yet but a strategy option must be specified when using NEAREST.

var replSet = new ReplSetServers( [
    new Server( "localhost", 27017, { auto_reconnect: true, native_parser: true, slaveOk: true } ),
    new Server( "<replmem1>", 27017, { auto_reconnect: true, native_parser: true, slaveOk: true } ),
    new Server( "<replmem2>", 27018, { auto_reconnect: true, native_parser: true, slaveOk: true } )
  ],
  {read_secondary:true, secondaryAcceptableLatencyMS:500, stragegy: 'ping' }
);

http://mongodb.github.com/node-mongodb-native/driver-articles/anintroductionto1_1and2_2.html#read-preferences

Upvotes: 2

Related Questions