Replication in MongoDB

Replication is the method of duplication of data across multiple servers in MongoDB.

For example, we have an application that reads and writes data to a database and says server A has a name and balance which will be copied/replicated to two other servers in two different locations.

Replication increases redundancy and data availability with multiple copies of data on different database servers. So, it will increase the performance of reading scaling.

The set of servers that maintain the same copy of data is known as replica servers or MongoDB instances.

Key Features of Replication:

  • Replica sets are the clusters of N different nodes that maintain the same copy of the data set.
  • The primary server receives all write operations and record all the changes to the data i.e, oplog.
  • The secondary members then copy and apply these changes in an asynchronous process.
  • All the secondary nodes are connected with the primary nodes. there is one heartbeat signal from the primary nodes. If the primary server goes down an eligible secondary will hold the new primary.

Advantages of Replication

  • High Availability of data disasters recovery
  • No downtime for maintenance ( like backups index rebuilds and compaction)
  • Read Scaling (Extra copies to read from)

How to Perform Replication in MongoDB

In order to perform replication in MongoDB, we need to first create replica sets and give permission to script the file. The basics syntax of –replSet  is −

mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"

 Or

create a ".sh"  file create_replicaset.sh and init_mongoreplica.js

Examples: 

Then run the following script :

./create_replicaset.sh
  • Directories will be created and then run the mongo.
  • In the Mongo terminal, use the command rs.initiate() to initiate a new replica set.

MongoDB – Replication and Sharding

Replication and Sharding are two important features for scalability and data availability in MongoDB. Replication enhances data availability by creating duplicate copies of the dataset, whereas sharding helps in horizontal scaling by partitioning the large collection (dataset) into smaller discrete parts called shards.

In this article, we will learn about Sharding and Replication in MongoDB. We will cover all important concepts related to them and look at their functioning with diagrams.

Similar Reads

Replication in MongoDB

Replication is the method of duplication of data across multiple servers in MongoDB....

Sharding in MySQL

Sharding is a method for distributing large collection(dataset) and allocating it across multiple servers. MongoDB uses sharding to help deployment with very big data sets and high volume operations....

Conclusion

Both Replication and sharding in MongoDB helps in scaling of database. Where replication helps in data availability, sharding is useful to horizontally scale large datasets....

Contact Us