how to understand read preferences in mongo

237 views Asked by At

I'm just getting started with mongoDB. I am trying to understand how to set up my secondary database servers so that when there is no primary, the secondaries can be used to read data. I believe the read preference I'm going for is preferredPrimary.

Now that I kinda understand which of the read preferences I want to test out, I'm trying to understand how to set up my replica set for preferredPrimary. I've been reading through the following documentation:

http://docs.mongodb.org/manual/tutorial/configure-replica-set-tag-sets/

Questions:

  1. Is this the right doc to follow to set up read preferences?
  2. Assuming that it is, I want to verify that the tags names / values are anything that I come up with? So specifically, the key used in the example "dc" is NOT a keyword in mongo. Is that correct?
  3. once I set up these tags, in my client, when I'm connecting to the mongo database, do i have to specify any settings? I'm using a php front end, and I found this: http://php.net/manual/en/mongodb.setreadpreference.php
  4. can you confirm that these tags replace the rs.slaveOK() method?

Environment:

  • mongoDB version 2.6.5
  • replica set with 3 members - one primary and 2 secondary servers
1

There are 1 answers

0
vav On BEST ANSWER
  1. Yes
  2. Yes
  3. Yes, but the link that you provided is only for readPreference You also need to supply custom writeConcern (extract from link in a question):

    db.users.insert( { id: "xyz", status: "A" }, { writeConcern: { w: "MultipleDC" } } )

Look into php driver documentation how to do that.

  1. Yes, you may skip call to slaveOK in this case (especially, that in 95% cases you will be reading from primary)