Reputation: 4423
One thing I'm struggling to understand is when setting up our current Mongo configuration we have two servers with an arbitrator currently.
Now first off I created a record on the master; went to the secondary then tried to find it and I was receiving errors which looked like:
error: { "$err" : "not master and slaveok=false", "code" : 13435 }
Upon reading on mongo's website about Slave OK I found that on the secondary servers I needed to set
rs.slaveOk();
However I'm not totally understanding why I would do this in PHP when querying the servers; or if I'm mis-understanding the point all together.
Essentially I have a pool of servers, and I was planning on connecting to them as such:
$m = new Mongo("mongodb://localhost:27017", array("replicaSet" => "myReplSetName"));?>
This would then connect to the master.. however this doesn't appear to distribute the read load then.. how could I distribute the read load so that it would span across both servers and thus make queries quicker?
Thank you in advance
Upvotes: 3
Views: 4823
Reputation: 5126
That's an acceptable way to connect to the replica set. Assuming that the named server is up, the driver connection will discover the remainder of the servers in the cluster and be able to direct queries to them when permitted, and follow the "PRIMARY" designation through failover events. It's a good idea to add other servers to this "seed list", though, so that the connection can still succeed if one of the "seed" servers happens to be down.
As for distributing reads ... this behavior is in practice at the discretion of the driver. But the generally accepted semantics is: if a slaveOk read is requested, that read query will go to a SECONDARY server if one is available; only if none are available will it go to the PRIMARY. Write queries, of course, will always go to the PRIMARY.
One suggestion for having the PRIMARY share some of the read load, is to randomize when you flag a query as slaveOk -- see the discussion here
To flag a query as slaveOk in PHP, you can set this bit at several levels -- connection, database, collection, or cluster -- API details here This affords several possible ways of implementing the aforementioned randomization technique -- e.g., try to make sure half the connections in your connection pool have the slaveOk bit set.
Hope that helps!
Upvotes: 10