codephobia
codephobia

Reputation: 1590

Mongoose / MongoDB replica set using secondary for reads

I recently changed my server setup to include a replica set. The secondary DBs are located in multiple regions around the world to decrease latency. The problem is that I think all of the reads are being done from the master and not from the secondary servers. I'm seeing 500ms+ latency in newrelic on servers far away from the master DB, but the staging server, which is in the same region as the master is ~20ms. How can I check if the secondary read or nearest is working, or do I have a setting missing / wrong? (I have tried both SECONDARY_PREFERRED, and NEAREST)

Url:

mongodb://1.1.1.1:27017,1.1.1.2:27017,1.1.1.3:27017,1.1.1.4:27017,1.1.1.5:27017/mydatabase

My options look like this:

"replSet": {
   "rs_name": "myRepSet"
   "readPreference": "ReadPreference.SECONDARY_PREFERRED",
   "read_preference": "ReadPreference.SECONDARY_PREFERRED",
   "slaveOk": true
}

Mongoose version: 3.8.x

Upvotes: 5

Views: 1308

Answers (3)

sahil gupta
sahil gupta

Reputation: 2349

As per the project issues on gitHub there was a issue where the Read preference does not seem to be working when upgrading to the newest version ([email protected] & [email protected]) as all the reads are being done from the master and not from the secondary servers.

As per the comments this problem doesnot come when you roll back to older version([email protected] & [email protected]), reads will start going to the secondaries(collection level). This issue is meant to be fixed in version 3.8.7.

Please reference the following issues for the same:

https://github.com/Automattic/mongoose/issues/1833 https://github.com/Automattic/mongoose/issues/1895

Upvotes: 3

womp
womp

Reputation: 116977

How can I check if the secondary read or nearest is working,

If you have access to your machines, a simple way to check which ones are being queried is with mongostat. Just log into one of your servers and run

mongostat --discover

This will give you basic output on inserts/queries/updates/delete that are being run on each machine in your replica set. If you have a quiet system it will be easy to see where the queries are being redirected to so you can at least know whether your secondaries are being hit.

If they aren't, you will need to investigate your driver settings.

Upvotes: 2

malay biswal
malay biswal

Reputation: 11

Instead of secondary can you check with nearest option. I guess that should work. Check this link. http://docs.mongodb.org/manual/reference/read-preference/#nearest

Upvotes: 0

Related Questions