Databases 13 min read

MongoDB Cluster Architecture: Master‑Slave, Replica Set, and Sharding

This article explains MongoDB's three cluster architectures—deprecated Master‑Slave replication, the highly available Replica Set with automatic failover, and horizontal scaling Sharding—detailing their roles, data flow, advantages, limitations, and operational considerations for building reliable, scalable databases.

Architect
Architect
Architect
MongoDB Cluster Architecture: Master‑Slave, Replica Set, and Sharding

MongoDB provides three cluster deployment models: the legacy Master‑Slave (now discouraged), the Replica Set for high‑availability replication, and Sharding for horizontal scaling.

Master‑Slave mode uses a single primary node that handles both reads and writes, while one or more secondary nodes replicate data from the primary and serve read‑only traffic. This model suffers from data‑staleness on secondaries, requires manual failover, and has been deprecated since MongoDB 3.6.

Replica Set mode replaces Master‑Slave. A replica set consists of Primary , Secondary , and optional Arbiter members. The primary receives all writes and records operations in the oplog (stored in the local database). Secondaries replicate the oplog to stay in sync and can be promoted automatically when the primary fails, providing automatic failover and high availability. The oplog entry format is shown below:

{
    "ts" : Timestamp(1446011584, 2),
    "h"  : NumberLong("1687359108795812092"),
    "v"  : 2,
    "op" : "i",
    "ns" : "test.nosql",
    "o"  : { "_id" : ObjectId("563062c0b085733f34ab4129"), "name" : "mongodb", "score" : "10" }
}

Replication proceeds via an initial full sync followed by continuous keep (incremental) sync. Elections are triggered by heartbeats; any node can become primary after a two‑phase election with majority voting, ensuring the cluster remains operational without manual intervention.

Sharding mode distributes large collections across multiple shard servers (each a replica set) to achieve near‑unlimited storage and throughput. The architecture includes a Router Server (mongos) that routes client requests, Shard Servers that store data, and a Config Server that holds metadata. Data is partitioned using a shard key into chunks . MongoDB supports three sharding strategies:

Hashed Sharding : hashes the shard key to assign chunks, offering fast distribution and good balance but poor range query performance.

Ranged Sharding : stores chunks in key order, enabling efficient range queries but can create hotspot writes if many documents share similar key prefixes.

Zone Sharding : groups chunks into zones to control data placement on specific shards, combining benefits of range sharding with locality control.

In summary, the Master‑Slave model is obsolete, the Replica Set provides automatic failover and strong consistency, and Sharding enables horizontal scaling for massive data sets. Choosing the appropriate model depends on consistency requirements, fault‑tolerance needs, and workload characteristics.

ShardingHigh AvailabilityMongoDBdatabase replicationCluster ArchitectureReplica Set
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.