Here we’ll try to cover about Replication, Sharding and Backup in MongoDB.


Replication is the methodology of synchronizing data crosswise over different servers. Replication gives repetition and builds data accessibility with numerous duplicates of data on diverse database servers; replication ensures a database from the departure of a single  server. Replication likewise permits you to recuperate from equipment failures and administration intrusions. With extra duplicates of the data, you can devote one to fiasco recovery, reporting, or backup.

Why Replication?

  • To keep your data safe
  • High (24*7) accessibility of data
  • Calamity Recovery
  • No downtime for upkeep (like backups, record modifies, compaction)
  • Read scaling (additional duplicates to peruse from)
  • Reproduction set is transparent to the application

How replication functions in MongoDB

MongoDB attains replication by the utilization of Replica set. An replica  set is a gathering of MongoD instances  that have the same data set. In a replica one hub is essential hub that gets all write  operations. All different occasions, secondary’s, apply operations from the essential with the goal that they have the same data set. Need to note that the eeplica set can have only one node that is primary.

  1. Replica set is a collection of two or more hubs (for the most part least 3 hubs are needed).
  2. In an replica set , one hub is essential hub and remaining hubs are optional.
  3. All data flows from primary  to auxiliary hub.
  4. At the time of programmed failover or support, decision secures for essential and another essential hub is chosen.
  5. After the recovery of fizzled hub, it again joins the replica set and fills in as an auxiliary hub.
READ  MongoDB - Covered and Analyzing Queries

Replica set peculiarities

  • A group of N nodes
  • Any hub cane  be made as primary
  • All write  operations must go to primary
  • Programmed failover
  • Programmed Recovery

Set up a copy set

In this exercise we will change over standalone mongod instance to a replica set. To change over to replica set take a look at the  below mentioned  given steps:

  • Shutdown officially running MongoDB server.

Presently begin the MongoDB server by defining -replset alternative.

Fundamental sentence structure of -replset is given beneath:

mongod -"PORT" -dbpath "Your_db_data_path" -replset "Replica_set_instance_name"

Foe example

mongod --port 27017 --dbpath "D:\set up\mongodb\data" --replSet set0

It will begin a mongod example with the name set0, on port 27017. Begint the command prompt and connect with the mongod instance. In mongo customer issue the commans rs.initiate() to start another replica set. To check the replica set setup , try the command rs.conf(). To check the status of reproduction sete issue the command  rs.status().

Add parts to replica  set

To add parts to replica  set, begin mongod occurrences on numerous machines. Presently begin a mongo customer and issue a commans like  rs.add().


Fundamental structure  of rs.add() command is as listed below:


Assume your mongodb case name is on port 66089 then to add this occurrence to reproduction set issue the charge rs.add() in mongo customer.

For Example


MongoDB Sharding

You can add mongod case to replica set just when you are joined with primary  hub. To check whether you are joined with primary or not, fire the command db.ismaster() in mongo customer.


Sharding is the methodology of putting away data records crosswise over various machines and it is MongoDB’s methodology to take care of the requests of data development. As the span of the data builds, a solitray  machine may not be sufficient to store the data nor give a worthy update  throughput. Sharding takes care of the issue with flat scaling. With sharding, you add more machines to help data development and the requests of read and compose operations.

READ  MongoDB Text Search and Rockmongo

Why Sharding?

  • During replication , the writes goes to the master hub
  • Inactivity delicate inquiries still go to master
  • Single replica set has restriction of 12 hubs
  • Memory can’t be huge enough when dynamic dataset is enormous
  • Nearby Disk is not huge enough
  • Vertical scaling is excessively costly

Sharding in MongoDB

  • Shards: Shards are utilized to store data. They give high accessibility and data consistency. In live environment every shard is a different replica set.
  • Config Servers: Configuration servers take care to store the metadata of the cluster. This data contains a mapping of the cluster’s data set to the shards.  In live  environment sharded clusters have precisely 3 config servers. 
  • Query Routers: Query Routers are fundamentally mongos instancecs, interface with customer applications and immediate operations to the fitting shard. The query  router methodologies and targets operations to shards and after that return results to the customers. A sharded group can contain more than one query  router to share the request  load.By and large a sharded cluster has numerous query  router.

MongoDB Create Backup

Dump MongoDB Data

To make backup of database in MongoDB you ought to utilize mongodump command. This command will dump all data of your server into dump index. There are numerous choices accessible by which you can restrain the measure of data or make backup of your remote server.


Essential  structure of mongodump command is as listed below.



Initiate your mongod server. If you are assuming that your mongod server is running on port 27017 and on the local, then you may open the command prompt , go to bin directory and then fire the command mongodump.

READ  MongoDB Relationship Example

This command will help to connect to the server running at localhost  and port 27017 and back all data of the server to directory /bin/dump/.