Data replication in MongoDB is the process

logeswarisaravanan 7 views 11 slides Nov 01, 2025
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

Data replication in MongoDB is the process of duplicating data across multiple servers to ensure availability, reliability, and fault tolerance


Slide Content

MongoDB Data Replication in Servers Dr.T.Logeswari

Introduction Data replication in MongoDB is the process of duplicating data across multiple servers to ensure availability, reliability, and fault tolerance . Replication allows the same data to exist in more than one server, so if one server fails, another can continue to serve client requests without interruption. Primary Server → Handles all write operations. Secondary Servers → Copy data from Primary (asynchronous replication). Arbiter (optional) → Participates in elections but does not store data.

Why Replication is needed To keep your data safe 24/7 High Availability of data Disaster Recovery No downtime for maintenance (like backups, index rebuids) Replica set - group of two or more nodes One node is primary node and remaining node are secondary

Working of Replication The Primary Server is responsible for handling all write operations . Every change made in the Primary is recorded in a special log called the oplog (operations log) . The Secondary Servers replicate the Primary’s oplog asynchronously, ensuring they always have an updated copy of the data. Clients can send read requests to either the Primary or Secondary servers (depending on configuration). If the Primary fails , an automatic election process occurs and one Secondary is promoted to become the new Primary. This process ensures automatic failover and uninterrupted service.

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.

Example Setup Server A → Primary Server (handles read/write operations, stores “Name” and “Balance”). Server B & Server C → Replica Servers (store replicated copy of data, handle read operations). Applications write to Server A (Primary) but can read from any of the three servers.

How to Perform Replication in MongoDB Start MongoDB with Replica Set Name mongod --port 27017 -- dbpath "D:\setup\ mongodb \data" -- replSet rs0 This starts MongoDB on port 27017 with replica set name rs0 . 2. Connect to the Mongo Shell mongo --port 27017 3. Initialize Replica Set rs.initiate () 4. Check Configuration and Status rs.conf () rs.status ()

Key Features of Replication Replica sets consist of multiple nodes maintaining the same dataset. The Primary server records all write operations. Secondary nodes replicate changes asynchronously from the oplog . Heartbeat signals are exchanged between nodes; if the Primary fails, a Secondary becomes the new Primary.

Adding Members to Replica Set After initiating the replica set, you can add members (other servers rs.add (" hostname:port ") Example : rs.add ("mongod1.net:27017") This command adds a Secondary server to the Replica Set.

Benefits of Replication High Availability – No downtime during server failures. Data Redundancy – Safe storage across multiple servers. Automatic Failover – Secondary can become Primary. Load Balancing – Read operations can be distributed to replicas. Disaster Recovery – Data safety in case of crashes.

MongoDB Sharding Introduction Sharding in MongoDB is the process of splitting large datasets into smaller parts (called shards ) and distributing them across multiple servers. While replication is about data availability and fault tolerance , sharding is about scalability and performance . In simple words: Replication = Copies of same data on many servers (backup, failover). Sharding = Breaking data into pieces across many servers (scaling with big data).
Tags