Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-1049

secondaryPreferred reads from all nodes

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 2.2.30
    • Affects Version/s: 2.2.29
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • 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.

            Assignee:
            christkv Christian Amor Kvalheim
            Reporter:
            kevin.adistambha@mongodb.com Kevin Adistambha
            None
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: