af83

Mini-howto: using the replica set of MongoDB in Java

One of the cool new feature of MongoDB 1.6 is the Replica Set. It's a nice way to scale readers and have automatic failover. You should use them instead of the Replica Pair, as Replica Set can be seen as "Replica Set version 2".

To create a replica set on 3 servers, you just have to launch mongod on them and initialize the replica set. But, for testing it on local, you can create your replica set with 3 instance on the same station:

mongod --replSet foo --port 27017 --dbpath /data/db1
mongod --replSet foo --port 27018 --dbpath /data/db2
mongod --replSet foo --port 27019 --dbpath /data/db3

Mongo will warn you that the replica set is not ready yet:

Mon Aug  2 11:30:19 [startReplSets] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)

So, initialize it:

mongo localhost:27017
MongoDB shell version: 1.6.3
connecting to: localhost:27017/test
> rs.initiate()
> rs.add("localhost:27018")
> rs.add("localhost:27019")

And you can check that you are done with the status command:

> rs.status()
{
    "set" : "foo",
    "date" : "Tue Nov 02 2010 16:57:23 GMT+0100 (CET)",
    "myState" : 1,
    "members" : [
        {
            "_id" : 0,
            "name" : "localhost:27017",
            "health" : 1,
            "state" : 1,
            "self" : true
        },
        {
            "_id" : 1,
            "name" : "localhost:27018",
            "health" : 1,
            "state" : 2,
            "uptime" : 6,
            "lastHeartbeat" : "Tue Nov 02 2010 16:57:21 GMT+0100 (CET)"
        },
        {
            "_id" : 2,
            "name" : "localhost:27019",
            "health" : 1,
            "state" : 2,
            "uptime" : 6,
            "lastHeartbeat" : "Tue Nov 02 2010 16:57:21 GMT+0100 (CET)"
        }
    ],
    "ok" : 1
}

The important thing to note is the state of each member:

  • 1 for the master
  • 2 for a slave
  • 3 for a instance that is synchronizing (just wait, it synchronizes automatically)
  • other statuses are errors.

Now that we have a replica set, it's time to use it with the Java driver. First, we can connect to the replica set. We can connect to any instance, the driver will fecth the list of other instances and other informations like who is the master. But, it's a good practice to have a list of several nodes to connect to, so if one node we connect to is down, we can fetch the nodes list from the other:

String url = "localhost:27017,localhost:27018";
ArrayList<ServerAddress> addr = new ArrayList<ServerAddress>();
for (String s: url.split(",")) {
    addr.add(new ServerAddress(s));
}
Mongo mongo = new Mongo(addr);

Then, you can use the driver normally. By default, it will send all the requests, reads and writes, to the master. Bit you can configure the driver to send only writes to the master, the reads will be dispatched on the slaves. And it's only one line of code to do this:

mongo.slaveOk();

It was easy!

Last thing, the java driver has some nice improvements on the master branch, so you should use it.

blog comments powered by Disqus