Tutorial: Replica Sets with Drupal7

You are viewing a wiki page. You are welcome to join the group and then edit it. Be bold!

MongoDB Tutorial if you are using Replica Sets.

This tutorial builds on the tutorial located here. Refer
to that tutorial on the basics of installing just a single MongoDB

MongoDB supports what is called a Replica set. This differs slightly from the master/slave paridigmn where all transactions in a master/slave took place through the master - the slave served mostly as a hot spare.

Replica sets can function all at once and provides a "High Availability" setup. The master node of a replica set accepts all incoming changes and then the secondary nodes (which can be to the nth in number) consume the changes. All nodes within a replica set can answer to a query. If you have a large replica set it is highly advised that one node be set up as an Arbitrator - the decision maker if this a conflict between the nodes.

Refer to here on how to setup a replica set or let your fingers do the walking in Google as there are many good tutorials on how to accomplish that.

A very important "Ah-Gotcha" about replica sets. DNS and Hostnames can cause you much grief with replica sets. By default Mongo takes the hostname of the machine to be used in the replica set configuration (data that gets passed back to the PHP Mongo client). This can cause a big problem if the replica set configuration has a server name within it that can't be resolved from your external servers.

If you are building this in AWS reference this post on StackOverflow about how to automatically configure and create DNS entries for EC2 instances using Route53, a python utility, and instance meta-data. This also gives you a handy fixed DNS name for you primary Mongo server of your replica set that can be configured in your Drupal setup.

Now for the actual configuration for replica sets. This configuration is exactly the same as the other tutorial with the addition of online one line of code:

'connection_options' => array( 'replicaSet' => 'REPLICASETNAME' ),


So the complete settings.php additions that need to be added look like:

['mongodb_connections'] = array(
'default' => array(                             // Connection name/alias
'host' => 'USER:PASS@HOST1,USER@PASS@HOST2,....',                       // Omit USER:PASS@ if Mongo isn't configured to use authentication. Add more than one for fallover.
'db' => 'YOURDBNAME', // Database name. Make something up, mongodb will automatically create the database.
'connection_options' => array( 'replicaSet' => 'REPLICASETNAME' ), //The name of the Replica Set you are attaching to


# -- Configure Cache
$conf['cache_backends'][] = 'sites/all/modules/contrib/mongodb/mongodb_cache/mongodb_cache.inc';
$conf['cache_class_cache'] = 'DrupalMongoDBCache';
$conf['cache_class_cache_bootstrap'] = 'DrupalMongoDBCache';
$conf['cache_default_class'] = 'DrupalMongoDBCache';

# -- Don't touch SQL if in Cache
$conf['page_cache_without_database'] = TRUE;
$conf['page_cache_invoke_hooks'] = FALSE;

# Session Caching
$conf['session_inc'] = 'sites/all/modules/contrib/mongodb/mongodb_session/mongodb_session.inc';
$conf['cache_session'] = 'DrupalMongoDBCache';

# Field Storage
$conf['field_storage_default'] = 'mongodb_field_storage';

# Message Queue
$conf['queue_default_class'] = 'MongoDBQueue';

The last thing that can be added is the following. If you want to make it so that the php driver be able to query the slaves of the replica set (you probably do if you are running a replica set), add the following to your settings.php file

::$slaveOkay = true;



Perignon's picture

I haven't got fallover working yet. So as I can tell right now. This tutorial isn't 100% complete.

I'll update it with my findings soon.

Found the problem

Perignon's picture

It was in the PHP documentation but it didn't stand out. Took a retake and reread of the documentation. The connection string must have more than one server in it for it to maintain connection to Mongo.

This isn't as mature as the GlusterFS connection to where even if only one server is specified to mount a cluster the server will know of all the other nodes in the cluster after it connects to only just one of them.

So for PHP you have to specify as many servers as you can to achieve fallover. Not what I would catagorize as a 100% High Availbility solution like GlusterFS is. MongoDB is growing though and fairly rapidly.

MongoDB Speed

Perignon's picture

Want to know much MongoDB can speed up a website?

Here are some metrics just for observation. http://cyrve.com/mongodb/

In real life, page load times drop dramatically after the page data has been cached into the Mongo DB.


Group categories

More Specifically

Group events

Add to calendar

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: