How read preference modes really work?

I have a replica set: 1 primary & 2 secondaries. I want to set read preference mode to check read operations.

In my use case, I open 3 nodes in 3 mongosh terminal. I used to think read preference mode is synced between nodes, but I have to set it separately. In addition, even I set ‘secondary’ mode, primary node still can read data. Therefore, I wonder how read preference modes really work? And in production, what is the way client application can direct to read from specific nodes instead of using mongosh terminals?

Thank you!

Hi @npk72 ,

The read preference is a client option that instructs or guides the client which member or members of a repllicaset to read from. It can be set at the client level on the connectionString or client options. It can also be updated by the application for a specific database, collection or transaction.

Specific members can be targeted for secondary read by using replicaset tags. On MongoDB Atlas predefined tags can be used in the same manner.

To read from just one specific member connect directly by having it as the only host on the connection string and specify the connection option directConnection as true.

The following links provide much more detail.
https://www.mongodb.com/docs/manual/core/read-preference/#read-preference
https://www.mongodb.com/docs/manual/tutorial/configure-replica-set-tag-sets/#configure-replica-set-tag-sets
https://www.mongodb.com/docs/atlas/reference/replica-set-tags/#query-using-pre-defined-replica-set-tags

2 Likes