MongoDB Replica Set

I have setup the replica set in one of my project, so thought of sharing knowledge with you. Hope you will find this useful.

If you refer to my previous blog where I have shown how to install the MongoDB on Windows Server.

In this post i will be focusing on how to create replica set, Integrated the replica set in Sitecore Application and doing End to end testing replica set is working.

As per the Sitecore Documentations and Best practises we should have minimum 3 Set of Server out of which one will be primary and other two will be secondary Server.

In Replication, Each Mongo DB share the same dataset, which provide redundancy and high Availability. You can place server at different location for Disaster recovery and High Availability.

This also help increase read Capacity, by directly reading the data from Secondary server.

  • Write Operation can only happened on Primary database.
  • All changes are recorded into Operation log.
  • Both the Secondaries database copy from the Primary Operation log.
  • If Secondary server with Priorities 0 it can never become primary.

Note: – This doesn’t solve the problem of Scaling, For Resolving scaling MongoDB Sharding method will be required.

Installations

I have installed all the 3 MongoDB Server on one machine, by using 3 Different Port number.

In order to do I have just added the below 2 line in Mongo.Config.

#port number
port=2684

#replica set name
replSet=rs1

You can install the mongoDB using the below command.

C:\mongodbSecondary\bin>mongod --config C:\mongodbSecondary\mongo.config --serviceName MongoDB2 --serviceDisplayName MongoDB2 –install
I have created the 3 Server:-

  • PrimaryLocalhost:2684
  • SecondaryLocalhost:2690
  • SecondaryLocalhost:2692

Now go to command prompt add the below command

C:\mongodb\bin>mongo localhost:2684

You will see the below output

MongoDB shell version: 3.2.6

connecting to: localhost:2684/test

rs1:PRIMARY>

Adding Replica Set to the Server.


Add the below command mark in yellow colour, I have added the 2 server, you can add the 3 server in the command.

I was facing the error while adding the replica Set, So I am using the command as rs.reconfig(config, {force:true})

Checking Replica Set Status

Using rs.status() command, You can find One server is primary and other is secondary.

Adding Priority & Voting to Replica Set

I found that you need add the priority and Voting which make your Secondary server to Primary. When your Primary Server is down. Replica set Uses election to determine which server will become primary.


cfg = rs.conf()
cfg.members[0].votes = 1;
cfg.members[0].priority = 20;
cfg.members[1].votes = 1;
cfg.members[1].priority = 15;
cfg.members[2].votes = 1;
cfg.members[2].priority = 10;
rs.reconfig(cfg);

Votes: – The number of Votes each member have either 1 or 0. A replica set can have 50 Member but only 7 voting member.

Priority:-  The primary is the only member of a replica set that can receive write operations. You set priority by allocating a number to each instance, the highest number gets top priority. The other member with less priority will become secondary.

If the priority of any server is 0 it can never become primary server.

By using rs.Config() command will give you the information on priority.

Configuration Options:-

There are few other Configuration options.

  • Arbiter Node: – This node doesn’t hold any copy and involved only in Elections.
  • Hidden Node: – This server never become primary and used by the applications. It used for Backup and reporting purpose.
  • Writing Concern :-
  • Red Preference: – it help to route the read Operation to other member. By default Primary server is set to Read

Sitecore Connection String

In the connection string you need to mention all the server name and replica set name.

Testing:-

In Order to test the replica set, you can stop the Mongo Primary Server and using the command rs.status() you can find that Secondary Server has become Primary. Primary server will error unable to connect.

As mentioned in Sitecore link, by changing the Session Timeout Default settings and visiting to local website you can find the interaction in MongoVue.

References:-

https://stackoverflow.com/questions/48024653/how-to-assign-priority-to-mongodb-3-2-replica
http://dougchang333.blogspot.com.au/2012/09/mongodb-dbruncommandreplsetinitiate-try.html
https://docs.mongodb.com/manual/reference/connection-string/

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.