In MongoDB, a replica set is a group of MongoDB servers that maintain the same data set. Replica sets provide redundancy, high availability, and data protection. The terms
primary and secondary refer to the roles that each member of the replica set can take.
1. Primary Replica Set
Role: The primary is the main node that receives all write operations. It is the source of truth for all data in the replica set.
Writes: Only the primary can accept write operations. When a write operation is performed (e.g., insert, update, delete), it is written to the primary replica first.
Reads: By default, read operations are routed to the primary, though read preferences can be configured to allow reads from secondary nodes as well.
Election: If the current primary becomes unavailable (e.g., due to failure or maintenance), the replica set will automatically elect a new primary from the secondaries using an election process. This ensures continuous availability of the database for writes.
Data Sync: The primary constantly replicates its data to the secondary members to keep them up to date.
2. Secondary Replica Set
Role: Secondary replicas are copies of the primary, and they maintain an exact replica of the data from the primary node.
Reads: By default, secondary nodes do not accept write operations. However, read operations can be routed to them using specific read preferences (e.g.,
secondary or nearest).
Replication: Secondary replicas replicate data from the primary (or from another secondary in case of a lag in replication). They apply the changes from the primary's oplog (operation log) to stay in sync.
Data Sync: Secondaries continuously receive and apply data changes from the primary. If a secondary is down for a period, it catches up with the primary once it becomes available again.
Failover: In the event of a primary failure, one of the secondary nodes is automatically promoted to primary through an election process, ensuring the system remains operational with minimal downtime.
Key Differences:
Feature
Primary
Secondary
Write Operations
Accepts write operations
Does not accept write operations
Read Operations
Default for read operations
Can be configured for read operations
Replication
Replicates data to secondaries
Replicates data from the primary or another secondary
Election
Elected node, initiates writes
Can be promoted to primary during an election
Health Check
Critical for replica set operation
Can be down temporarily without affecting writes
Summary:
Primary: Handles all write operations and acts as the authoritative source for data.
Secondary: Copies data from the primary, can serve read operations, and can be promoted to primary in case of failover.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In MongoDB, a replica set is a group of MongoDB servers that maintain the same data set. Replica sets provide redundancy, high availability, and data protection. The terms primary and secondary refer to the roles that each member of the replica set can take.
1. Primary Replica Set
2. Secondary Replica Set
secondaryornearest).Key Differences:
Summary: