MongoDB Replication- 2 Major Strategies of Sharding in MongoDB
MongoDB Replication

MongoDB Replication- 2 Major Strategies of Sharding in MongoDB

What is MongoDB Replication?

As the name says, MongoDB replication means instances that maintain the same data set. It contains several data bearing nodes and optionally one arbiter node. Out of all the data bearing nodes, only one of them is a primary node while the others are secondary nodes. A primary node can do all the write operations. A replica set containing primary node is can confirm writes with {w: “majority”}.

Do you know what is Rockmongo? 

The secondary nodes replicate the primary one and apply the operations to their respective dataset. When the data is reflected in the second one it also changes on the primary dataset. If the primary node is not available then the secondary nodes from themselves can elect one of them as a primary node.

Here arbiters do not have a dataset with themselves. Its purpose is to maintain a quorum in a replica set by responding to heartbeat and election requests by other replica members. If your replica set has an even number of members, add an arbiter to obtain a majority of votes in an election for a primary node.

Also, read – MongoDB Mapreduce with examples & commands

Automatic Failover

When a primary node does not communicate with other members of the set for a certain period of time i.e. electionTimeoutMills(10 seconds by default) period, then an eligible secondary node calls out for an election. The clusters present over here try to complete the election as fast as possible so that they can return to the normal operations to be performed.

Here, the replica set cannot process write operation until the election is completed.

How to Set Up a Replica Set in MongoDB?

Here, we will learn how to convert a standalone MongoDB instance to the replica set. Following are the steps to convert:

  • Shutdown the already running server.
  • Again start the server by writing the following syntax:
  1. mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"

Follow this link to know about MongoDB Text Search

Now, we will take an example to understand it better.

  1. mongod --port 27017 --dbpath "D:\set up\mongodb\data" --replSet rs0
  • It will start an instance with name rs0, on port 27017
  • Now connect this to MongoDB instance.
  • To initiate a new replica set write the command rs.initiate().

Now to add members to replica set we will use the following syntax:

  1. >rs.add(HOST_NAME:PORT)

What is MongoDB Sharding?

A sharded cluster consists of the following components:

  • shard: They contain the subset of sharded data. Each shard can be deployed as a replica set.
  • mongos: They act as a query router. They also provide an interface between client applications and a sharded cluster.
  • config server: They store metadata and configuration settings for the cluster. From MongoDB 3.4 onwards config server must be deployed as a replica set (CSRS).

The following diagram describes the interaction of components within a sharded clusters in MongoDB.

Have a look – MongoDB Covered and Analyzing Query with Example

MongoDB Sharding Strategies

There are two types of strategies offers by MongoDB Sharding:

  1. Hashed Sharding
  2. Ranged Sharding

Read Complete Article>>

要查看或添加评论,请登录

Malini Shukla的更多文章

社区洞察

其他会员也浏览了