Concept
Replica Sets & High Availability
A Replica Set is a group of MongoDB processes that maintain the same data set. It consists of:
- Primary Node: Receives all write operations.
- Secondary Nodes: Replicate data from the primary asynchronously. If the primary goes offline, secondary nodes elect a new primary automatically.
┌──────────────────────┐
│ Client App │
└──────┬────────┬──────┘
Writes │ │ Reads (e.g. secondaryPreferred)
▼ ▼
┌──────────┐ ┌──────────┐
│ Primary ├─▶Secondary │
│ (Write) │ │ (Read) │
└──────────┘ └──────────┘Read Preferences
Clients can route read queries to specific nodes inside the replica set using Read Preferences: