Lab - Read Concern and Read Preferences

Hi everyone,

I passed this lab but I don’t understand !

If I shutdown 2 nodes, the last is a secondary node. if I use the find command without defining a reading preference, it works. But “primary” is the default mod for readPref.

Documentation say:

Read preferences aren’t relevant to direct connections to a single mongod instance.

But she also says

However, in order to perform read operations on a direct connection to a secondary member of a replica set, you must set a read preference , such as secondary.

https://docs.mongodb.com/manual/core/read-preference/#indications

How to understand this behaviour ?
I am sorry in advance if the answer is given in the course, I do not master English.

I am thinking this is because you are connected to the replica set and not directly to a secondary as stated in the parts you have quoted.

Thx for your response. I connected from mongo shell directly to the node concerned.

mongo --host 192.168.103.100:27002 [others args]

What do you mean? Only one node is still in operation at this stage of the exercise

My understanding is that you’re connected to the single secondary node not the replica set. So the second rule about Replica Sets doesn’t apply.

So in order to connect to the Secondary node of the replica set, include the name of the replica set in your connection string and try again.

Thx for response. I did the test again.

If I include the replica set name:

mongo --host m103-repl/192.168.103.100:27002 [other args]

The connection is not possible because there is no more primary node:

2019-10-19T21:17:49.634+0000 W  NETWORK  [ReplicaSetMonitor-TaskExecutor] Unable to reach primary for set m103-repl

I can only connect to the node directly. Besides, it’s in the lab instruction:

you will not be able to connect to the replica set. You will have to connect to the third node.

In that case, it sounds like the replica set must be up and running for the second condition to apply. Bring it back up, and give it a try.

I performed the test with all the nodes of the replica set enabled. Same thing.
If I use find from a secondary node without defining readPref, it work.

db.products.find({})

According to the documentation, it should be:

db.products.find({}).readPref('secondary')

I just think that the default value of readPref mode automatically becomes secondary but as the documentation seems to say otherwise, it’s disturbing.

it’s not a big deal but it intrigues me.

Hey @grolip I was curious as well and I ran so I went ahead and ran some tests/investigated.

Before you can read from a secondary, you have to set slaveOk=true which I’m sure you’re doing. Invoking/executing rs.slaveOk() is telling mongo to redirect all reads to the secondary. And it appears that slaveOk is no longer supported in newer versions of PyMongo in support of Read Preference. See the change log for PyMongo:

In short, if you don’t set rs.slaveOk, you must use a secondary or secondaryPreferred Read Preference when connected to a Secondary as per documentation. Although, the documentation can be worded better!

Also see this MongoDB Node.JS driver github doc:

2 Likes

That’s it! You’re got damn right.
Thank you very much for your persistence.