MongoDB – Replica Sets


What is a replica set:

In short a replica set is a combination of replication, backups, high availability,  scalability and distributed data access combined.  What the guys over at Mongo decided to do is to basically challenge the way we think about these topics by combining them into replica sets.  You should see by looking through this document how easy it is to set up and how easy it is to add new servers to your replica set configuration.

Why use replica sets:

Replica sets offers you certain features as follows (from MongoDB Site):

  • Supports 1 – 7 servers in the cluster
  • Automatic failover and recovery
  • Data center aware
  • Supports passive set members (slaves) that are never primary

All this and much more can be gleaned from the MongoDB web site at:
http://www.mongodb.org/display/DOCS/Replica+Sets

How do I create a replica set:

Now to move into how you would go about creating this replica set or set it up at least.  Basically you would want two servers to start with.  Mongo allows you to add more as and when you need them.  This section though I will simply give you the short commands that you should run from Command Prompt in windows.  I will then discuss the various parameters on the command and why they’re important to know about.  Once again, all this information can be found on the Mongo site, but if you’re new to Mongo like myself and you just want a quick overview and set up so you can start playing with this “new” technology, then this guide will help you get up and running quickly.

To set this up do as follows:

  1. Create a batch file named: RunServer10000.bat to the root of your MongoDB installation’s bin folder.
  2. Add the following lines to the batch file:
    mkdir “data/localhost10000”
    mongod –rest –shardsvr –port 10000 –replSet set1/localhost:10001 –dbpath data/localhost10000 –logpath data/localhost10000/log.txt
    PAUSE
  3. Create another batch file named: RunServer10001.bat to the root of your MongoDB installation’s bin folder.
  4. Add the following lines to the batch file:
    mkdir “data/localhost10001”
    mongod –rest –shardsvr –port 10001 –replSet set1/localhost:10000 –dbpath data/localhost10001 –logpath data/localhost10001/log.txt
    PAUSE
  5. Run RunServer10000.bat
  6. Run RunServer10001.bat

That’s it now you should have two replica set instances running.  In the next section I’ll discuss how to configure these to start replicating to one another.  For now, let’s go through what we’ve just done.

If you open up the batch file called RunServer10000.bat in Notepad, then you’ll see four lines explained as follows:

  1. mkdir “data/localhost10000”
    If you’re not familiar with Command Line scripts then the simple answer to this one is that it creates the directory structure “data/localhost1000” for you under your default Mongo bin directory.
  2. mongod –rest –shardsvr –port 10000 –replSet set1/localhost:10001 –dbpath data/localhost10000 –logpath data/localhost10000/log.txt
    This has a couple of bits to take into consideration as follows:

    1. mongod
      This runs the Mongo Database instance.
    2. –rest
      This runs the Mongo database in a restful manner which will allow you to open the web admin UI that comes built in with MongoDB usually found at:  http://localhost:11000/ if you run MongoDB on port 10000.  You should be able to pick up the port number for the admin UI in the log file which I’ll explain in point below.
    3. –shardsvr
      This enables sharding on this specific instance, which I’ll explain in more detail later.
    4. –port 10000
      Specifies the port on which this specific instance of the mongod.exe should run.  This is the port you’ll use to connect to this instance.
    5. –replSet set1/localhost:10001
      This is the important bit when setting up a replication set.  The important bit to remember when setting up two or more instances in a replica set is that the convention is: “/”.  In our scenario we only have one alternative server for this replication set which is run when we do step 6 above.  MongoDB allows you to configure up to seven servers in the cluster, which is important when you start thinking about setting up a cluster.  So, strictly speaking you should be able to replace with a comma separated list in the format of “localhost:10001,localhost:10002” in essence thereby configuring this as a “replication” set which will give you the fail over you need in case one of your servers goes down for whatever reason.
    6. –dbpath data/localhost10000
      This specifies where to put our database.  If you don’t add this parameter Mongo will try and create the database in the default location of “c:\data\db” and will not start if that directory structure does not exist.  Personally I don’t like things on my C drive, which is why I’m specifying this parameter and ensuring that the directory structure exists by adding it to my batch file as specified above.
    7. –logpath data/localhost10000/log.txt
      This specifies where Mongo should write it’s log files for this specific instance.  Personally I like having my log file and my database files in the same directory if I’m running in development/test mode, because it keeps things nice and together.  In Production I would split the logging off to its own drive for speed and backup purposes.
    8. 3. PAUSE
      This is only in the batch file because I want to see if something went wrong and it’s not written to the Mongo log file.  You can remove it…

There are a few additional parameters that you can set which you can find at:
http://www.mongodb.org/display/DOCS/Replica+Set+Configuration

For this example though I’m not going to go into more detail seeing that I’m trying to explain things in as simple a manner as possible to get you up and running as fast as possible.

How do I configure the set:

Now that we have our two “servers” named “localhost:10000” and “localhost:10001” up and running we should configure them so they can start talking to each other.  Something to remember before attempting this step is that both your replica set instances needs to be up and running. If they’re not running then the next section will not work…

To make this process and running the MongoDB console against our default instance “localhost:10000” a bit easier you need to do as follows:

  1. Create a batch file named: RunMongoDBConsoleDefaultInstance.bat to the root of your MongoDB installation’s bin folder.
  2. Add the following lines to the batch file:
    mongo.exe localhost:10000
    PAUSE

Now that you have that let me quickly explain what it means.

  1. mongo.exe
    This is the application which gives you the MongoDB Console where you can do all kinds of funky things which you can read about on Mongo’s site.  They also have an online “TRY IT OUT” which includes a Tutorial on how to use the Mongo Console for you to play with.  For this exercise I’m not going to go into that.
  2. 2. localhost:10000
    This specifies which instance you want to connect to.  In our case we want to connect to the default instance.

Once you have this up and running you should see something like:

MongoDB shell version: 1.6.2
connecting to: localhost:10000/test
>

Now that you have the console running you can type in the following:

  1. 1. cfg = { _id : “set1”, members : [ { _id : 0, host : “localhost:10000”}, { _id : 1, host : “localhost:10001”} ] };
    This sets up the configuration variable for use by configuring our two servers.
  2. 2. rs.initiate(cfg);
    This initiates the replica sets that we’re setting up.

How do I add a new server to the set:

Now that you have your cluster of two servers up and running you may need to add another server into the mix.  This is extremely simple as follows:

  1. Create a batch file named: RunServer10002.bat to the root of your MongoDB installation’s bin folder.
  2. Add the following lines to the batch file:
    mkdir “data/localhost10002”
    mongod –rest –shardsvr –port 10002 –replSet set1/localhost:10000 –dbpath data/localhost10002 –logpath data/localhost10002/log.txt
    PAUSE
  3. Run RunServer10002.bat

That should run your new replica set instance.  Now to configure it all you have to do is run the batch file you created in the “How do I configure the set” section above called:  RunMongoDBConsoleDefaultInstance.bat.  This will start the MongoDB console by connecting to our default instance where you should then run the following simple command:

rs.add(“localhost:10002”);

This will initiate this new server for you in the replica set and it will start synchronizing data for you automatically.

Conclusion

Now that we’re done you can see that it’s as simple as following 10 simple steps.  Of course it’s not quite that easy when you’re starting out and you don’t quite know what all the stuff means and what the underlying architecture involves, but at least now you should be able to get up and running so you can start coding against your own replica sets.  I hope you enjoy MongoDB as much as I’ve enjoyed it.  It’s a really powerful tool to be used in many scenarios as long as you pick the right scenario 🙂

Related Articles

  1. diep ngo
    May 7, 2012 at 17:25

    Thanks for your tut .However , i have some problem : if i hit ctrl+c or close windows command prompt , the repilcas will down and all replicas will fail. So, how to keep replica without always opens windows command promt.

  2. March 21, 2013 at 03:40

    thank for your sharing,i like it very much!

  1. No trackbacks yet.

Leave a comment