Not able to use secondary for read using Readpreference=secondary and nearest.

111 views
Skip to first unread message

Amit Agarwal

unread,
Oct 20, 2016, 6:15:40 PM10/20/16
to mongodb-user

Hi,
We are using mongodb with some node.js application and need to figure output to push the read connections to secondary/nearest secondary.
We tried couple of options with mongodb url
1) With replica set as below
All the connection goes to primary in case of replicaset and without replicates also it goes to primary alongwith all secondary because those are the part of URL.
Can someone help us to provide some details so we can push all reads just to secondaries?

Thanks

Kevin Adistambha

unread,
Nov 2, 2016, 1:38:58 AM11/2/16
to mongodb-user

Hi,

We are using mongodb with some node.js application and need to figure output to push the read connections to secondary/nearest secondary.

Please note that using Secondaries to provide extra read capacity could potentially interfere with the high availability aspect of the replica set. See Can replica help scaling post and Counter-Indications for more information.

Also, there is no “nearest Secondary” read preference. The “nearest” read preference setting instructs the driver to read from the nearest node, either Primary or Secondary. Please see the Read Preference Modes page for more details.

Having said that, the node.js driver follows the MongoDB URI connection string format specification. For example, if the replica set consists of host1, host2, and host3 with replica set name replset and you require Secondary reads, the URI should be in the form of:

mongodb://user:password@host1:port1,host2:port2,host3:port3?replicaSet=replset&readPreference=secondary

All the connection goes to primary in case of replicaset and without replicates also it goes to primary alongwith all secondary because those are the part of URL.

The driver maintains connection to all members of the replica set. This is to allow the driver to continue to operate when failovers happen and a new Primary is elected. You can check this behaviour by temporarily decreasing the slow queries threshold to -1 so that all queries will be logged using the db.setProfilingLevel(0,-1) command. If you specify Secondary read preference in the driver, you should see a line in the Secondary’s log similar to this:

2016-11-02T16:36:03.870+1100 I COMMAND  [conn135] command test.test command: find { find: "test", filter: {} } planSummary: COLLSCAN cursorid:33674518980 keysExamined:0 docsExamined:101 keyUpdates:0 writeConflicts:0 numYields:0 nreturned:101 reslen:12715 locks:{ Global: { acquireCount: { r: 2 } }, Database: { acquireCount: { r: 1 } }, Collection: { acquireCount: { r: 1 } } } protocol:op_query 0ms

but no similar lines in the Primary, which indicates that the driver is reading from the Secondary.

If you’re still having issues, could you post:

  • Your MongoDB version
  • Your node.js driver version

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages