Replica Sets - Basics

For production use you will want a minimum of three nodes in the replica set.

Either:

  • 2 full nodes and 1 arbiter
  • 3 full nodes

To avoid a single point of failure, these nodes must be on different computers.

It is standard to have at least 2 nodes equipped to handle primary duties.

Basic Configuration

  • Replica sets typically operate with 2 to 7 full nodes and possibly an arbiter.
  • Within a given set, there should be an odd number of total nodes.
    • If full nodes are only available in even numbers, then an arbiter should be added to provide an odd number.
    • The arbiter is a lightweight mongod process whose purpose is to break ties when electing a primary.
    • The arbiter does not typically require a dedicated machine.

Example: 3 full servers

In this example, three servers are connected together in a single cluster. If the primary fails any of the secondary nodes can take over.

Getting Started – A sample session

The following is a simple configuration session for replica sets.

For this example assume a 3-node Replica set with 2 full nodes and one arbiter node. These servers will be named sf1, sf2 and sf3.

Step 1: Start mongod with --replSet

On each of the servers start an instance of the mongo daemon:

sf1$ mongod --rest --replSet myset
sf2$ mongod --rest --replSet myset
sf3$ mongod --rest --replSet myset
the --replSet parameter has the same value myset on all three instances.

Step 1a: Check the Replication UI (optional)

Visit http://sf1:28017/_replSet, this will give you an idea on the current status of the replica set. As you proceed through the remaining steps, refreshing this dashboard to see changes. See the docs here for more details.

Step 2: Initiate the replica set

Connect to mongo on sf1.

$ mongo --host sf1
> rs.initiate()
{
    "info2" : "no configuration explicitly specified -- making one",
    "info" : "Config now saved locally.  Should come online in about a minute.",
    "ok" : 1
}
>
Initializing the replica set this way will cause the replica set to use the hostname of the current server in the replica set configuration. If your hostnames are not known to all mongo and application servers, you may need to initialize the hosts explicitly - see Replica Set Configuration for more details.

Step 3: Add nodes to the replica set

$ mongo --host sf1
> rs.add(“sf2”)
{ “ok” : 1 }
> rs.addArb(“sf3”)
{ “ok” : 1 }

Any operations that change data will now be replicated from sf1 to sf2.

If sf1 is shut down, you will see sf2 take over as primary

Changing Client Code

  • To leverage replica sets from your client code, you will need to modify your client connection code.
  • The details for doing this will vary with each driver (language).

Follow @mongodb

MongoDB Pittsburgh - May 15
MongoNYC - May 23
MongoDB Paris - Jun 14
MongoDB UK - Jun 20
MongoDC - June 26


Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

PLEASE POST QUESTIONS IN THE USER GROUPS FORUM. Post non-question comments and helpful hints here.

blog comments powered by Disqus