MongoDB Replication


What is MongoDB Replication?

In simple terms, MongoDB replication is the process of creating a copy of the same data set in more than one MongoDB server. This can be achieved by using a Replica Set. A replica set is a group of MongoDB instances that maintain the same data set and pertain to any MongoDB process.

Replication enables database administrators to provide:

  • Data redundancy
  • High availability of data

Maintaining multiple MongoDB servers with the same data provides distributed access to the data while increasing the fault tolerance of the database by providing backups.

Additionally, replication can also be used as a part of load balancing, where to read and write operations can be distributed across all the instances depending on the use case.

How does MongoDB replication work?

MongoDB handles replication through a Replica Set, consisting of multiple MongoDB nodes grouped as a unit.

A Replica Set requires a minimum of three MongoDB nodes:

  • One of the nodes will be considered the primary node that receives all the write operations.
  • The others are considered secondary nodes, and these secondary nodes will replicate the data from the primary node.

While the primary node is the only instance that accepts write operations, any other node within a replica set can accept read operations. These can be configured through a supported MongoDB client.

If the primary node is unavailable or inoperable, a secondary node will take the primary node's role to provide continuous data availability. In such a case, the direct node selection is made through Replica Set Elections, where the most suitable secondary node is selected as the new primary node.

The Heartbeat process

Heartbeat is the process that identifies the current status of a MongoDB node in a replica set. There, the replica set nodes send pings to each other every two seconds (hence the name). If any node doesn't ping back within 10 seconds, the other nodes in the replica are set to mark it as inaccessible.

This functionality is vital for the automatic failover process where the primary node is unreachable, and the secondary nodes do not receive a heartbeat from it within the allocated time frame. Then, MongoDB will automatically assign a secondary server to act as the primary server.