Mongos with slaveOk skips master for read operations

789 views
Skip to first unread message

Diego Moreno Quinteiro

unread,
Jun 27, 2011, 7:28:14 PM6/27/11
to mongodb-user
In my tests, setting slaveOk on mongos routes all queries to the
secondaries, ignoring primary.

I want to include all server, including primary, in read operations
load balance, is that possible?

Nat

unread,
Jun 28, 2011, 4:11:18 AM6/28/11
to mongod...@googlegroups.com
Yes. If slaveOk is set, it will try to use slaves as much as possible unless all slaves fail. You can work around it by setting slaveOk randomly with some weights to distribute some reads to master.

Dan Pasette

unread,
Jun 28, 2011, 11:03:43 AM6/28/11
to mongodb-user
Which driver and driver version are you using?

Diego Moreno Quinteiro

unread,
Jun 28, 2011, 3:46:11 PM6/28/11
to mongodb-user
I'm using the Java Driver 2.6.3.

Any specific reason for having no configuration option enabling all
nodes for reading?

And how can I set slaveOk randomly as Java Driver gives me no control
of the connection pool?

Thanks,
Diego

Scott Hernandez

unread,
Jun 28, 2011, 4:27:37 PM6/28/11
to mongod...@googlegroups.com
You can call slaveOk() at the Mongo/DB/DBCollection/DBCursor level:
http://api.mongodb.org/java/2.6.3/com/mongodb/Mongo.html#slaveOk()

SlaveOk just tells the java driver to prefer the secondaries for reads
but if there are none then the primary will be used.

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Zack Radick

unread,
Jun 28, 2011, 4:34:00 PM6/28/11
to mongod...@googlegroups.com
Link to the DBCursor API slaveOk entry:
 
Setting it on the Mongo instance is global, right?
Cheers,
--Zack

Scott Hernandez

unread,
Jun 28, 2011, 4:58:49 PM6/28/11
to mongod...@googlegroups.com
At each level it is applied to the lower ones, unless overridden. It
is an inheritance model. Setting it at the top changes the default
from false to true.

Scott Hernandez

unread,
Jan 6, 2012, 5:55:09 PM1/6/12
to mongodb-user
You can periodically call replSetGetStatus to see the lag, but other
than that there isn't much to help you decide.
http://www.mongodb.org/display/DOCS/Replica+Set+Commands

In general you may want to take the other approach and simply make
sure your writes are going to secondaries so they never get behind.
This will slow down writes but leads to a more consistent view of the
data.

You can also add a feature request to the php driver (in jira) for a
ReadPreference (new name for features like slaveOk) for
maxSecondaryDelay to control this.

On Fri, Jan 6, 2012 at 6:51 PM, Chris <christi...@gmail.com> wrote:
> Hi,
>
> I have a question about replication lag.
> Let say I am using the setSlaveOk in my code (php) and one of my
> slaves is lagging behind by a couple of seconds/minutes.
> How would I know that I should not use the slave(s) for reads? I would
> like to check for lag and all slaves are behind then just not use
> setSlaveOk any suggestions??
>
> Thanks
> Chris
>
> On Jun 28 2011, 3:58 pm, Scott Hernandez <scotthernan...@gmail.com>


> wrote:
>> At each level it is applied to the lower ones, unless overridden. It
>> is an inheritance model. Setting it at the top changes the default
>> from false to true.
>>
>>
>>
>>
>>
>>
>>
>> On Tue, Jun 28, 2011 at 4:34 PM, Zack Radick <zrad...@conducivetech.com> wrote:
>> > Link to the DBCursor API slaveOk entry:

>> >http://api.mongodb.org/java/2.6.3/com/mongodb/DBCursor.html#slaveOk%2...


>>
>> > Setting it on the Mongo instance is global, right?
>> > Cheers,
>> > --Zack
>>

>> > On Tue, Jun 28, 2011 at 1:27 PM, Scott Hernandez <scotthernan...@gmail.com>

Reply all
Reply to author
Forward
0 new messages