Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 852
codephobia Avatar asked Sep 27 '22 08:09

codephobia


People also ask

How do I read secondary data in MongoDB?

Reading from secondary (up-to-date-data) Method 1: Don't want to read stale data, keep all your secondary up-to-date. To achieve this we have to get write concern acknowledged by all the secondaries i.e write-concern = total number of replicaset including primary.

Which read Preference mode allows a secondary to be the only choice for handling read operations on a MongoDB replica set?

Starting in version 4.4, primaryPreferred supports hedged reads on sharded clusters. Operations read only from the secondary members of the set. If no secondaries are available, then this read operation produces an error or exception.


2 Answers

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

like image 83
sahil gupta Avatar answered Sep 30 '22 06:09

sahil gupta


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.

like image 43
womp Avatar answered Sep 30 '22 06:09

womp