A MongoDB sharded cluster consists of the following components:
shard: Each shard contains a subset of the sharded data. Each shard must be deployed as a replica set.
Routing with mongos: The mongos acts as a query router, providing an interface between client applications and the sharded cluster.
config servers: Config servers store metadata and configuration settings for the cluster. Config servers must be deployed as a replica set (CSRS).
Replica Sets: Replica sets consist of multiple nodes (usually an odd number for elections) that contain identical copies of the data.
Write and Read Operations: The primary node handles write operations, while secondary nodes provide read scaling by distributing read queries.
Automatic Failover: If the primary node goes down, a secondary node is automatically promoted to primary, ensuring continuous availability.
Oplog: A special capped collection on the primary node that records all changes. The secondary nodes use this log to keep themselves updated.
MongoDB sharding horizontally splits your data across multiple shards (each typically a replica set), so a 4‑shard cluster spreads one logical collection’s data over 4 independent data-bearing groups of nodes.mongodb+1
Sharding is MongoDB’s horizontal scaling method: a single logical dataset is partitioned and stored on multiple servers called shards.geeksforgeeks+1
A sharded cluster has three main components:
Shards: store subsets of your data (each shard is a replica set).mongodb+1
Config servers (3‑member CSRS): store metadata like chunk ranges and shard locations.geeksforgeeks+1
Mongos routers: your applications connect here; mongos routes reads/writes to the right shard.mongodb+1
When you shard a collection, you must choose a shard key (one or more fields) that determines how documents are partitioned.devopedia+1
MongoDB divides the shard key space into ranges called chunks; each chunk covers an inclusive lower and exclusive upper bound of shard key values.bmc+1
The balancer process runs in the background, moving chunks between shards so each shard holds roughly the same number of chunks and data, avoiding hotspots.mongodb+1
Range sharding uses contiguous ranges of the shard key (for example, dates or IDs), which is good for range queries but needs care to avoid all new inserts landing on one shard.mongodb+1
Hashed sharding hashes the shard key value before assigning to a chunk, which spreads inserts more evenly across shards, especially for monotonically increasing keys.200oksolutions+1
In a 4‑shard cluster, each shard owns some set of chunks; together, all shards’ chunks represent the full data set of each sharded collection.scylladb+1
When new documents are inserted, mongos uses the shard key, looks up the owning chunk in the config metadata, and sends the operation to the shard that currently owns that chunk.mongodb+1
As data grows or becomes unbalanced, the balancer splits large chunks and migrates some chunks from “heavier” shards to “lighter” shards so that all 4 shards stay close in chunk count and data size.percona+1
For your 4‑shard, 3‑config, multi‑mongos environment, the high‑level steps are:
Connect to mongos and enable sharding on the database:
sh.enableSharding("yourDb") enables that database for sharding.percona+1
Create an index on the chosen shard key fields on the target collection
(for example, { userId: 1 } or { userId: "hashed" }).geeksforgeeks+1
Shard the collection:
sh.shardCollection("yourDb.yourColl", { userId: 1 }) for range sharding, or
sh.shardCollection("yourDb.yourColl", { userId: "hashed" }) for hashed sharding.mongodb+1
After this, MongoDB creates initial chunks for that collection and starts placing them on the 4 shards; the balancer will keep moving chunks to maintain even distribution as data grows.bmc+1
https://www.geeksforgeeks.org/mongodb/manage-sharded-cluster-balancer-in-mongodb/
https://devopedia.org/mongodb-sharding