What is Sharding in MongoDB?
Sharding distributes data across multiple machines by partitioning collections on a shard key. The way MongoDB scales horizontally beyond a single server's capacity.
Shard key — chosen at collection level; determines which shard a document lives on. Pick carefully — hard to change.
Chunks — data partitioned into chunks; balanced across shards automatically.
mongos router — routes queries to the right shard; clients only talk to mongos.
Config servers — metadata replica set tracking which chunk lives where.
Hashed vs ranged — hashed gives even distribution; ranged gives query locality.
Use only when needed — adds complexity; vertical scaling is simpler if it fits.
Know the three components: shards, config servers, mongos. Shard key selection is the most critical decision — high cardinality + even distribution + query pattern alignment. Ranged vs hashed sharding trade-offs.
Sharding is for datasets too large for a single server. Each shard is a replica set.