Introducing Dragonfly Cloud! Learn More

Question: How do you increase shards in MongoDB?

Answer

In MongoDB, sharding is the process of storing data records across multiple machines and it's a common approach used to scale horizontally. When the need arises to increase the number of shards in a MongoDB cluster, you're essentially adding more servers to distribute the data more efficiently and handle more operations. Here's a high-level overview of how to increase shards:

  1. Deploy a New Shard: First, deploy a new replica set to serve as the additional shard. Each shard in a MongoDB cluster is a replica set.

    mongod --shardsvr --replSet newShardReplicaSetName --dbpath /data/db/newShard --port 27018
  2. Initialize the Replica Set: Connect to one of the mongod instances in the new shard and initiate the replica set.

    mongo --port 27018 > rs.initiate()
  3. Add the New Shard to the Cluster: Connect to a mongos instance and add the new shard to the cluster.

    mongo --port 27017 # Assuming 27017 is the port for mongos > sh.addShard("newShardReplicaSetName/newShardHost1:27018")
  4. Enable Sharding for Collections (if necessary): If you are adding a shard to a cluster that already has databases and collections but they are not yet sharded, you can enable sharding on them.

    > sh.enableSharding("databaseName") > sh.shardCollection("databaseName.collectionName", { "shardKey": 1 })

When choosing a shard key, consider the fields that will allow for even distribution of data and queries across shards.

Considerations:

  • Capacity Planning: Before increasing shards, assess your current and future capacity needs. Adding more shards increases complexity and operational overhead.
  • Balancing: After adding a new shard, MongoDB will start balancing chunks of data between the old and new shards automatically. This process might affect performance temporarily, so plan accordingly.
  • Security: Ensure your new shards are secured, following best practices for authentication, authorization, and encryption."

Note that the steps and code examples provided are illustrative. Your actual implementation details (like port numbers, replica set names, and paths) may vary based on your environment and configuration.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

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