Tag Aware Sharding

  • tag_aware_sharding

When we shard a collection MongoDB distributes its data across all the shards of the cluster. MongoDB uses chunks, split and round balancing for getting it. For all of you who want to read about it here it is our last post MongoDB Pre-Splitting.

This is how MongoDB allows our apps to scale, this is called “horizontal scale”.

In certain occasions we need to store concrete data at a given shard. For example, think about a cluster whose shards are geographically dispersed and we need that data related to users of a specific country is stored at the nearest shard.

Ok, we only need to tag our shards and assign this tag to the chunks we want to store in the shard with the same tag.

Tagging our shards

Suppose we have a cluster with three shards, called s0, s1 and s2. The next commands will allow us to tag them:

Tagging ranges

Our next task, as I mentioned before, will be to tag the chunks with the same tag of the shard where we want our chunks belong to. Let’s go:

We can check it by querying the “tags” collection, from the “config” database:

We have all the work done. When inserting data in our collection, the chunks will be created and distributed as we have previously configured.

Let’s look at our cluster:

The last step is to insert some documents and to check their locations:

We connect to the Primary of s0 shard:

And we can check that it stores the right documents:

Now we repeat the same step with the s1 shard:

And finally, with s2 shard:

Ok, we have reached the end of our post and checked that the Tag Aware Sharding concept is not so difficult as we thought, right? Anyway, if you have some question, doubt or have noticed a mistake or perhaps something not explained enough, please, do not hesitate to write about it at the comments.

Juan Roy

MongoDB Fan & Financial Software Developer

You may also like...

Leave a Reply