-
Type:
Bug
-
Resolution: Fixed
-
Priority:
Major - P3
-
Affects Version/s: 2.2.29
-
Component/s: None
-
None
The latest node driver seems to spread reads to all nodes in the replica set instead of preferring secondaries.
For example, this Javascript code:
const MongoClient = require('mongodb').MongoClient; const mongouri = 'mongodb://localhost:27017,localhost:27018,localhost:27019/test?replicaSet=replset&readPreference=secondaryPreferred'; MongoClient.connect(mongouri, function(err, conn) { if (err) console.log(err); for (var i = 0; i < 10000; i++) { conn.collection('test').findOne({seq: i}, function(err, res) { console.log(res); }); } });
will result in these numbers in mongostat:
host insert query update delete getmore command ... localhost:27017 *0 445 *0 *0 0 3|0 ... localhost:27018 *0 527 *0 *0 0 3|0 ... localhost:27019 *0 914 *0 *0 0 3|0 ...
However, a similar code in Python spreads the reads only to the secondaries:
import pymongo conn = pymongo.MongoClient( 'mongodb://localhost:27017,localhost:27018,localhost:27019/test' + '?replicaSet=replset&readPreference=secondaryPreferred') for seq in range(10000): print conn.test.sequences.find_one({'seq': seq})
which results in:
host insert query update delete getmore command ... localhost:27017 *0 *0 *0 *0 0 2|0 ... localhost:27018 *0 870 *0 *0 0 2|0 ... localhost:27019 *0 852 *0 *0 0 2|0 ...
I have also confirmed that the Java driver behaves like the Python driver.
Since secondaryPreferred should prefer the secondaries and only read from the primary when no secondaries are available, I think the node driver should behave like the Python & the Java driver.