Database Management System (DBMS) / Clustering, Partitioning and Sharding

Replica Set (Clustering)

A Replica Set in the context of clustering is a group of database servers that work together to ensure high availability and data redundancy. It’s a term most commonly associated with MongoDB, but the concept applies to many distributed database systems.


🔹 What is a Replica Set?

A replica set is a cluster of servers (nodes) that all maintain the same dataset. One server is primary, and the others are secondaries. The secondaries replicate the data from the primary to provide backup, read scaling, and failover support.


🔸 Why Use Replica Sets?

  • ✅ High Availability – If the primary goes down, a secondary is automatically elected as the new primary.

  • ✅ Data Redundancy – Data is copied to multiple servers.

  • ✅ Disaster Recovery – Protects against data loss.

  • ✅ Read Scalability – You can offload read queries to secondaries.


🔹 Components of a Replica Set

Component

Role

🟩 Primary

Accepts all write operations.

🟦 Secondary

Replicates data from primary. Can serve read requests (if configured).

🟥 Arbiter

Votes in elections but doesn’t store data. Useful for tie-breaking.


🔸 How It Works

  1. Write Operation → Goes to the Primary.

  2. Replication → Primary’s data is copied (asynchronously or with write concern) to secondaries.

  3. Failover:

    • If the primary fails, secondaries hold an election.

    • One is elected as the new primary.

  4. Clients automatically connect to the new primary.


🔹 Replica Set vs Clustering

Feature

Replica Set

Sharded Cluster

Goal

Redundancy & high availability

Horizontal scaling of data

Nodes

Same data on all nodes

Data is partitioned across shards

Use Case

Backup and failover

Big data systems needing scalability

Example

MongoDB Replica Set

MongoDB Sharded Cluster



🛠️ Use Case Example (MongoDB Command)

rs.initiate({
  _id: "rs0",
  members: [
    { _id: 0, host: "localhost:27017" },  // Primary
    { _id: 1, host: "localhost:27018" },  // Secondary
    { _id: 2, host: "localhost:27019" }   // Secondary
  ]
})

🔚 Summary

  • A replica set is a basic form of clustering focused on availability and fault tolerance.

  • It ensures your system stays operational even if one or more servers fail.

  • Commonly used in MongoDB, MySQL Group Replication, and PostgreSQL streaming replication.