Skip to content

How to work with MongoDB Sharding Cluster

In the previous post, I have shared with you how to setup a simple MongoDB Sharding Cluster. In this post, I will share with you about sharding your database and how to work with a sharding mongodb cluster from Spring boot.

Getting Started

Before getting started, of course, you have to setup your sharding cluster successfully. If you’re not, check this out.

Configure Sharding

To configure sharding there are 2 things you need to know:

  • The sharding level: you must enable sharding on your database and on specific collection. You don’t have to shard all you collections, it’s depend on your need, it may be more efficient to store collections in one location since database queries to a single shard are faster.
  • Sharding strategy: before sharding your collections, you have to consider thoroughly about the sharding strategy. For more detail please have a read on this guide.

Enable Sharding at Database Level

First, we’ll enable sharding at the database level, which means that collections in a given database can be distributed among different shards.

  1. Access the mongos shell on your query router. This can be done from any server in your cluster:
  2. From the mongos  shell, create new or use your old database
  3. Enable sharding on your database
  4. Verify that sharding was successful or not, first switch to config  database:

    Then run find()  method on config database

    The result should contain your database!

Enable Sharding at Collection Level

At this step, your database is available for sharding, we need to enable sharding at collections level. This allows documents within a collection to be distributed among your shards. For simplicity, I will use a hash-based strategy for enable sharding collections.

  1. Connect to your mongo  shell on query router and select your database
  2. Create a new collection to enable sharding or use your existing collection and enable sharding by hash its _id  key
  3. Finally, shard the collection

Test Your Cluster

To test your cluster let’s insert a lot of data into the sharded collection. Then check your data distribution by

This will output information about your shards, how many data is on which shard…

Configure Spring Mongo To Work With Sharding Cluster

Your cluster is now up and your collections is sharded. This time is to work with your cluster. I’m using Spring Mongo to work with my sharding cluster.

As mentioned in the architecture of the cluster, the query router is the interface between the client application and the cluster shards. So that, let’s configure your code to connect to the query router.

Open the application.yml (or with difference syntax) file and added the following setup

That’s all. From now on, all CRUD operations will be auto execute on your sharding cluster!


Published inBig DataJavaMongoDBServer

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *