Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Running a replica-set with 1 primary and 2 secondaries, I wonder why the READs are also done on the primary. Shouldn't the primary mainly do the WRITEs and the secondary can share the READ-load?!

I am using the PHP driver for the mongodb and sending the details for replica set. The PHP is running from the secondaries and in the script the intitial connection goes to the secondary itself which is localhost there.

The php documentation says, that the driver will find the primary and secondaries on its own as soon as it connects to any member of the replica-set, no matter if primary or secondary.

After checking the load and logs, I found out that currently my poor primary has to deal with all the READs and the slaves are bored.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

I'm not sure what documentation you've read so apologies if I'm repeating anything here.

To distribute reads to secondary nodes, most drivers allow you to set a readPreference value for the current session. Clients set read preference on a per-connection basis. With slaveOk, the driver should will always send queries to the secondaries, if they're available.

Distributing reads to secondaries requires the use of ReplicaSetConnection with ReadPreference.SECONDARY.

See “rs.slaveOk()” for more information and this link.

In the mongo shell, to enable secondary reads, issue the following command :

rs.slaveOk()

The PHP documentation for it is here but I'm guessing that may be the documentation you're referring to.

As a FYI, here's an old discussion about it on the MongoDB Google Group.

If you're still having issues, I'd recommend using the MongoDB Google Group and providing some further information such as the version of MongoDB you're using, the version of the PHP driver, your log files, rs.conf() and rs.status().

As a FYI, you have to be careful with read scaling as sending too many reads to the secondaries can often result in the secondaries lagging the primary and becoming stale, thus requiring a full resync.

share|improve this answer
    
shouldn't the php driver deal with the replicaset correctly and NOT push all read requests to the master/write server?! –  ledy Jul 2 '12 at 8:20
2  
No, the correct behaviour is to send all requests (including reads) to the master unless specifically told otherwise (with slaveOk). –  Mark Hillick Jul 6 '12 at 10:23
    
It has to be added that there is no guarantee that the data coming from the secondary is consistent with the primary unless the write concern for said data was set to the replication factor. A note: a replica ought to provide failover capabilities. For read and write scaling, there is sharding. –  Markus W Mahlberg Jul 7 at 15:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.