Skip to content

Chapter 6 of 8

Sharding for Horizontal Scaling

Sharding is MongoDB's strategy for horizontal scaling, enabling databases to handle data sets and throughput levels that exceed the capacity of any single server. In a sharded deployment, data is distributed across multiple machines called shards, with each shard holding only a subset of the total data. The basis for this distribution is a shard key, an indexed field or compound of fields that MongoDB uses to determine where each document belongs.

A sharded cluster consists of three main components. Shards store the actual data, and each shard is itself a replica set, providing both scalability and redundancy. Config servers store metadata and configuration information for the entire cluster, including the mapping of shard key ranges to shards. Mongos instances act as query routers, receiving client operations and directing them to the appropriate shards based on the shard key. Clients typically connect to one or more mongos processes rather than directly to shards, which insulates applications from the underlying distribution of data.

Choosing a good shard key is one of the most consequential decisions in sharded cluster design. An effective shard key should have high cardinality, low frequency so that no single value dominates, and should distribute writes evenly across shards. A poor shard key leads to hot spots, where one shard receives disproportionately more traffic and becomes a bottleneck. Once chosen, the shard key is difficult to change, so careful planning and testing are essential before deployment.

All chapters
  1. 1Foundations of MongoDB
  2. 2CRUD Operations and Querying
  3. 3The Aggregation Framework
  4. 4Indexing and Query Performance
  5. 5Replication for High Availability
  6. 6Sharding for Horizontal Scaling
  7. 7Schema Design Patterns
  8. 8Advanced Features and Ecosystem

Drill it

Reading is not remembering. These come from the Nosql MongoDB deck:

Q

What is MongoDB?

MongoDB is a NoSQL document-oriented database that stores data in flexible, JSON-like documents called BSON. It is designed for scalability and high performance...

Q

What is the document model in MongoDB?

The document model stores data as documents, which are rich data structures similar to JSON objects. Each document can contain nested fields, arrays, and sub-do...

Q

What is BSON?

BSON (Binary JSON) is the binary-encoded serialization format MongoDB uses to store documents and make remote procedure calls. It extends JSON with additional d...

Q

What is an ObjectId in MongoDB?

An ObjectId is a 12-byte unique identifier automatically generated by MongoDB as the default _id field for each document. It consists of: A 4-byte timestampA 5-...