Node js app routes all request to only one mongodb secondary node

Hi, I configure mongodb replicaset: 1 primary and 3 secondary nodes. I want to route all read data’s to all secondary nodes but node js app routes all request to only one mongodb secondary node. why does it work like this?
mongodb server rs config:

testrs [direct: secondary] test> rs.config()
{
  _id: 'testrs',
  version: 7,
  term: 31,
  members: [
    {
      _id: 0,
      host: 'mongodb01.test.local:27017',
      arbiterOnly: false,
      buildIndexes: true,
      hidden: false,
      priority: 1,
      tags: {},
      secondaryDelaySecs: Long("0"),
      votes: 1
    },
    {
      _id: 1,
      host: 'mongodb02.test.local:27017',
      arbiterOnly: false,
      buildIndexes: true,
      hidden: false,
      priority: 0.5,
      tags: {},
      secondaryDelaySecs: Long("0"),
      votes: 1
    },
    {
      _id: 2,
      host: 'mongoarbiter.test.local:27017',
      arbiterOnly: true,
      buildIndexes: true,
      hidden: false,
      priority: 0,
      tags: {},
      secondaryDelaySecs: Long("0"),
      votes: 1
    },
    {
      _id: 3,
      host: 'mongodb03.test.local:27017',
      arbiterOnly: false,
      buildIndexes: true,
      hidden: false,
      priority: 1,
      tags: {},
      secondaryDelaySecs: Long("0"),
      votes: 1
    },
    {
      _id: 4,
      host: 'mongodb04.test.local:27017',
      arbiterOnly: false,
      buildIndexes: true,
      hidden: false,
      priority: 1,
      tags: {},
      secondaryDelaySecs: Long("0"),
      votes: 1
    }
  ],
  protocolVersion: Long("1"),
  writeConcernMajorityJournalDefault: true,
  settings: {
    chainingAllowed: true,
    heartbeatIntervalMillis: 2000,
    heartbeatTimeoutSecs: 10,
    electionTimeoutMillis: 10000,
    catchUpTimeoutMillis: -1,
    catchUpTakeoverDelayMillis: 30000,
    getLastErrorModes: {},
    getLastErrorDefaults: { w: 1, wtimeout: 0 },
    replicaSetId: ObjectId("63636928358215d6a25de74c")
  }
}

node js app server config:

  "spring.data.mongodb.uri": "mongodb://notification:password@mongodb01.test.local:27017,mongodb02.test.local:27017,mongodb03.test.local:27017,mongodb04.test.local:27017/notification?replicaSet=testrs&readPreference=secondaryPreferred&serverSelectionTimeoutMS=10000&connectTimeoutMS=10000"

Hey @Murad_Samadov,

In MongoDB, there is a primary node and multiple secondary nodes. By default, all write operations (insert, update, delete) are directed to the primary node because it’s responsible for handling writes and maintaining data consistency. However, for read operations, MongoDB provides different read preferences that allow you to control how reads are distributed among secondary nodes.

Please refer to the documentation for more information on the common read preferences modes.

Would you please clarify what you mean by “route all read data to all secondary nodes”?

Looking forward to your response.

Best regards,
Kushagra

Thanks for response.
Would you please clarify what you mean by “route all read data to all secondary nodes” ? : i mean i choose secondaryPreferred read preference mode in app side. Please explain me how it is working? App get read data onle 1 secondary node? Or all replica members?

Hi @Murad_Samadov,

As per the secondaryPreferred - documentation:

  • When using the “secondaryPreferred” read preference, the client figures out how outdated each secondary server is by comparing when it was last updated compared to the primary server.
  • Then, the client sends the read request to a secondary server that is not too far behind in updates.
  • If there is no primary server available, the client chooses the secondary server that was updated most recently for comparison.
  • If none of the secondary servers are sufficiently up-to-date within the maxStalenessSeconds limit, the client will read from the primary node in the replica set.

So, in simple terms, in “secondaryPreferred” mode, the client tries to read from a secondary server that’s not too far behind the primary, and if that’s not possible, it reads from the primary.

Hope it answers your questions!

Regards,
Kushagra

1 Like