Re: [mongodb-user] Load balance single replica set

669 views
Skip to first unread message

Octavian Covalschi

unread,
Jul 11, 2012, 2:33:06 PM7/11/12
to mongod...@googlegroups.com
I believe that in your case it should be handled by mongodb driver. Basically when you create a connection you specify all members of you replica set. Also I think you don't have to put all of them, driver would discover rest of them, but I haven't used it in that way so I'm not sure if there is a difference...

On Wed, Jul 11, 2012 at 10:35 AM, Colin Ramage <ramage...@gmail.com> wrote:
Hi there,

We are looking for advice on how to load balance a single replica set. 

Our setup is 3 MongoDB machines on Amazon EC2 in Europe, 1 machine in each availability zone. 

I am stumped on how to setup load balancing for this without going down the sharding route which would be overkill for our setup. Is there a way this can be handled without MongoS?

Can anyone help please?

Thanks,

Colin. 

--
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
See also the IRC channel -- freenode.net#mongodb

Octavian Covalschi

unread,
Jul 11, 2012, 6:13:58 PM7/11/12
to mongod...@googlegroups.com
Speaking of slaveOk(). Java driver 2.8 says slaveOk() is deprecated and was replaced with ReadPreference.SECONDARY

So, I have to use mycollection.setReadPreference() ?

Thanks.

On Wed, Jul 11, 2012 at 2:21 PM, Emily S <emily....@10gen.com> wrote:
The driver will take care of load balancing, and if you set slaveOK to true, you'll be able to read from secondaries.

Here is some documentation for your reference:

Message has been deleted

Octavian Covalschi

unread,
Jul 13, 2012, 1:45:45 PM7/13/12
to mongod...@googlegroups.com
Thank you.

On Fri, Jul 13, 2012 at 11:36 AM, Emily S <emily....@10gen.com> wrote:
You can call setReadPreference on either DB or DBCollection and can also pass it as part of the find calls. 

For example:
collection.findOne(ReadPreference.PRIMARY)
collection.find().setReadPreference(ReadPreference.PRIMARY)

and here is the javadoc for further reference:

Emily S

unread,
Jul 13, 2012, 1:58:01 PM7/13/12
to mongod...@googlegroups.com
Correction:
collection.findOne(ReadPreference.PRIMARY) will not work.

this should work:
find().setReadPreferences(ReadPreference.PRIMARY).limit(1).next()

Gerald L. Updyke

unread,
Dec 12, 2013, 11:59:16 AM12/12/13
to mongod...@googlegroups.com
Apologies for resurrecting this old thread, but I have the exact same question and the answer doesn't really make sense to me: "The driver will handle load balancing".

We have a 3 server MongoDB replica set in an AWS environment. When PRIMARY goes down, a SECONDARY gets promoted, so that's all well and good. We use Jenkins to deploy code and specify the PRIMARY MongoDB server by name and the data gets replicated, which is also well and good. But when I kill the PRIMARY, it breaks the app. Even though a SECONDARY gets promoted to PRIMARY, the app itself is still trying to hit the original MongoDB server and is not routing properly.

So I'm left baffled as to how, if our app is pointing to MongoDB_Server_1, it will continue to function if MongoDB_Server_2 is now PRIMARY?

I'm obviously new to this whole AWS/MongoDB thing. Any help is greatly appreciated.

Thank you.

Asya Kamsky

unread,
Dec 12, 2013, 11:51:36 PM12/12/13
to mongodb-user
You should not be connecting to "primary" by name as primary is just a
role that any of the nodes in the replica set can have.
You should be connecting to the replica set: set name and list of seed
nodes. The driver will take care of figuring out which one of them
is the primary.

Asya
> --
> --
> 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
> See also the IRC channel -- freenode.net#mongodb
>
> ---
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mongodb-user...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Octavian Covalschi

unread,
Dec 13, 2013, 11:58:47 AM12/13/13
to mongod...@googlegroups.com
I think it's a good question, even though it seemed to have an obvious answer... it may not be.

So, based in this http://docs.mongodb.org/meta-driver/latest/legacy/connect-driver-to-replica-set/  "Not all members need be specified", so technically I could use only one member for my replica set connection and driver should pull members from that member, right? And if driver is not caching that list, when the only member provided is down driver won't be able to figure out the master...

Gerald L. Updyke

unread,
Dec 13, 2013, 4:56:53 PM12/13/13
to mongod...@googlegroups.com
Thank you! I see in Jenkins where it specifies the connection string and I was able to put in the value as mentioned in the documentation. Now if only Jenkins wasn't acting up, I could see if this fixed it. I feel confident this was the missing piece.

Thank you, again.

Jerry

Asya Kamsky

unread,
Dec 15, 2013, 4:05:09 AM12/15/13
to mongodb-user
You only have to specify a single member but you have to specify it in the format of a replica set!

So connecting to host:27017 isn't the same as replSetName/host:27017   or mongodb://host:27017/?replicaSet=replSetName

Asya

Reply all
Reply to author
Forward
0 new messages