Introduction to MongoDB Replica set

Rama Sagar
Posted by in NoSql category on for Beginner level | Points: 250 | Views : 9001 red flag

MongoDB (from "humongous") is a cross-platform document-oriented database system. Classified as a NoSQL database, MongoDB eschews the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas
Read Introduction to MongoDB Install as a service before this article.


In this article we will learn about Replica set in  mongoDB.we will also see about primary ,secondary and arbiter databases in mongo db

Previous articles have provided an introduction to mongodb, setup and installation,and few command line options.You can get them from the following:


 The objective of the article is to learn about replica set in Mongo database.Sometimes things don't always go our way...sometimes our server may crash and when that happens...what happens to our application??

 If we have a backup we will spend some time and will restore the data which is a traditional way of doing.......

 But we can do much better than that where the replica set of mongo dB comes into role.........

 Replica set 

Mongo dB supports an arrangement called a replica set, the members of replica set are primary ,secondary or number of secondary’s and potentially an arbitrary. Let’s look at the rows of each one of those in a replica set.

The primary database is the one and only writable instance in a replica set that means any of the clients that want to write data to the database have to be connected to the primary and have to issue write commands against the primary, An attempt to write to secondary will fail...

The secondary databases are read only instances…we can have many number of secondary databases this means that we also have scalability because we can perform many more reads against the replicas rather than attacking a single server with all requests for crud operations..

In the secondary databases the data is gonna be replicated from the primary eventually which we call eventual consistency. At some point, if primary database is failed one of the secondary will take over and will become the primary where we can get automatic recovery from the crash of the primary…..

The third type of member in replica set is the arbiter. Arbiters are mongod instances that are part of 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, add an arbiter.

Arbiters have minimal resource requirements and do not require dedicated hardware. We can deploy an arbiter on an application server, monitoring host.

Now lets create the databases and see them in action..

Here we gonna create three directories in our dotnetfunda directory named db1,db2,db3

md \dotnetfunda\db1

md \dotnetfunda\db2

md \dotnetfunda\db3

now lets start the three instances give the command

@REM Primary

start "a" mongod --dbpath .db1 --port 30000 --replSet "demo"

@REM Secondary

start "b" mongod --dbpath .db2 --port 40000 --replSet "demo"

@REM Arbiter

start  "c" mongod --dbpath.db3 --port 50000 --replSet "demo"


In this article we have learned about replica set and we have started the overview with an example.


Read Introduction to MongoDB Mongo Shell after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

Rama Sagar
Full Name: RamaSagar Pulidindi
Member Level: Silver
Member Status: Member,MVP
Member Since: 12/30/2012 1:51:40 AM
Country: India
A Software Profesional working in Microsoft .NET technologies since year 2008, and I work for Dake ACE. I am passionate about .NET technology and love to contribute to the .NET community at Dot Net Funda

Login to vote for this post.

Comments or Responses

Posted by: Ashwink2 on: 1/15/2019 | Points: 25
I like this homepage and with this increasing my skill level for command prompt of my windows 10 computer system. You should need to visit this once if you are using this computer system.

Login to post response

Comment using Facebook(Author doesn't get notification)