+1 (617) 314-9079

Blog

 

MongoDB Replication approach & Setup using Arbiter

What Replication is:

Replication is the process of synchronizing data across multiple servers. Replication provides redundancy and increases data availability with multiple copies of data on different database servers, replication protects a database from the loss of a single server. Replication also allows you to recover from hardware failure and service interruptions. With additional copies of the data, you can dedicate one to disaster recovery, reporting, or backup.

Necessity of Replication:

  • No downtime for maintenance
  • Keeps your data safe
  • Your data is available 24×7
  • Disaster recovery

Replica set:

MongoDB achieves replication by the use of replica set. A replica set is a group of MongoDB instances that host the same data set. In a replica set one node is primary node that receives all write operations and all other nodes acts as secondaries. Replica set can have only one primary node.After each successive writes to the primary node, MongoDB will replicate the same data to all the secondary nodes present in the replica set.

Illustration:

Scala & mongodb

Types of Replica set Nodes:

Primary:

The primary is the only member in the replica set that receives write operations.

Secondary:

A secondary maintains the copy of the primary data set. To replicate data, a secondary applies operations from the primary’s oplog to its own data set in an asynchronous process. A replica set can have one or more secondary nodes.

Arbiter:

Arbiters are MongoDB instances that are part of a replica set but do not hold data. Arbiters participate in elections in order to break ties. If a replica set has an even number of members then you can add an arbiter. Arbiters have minimal resource requirements and do not require a dedicated hardware. Do not run the arbiter on the same system as a member of the replica set.To avoid default write operation in the arbiter un-comment the following line in the arbiter server on /etc/mongod.conf file
nojournal=true

Necessity of Arbiter:

When a replica set is initiated (or) when a primary node goes down then MongoDB will perform an election process to determine a new primary node. All the secondary nodes and arbiter node present in a replica set can participate in the election. All write operations are done in the primary node. So when failover happens, the election mechanism will select a new primary node within seconds, and restore the write capability without any manual intervention. An odd number of members ensures that the replica set is always able to elect a primary. If you have an even number of nodes add an arbiter to get an odd number. (Ref-1: http://docs.mongodb.org/manual/core/replica-set-architectures/  ; Ref-2: http://stackoverflow.com/questions/15597372/voting-in-mongodb ).

The following table will illustrate the majority of members/nodes required for a given replica set

table2

Example:            

Consider you have 2 MongoDB nodes. One act as primary and the other once act as secondary node. If any one of the node goes down then MongoDB cannot able to choose a primary node by itself because MongoDB needs majority of members (at least two active nodes) to choose a primary node in a multi node replica set. As a result of this all write operations are failed and a lock occurs. In this scenario if an arbiter exists then the arbiter will vote for the available node to become primary.

Replication setup:

Let us consider you have three MongoDB servers like mongodbserver1.yoursite.com (Primary node), mongodbserver2.yoursite.com (Secondary node) and mongodbserver3.yoursite.com (Arbiter node). Then follow the below steps to create a replica set.

Step-1: Edit the configuration file/etc/mongod.conf in all the three serversand the following line in it
replSet=replicationName
The above line will create a replication set.

Step-2: Restart the mongod instances in all the three servers

Step-3: Start the mongo shell in the primary server and execute the following command
rs.initiate()
The replica set is now operational.To view the replica set configuration, use rs.conf(). To check
the status of the replica set, use rs.status().

Step-4: Now add the secondary nodes with the below command
rs.add(“mongodbserver2.yoursite.com: 27017″)

Step-5: Now add the arbiter server with following command
rs.addArb(“mongodbserver3.yoursite.com: 27017″)

Step-6: Now restart the MongoDB instance in all the three servers

Note:

Do not execute the command “rs.initiate()” in all the servers, Because this command will make the server as primary by default.This replication approach is tested with the Mongodbversion 2.6.7. Always use odd number of nodes in a replica set (This is given in the Mongodb documentation).

-Sarath T

Share with friends   

Written by

The author did not add any Information to his profile yet

Comments 0

Leave a Reply

*