6

I have one primary and two secondaries on aws using ELB. What is best practice for reading and writing?

1) Do I create a single LB with all nodes in the ELB with the primary and secondaries and let the python (pymongo) deal with sorting out to read and write do?

2) Or do a place all secondaries under ELB and assign a primary to an elastic IP? This will require a script to keep track of node type and reassign if fail-over.

I am hoping for option 1.

Thanks

Tampa
  • 75,446
  • 119
  • 278
  • 425
  • 1
    Why would you use a load balancer at all - typically you give a client the list of all the members of the set and it will find out what the status of each member is and direct requests accordingly – Frederick Cheung Nov 13 '12 at 15:27
  • Hi Frederick, thanks, your succinct comment above really shined light on this for me - please could you quickly check my answer below to make sure it is correct. – danday74 Dec 22 '16 at 16:15
  • @FrederickCheung What about a case where you have the mongo instances in a private network and need to expose mongo without making the instances accessible from the internet? – Jonathan Jul 11 '17 at 08:17

3 Answers3

5

It depends on your situation. With the mongodb drivers, you do not have to keep track of which node the client is connecting to - you merely have to inform it about the replica set and it will automatically connect to the primary and handle failover.

If you want to distribute read load, then you can set read preference to allow an application to read from secondaries, but you will only ever be able to write to the primary. That means that setting ELB for all incoming connections is unnecessary, and could even hurt you because it could cause writes to be sent to secondaries (where they will fail). Using read preference will allow you to distribute reads, so in your case, I would recommend not using the ELB.

In general, I would take a look at http://www.slideshare.net/jrosoff/mongodb-on-ec2-and-ebs. You may find it helpful.

shelman
  • 2,689
  • 15
  • 17
  • 2
    So...if I have three nodes in a rep set....and I connect with mongo...what ip address do I give? Assuming anyone can be down. Should I attach a elastip IP to the current primary and always monitor who in primary? – Tampa Nov 14 '12 at 13:37
1

I'm in the same boat, but I am running 3 mongo instances in a single replicaset, 2 default and 1 arbiter. Setting up an ElasticIP is a bad idea b/c that means your mongo server will have a public facing connection which is what you don't want.

I ended creating an ELB which listens on 27017 and forwards to both default members of the replicaset, but not for actual load balancing, rather for fault tolerance. So if the 27017 health check fails the standby can handle subsequent requests. Plus with ELB you can create a DNS CNAME record and point your app to use it, in my case I use 'docstore'. This way you don't need to specify actual mongo instances like docstore-1 and docstore-2, etc.

Mike Purcell
  • 19,847
  • 10
  • 52
  • 89
  • 1
    Please expound on this, I have 3 mongo instances in a private subnet and need to make them available and would preferably like to do it via a single ip/elb cname, is this possible? – Jonathan Jul 11 '17 at 08:20
0

I think the answer is, you don't use a load balancer. Your client provides details of all replica set members (primary and secondary) and it connects to the current primary, whichever that may be.

See How do you connect to a replicaset from a MongoDB shell?

mongo --host replicaSetName/host1[:porthost1],host2[:porthost1],host3[:porthost3] databaseToConnect

Here, we are using the command line client to connect to the replica set (not an individual node), but it could be nodeJS, MongoChef (support replica sets) or whatever client you are connecting with.

Community
  • 1
  • 1
danday74
  • 52,471
  • 49
  • 232
  • 283