Zone Based Sharding in MongoDB

MongoDB shard zonesIn this blog post, we will discuss about how to use zone based sharding to deploy a sharded MongoDB cluster in a customized manner so that the queries and data will be redirected per geographical groupings. This feature of MongoDB is a part of its Data Center Awareness, that allows queries to be routed to particular MongoDB deployments considering physical locations or configurations of mongod instances.

Before moving on, let’s have an overview of this feature. You might already have some questions about zone based sharding. Was it recently introduced? If zone-based sharding is something we should use, then what about tag-aware sharding?

MongoDB supported tag-aware sharding from even the initial versions of MongoDB. This means tagging a range of shard keys values, associating that range with a shard, and redirecting operations to that specific tagged shard. This tag-aware sharding, since version 3.4, is referred to as ZONES. So, the only change is its name, and this is the reason sh.addShardTag(shard, tag) method is being used.

How it works

  1. With the help of a shard key, MongoDB allows you to create zones of sharded data – also known as shard zones.
  2. Each zone can be associated with one or more shards.
  3. Similarly, a shard can associate with any number of non-conflicting zones.
  4. MongoDB migrates chunks to the zone range in the selected shards.
  5. MongoDB routes read and write to a particular zone range that resides in particular shards.

Useful for what kind of deployments/applications?

  1. In cases where data needs to be routed to a particular shard due to some hardware configuration restrictions.
  2. Zones can be useful if there is the need to isolate specific data to a particular shard. For example, in the case of GDPR compliance that requires businesses to protect data and privacy for an individual within the EU.
  3. If an application is being used geographically and you want a query to route to the nearest shards for both reads and writes.

Let’s consider a Scenario

Consider the scenario of a school where students are experts in Biology, but most students are experts in Maths. So we have more data for the maths students compare to Biology students. In this example, deployment requires that Maths students data should route to the shard with the better configuration for a large amount of data. Both read and write will be served by specific shards.  All the Biology students will be served by another shard. To implement this, we will add a tag to deploy the zones to the shards.

For this scenario we have an environment with:

DB: “school”

Collection: “students”

Fields: “sId”, “subject”, “marks” and so on..

Indexed Fields: “subject” and “sId”

We enable sharding:

And create a shardkey: “subject” and “sId” 

We have two shards in our test environment


Zone Deployment

1) Disable balancer

To prevent migration of the chunks across the cluster, disable the balancer for the “students” collection:

Before proceeding further make sure the balancer is not running. It is not a mandatory process, but it is always a good practice to make sure no migration of chunks takes place while configuring zones

2) Add shard to the zone

A zone can be associated with a particular shard in the form of a tag, using the sh.addShardTag(), so a tag will be added to each shard. Here we are considering two zones so the tags “MATHS” and “BIOLOGY” need to be added.

We can see zones are assigned in the form of tags as required against each shard.

3) Define ranges for each zone

Each zone covers one or more ranges of shard key values. Note: each range a zone covers is always inclusive of its lower boundary and exclusive of its upper boundary.

4) Enable balancer

Now enable the balancer so the chunks will migrate across the shards as per the requirement and all the read and write queries will be routed to the particular shards.

Let’s check how documents get routed as per the tags:

We have inserted 6 documents, 4 documents with “subject”:”maths” and 2 documents with “subject”:”biology”

Checking the shard distribution for the students collection:

So in this test case, all the queries for the students collection have routed as per the tag used, with four documents inserted into shard0000 and two documents inserted to shard0001.

Any queries related to MATHS will route to shard0000 and queries related to BIOLOGY will route to shard0001, hence the load will be distributed as per the configuration of the shard, keeping the database performance optimized.

Sharding MongoDB using zones is a great feature provided by MongoDB. With the help of zones, data can be isolated to the specific shards. Or if we have any kind of hardware or configuration restrictions to the shards, it is a possible solution for routing the operations.

Share this post

Comments (5)

  • Manoj George

    Nice article. can the zone be used with Hash shard key. If so, could you please an example or some link to that

    July 21, 2018 at 9:46 am
  • Aayushi Mangal

    Thanks Manoj, yes, zones could be used with Hashed shard key, but with the hashed shard keys, upper and lower bound values will represent the hashed value for the field instead of actual value, and this might leads to broadcast operations and thus unexpected behaviour.
    Please refer more details on zones with hashed shard keys from here:

    August 30, 2018 at 4:21 am
  • Chris Scott

    Can you have a default Zone? If you have a large number of clients and you want to separate one client “Amazon” onto its own zone, (Zone A). All the other clients default to a different zone, (Zone B). I do not want to have to define all the other clients to Zone B as every day more clients are being added and there are hundreds of them.

    February 26, 2019 at 6:13 pm
  • samba sarnala

    Hi Aayushi this post is very helpful , i have a six org_ids like 1,2,3,4,5,6.
    i have 3 sharded cluster like sh0,sh1,sh2.

    I want to move data using below :
    1. org_ids 1 and 5 to sh0 shard
    2.org_ids 2 and 4 to sh1 shard
    3.org_ids 3 and 6 to sh2 shard

    Could you please help me i am facing this issue.

    Samba S

    April 15, 2019 at 10:01 am
    • Aayushi Mangal

      Hello Samba, I am glad that you found my blog helpful, please share what issue are you facing?

      August 29, 2019 at 7:54 am

Comments are closed.

Use Percona's Technical Forum to ask any follow-up questions on this blog topic.