Question: What is sharding in MongoDB?


Sharding in MongoDB is a method for distributing data across multiple servers or instances. It's primarily used to scale MongoDB's capacity and performance beyond the limitations of a single server, allowing for horizontal scaling. Sharding effectively partitions large datasets and distributes them across multiple databases on different servers, known as shards. Each shard holds a subset of the data, making queries more efficient by targeting only the relevant shard(s).

How Does Sharding Work in MongoDB?

MongoDB uses a shard key to distribute data across shards. The shard key is a field or combination of fields included in every document in a collection. MongoDB hashes the shard key value and uses this hash to assign documents to shards, ensuring an even distribution of data.

Components of MongoDB Sharding

  • Shard: A MongoDB instance that holds a subset of the sharded data.
  • Config Servers: MongoDB instances that store metadata about the cluster. This metadata maps chunks of data to shards.
  • Query Routers (mongos): Intermediate query routers that direct operations to the appropriate shard(s) based on the shard key. Applications connect to these rather than directly to the shards.

Setting up a Simple Sharded Cluster

Setting up a sharded environment involves configuring shard servers, config servers, and query routers. Below is a simplified overview of setting up a basic sharded cluster:

  1. Initialize the Config Servers:

    Run multiple mongod instances as config servers. In production, you should have three for redundancy.

    mongod --configsvr --dbpath /data/configdb --port 27019
  2. Start the Shard Servers:

    Each shard in the cluster is a mongod process. Start each shard on its own port.

    mongod --shardsvr --dbpath /data/shard1db --port 27020 mongod --shardsvr --dbpath /data/shard2db --port 27021
  3. Start the Mongos Process:

    The mongos process acts as a query router.

    mongos --configdb configServer1IP:27019,configServer2IP:27019,configServer3IP:27019 --port 27017
  4. Add Shards to the Cluster:

    Connect to one of the mongos processes and use the addShard command for each shard.

    sh.addShard("shard1IP:27020"); sh.addShard("shard2IP:27021");
  5. Enable Sharding for a Database and Collection:

    Before sharding a collection, enable sharding for the database:


    Then, choose a shard key and shard the collection:

    sh.shardCollection("mydatabase.mycollection", { "myShardKey": 1 });

Benefits of Sharding

  • Scalability: Easily scales out to accommodate more data and handle more load.
  • Performance: Queries can be routed to the specific shard(s) containing the relevant data, reducing response times.
  • High Availability: Replica sets can be used within each shard for high availability.

Sharding is a powerful feature for managing large datasets and high throughput applications, but it requires careful planning and consideration of the shard key to ensure balanced data distribution and optimal performance.

Was this content helpful?

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.