Database Reference
In-Depth Information
Tag Sharding
Sometimes, under certain circumstances it makes sense to say “I wish I could have all of that data on this shard”. This
is where MongoDB's tag sharding can shine. You can set up tags so that given values of your shard key are directed at
specific shards within your cluster! The first step in this process is to work out what you want to achieve with your tag
setup. In the next example we will work through a simple setup where we want to have our data distributed based on
geography, with one location in the US and another in the EU.
The first step in this process is to add some new tags to our existing shards. We do this with the sh.addShardTag
function, simply adding the name of our shard and a tag we wish to give it. In the example I have made shard0000 the
US shard and shard0001 the EU shard:
> sh.addShardTag("shard0000","US");
> sh.addShardTag("shard0001","EU");
Now, in order to view these changes we can run the sh.status() command and review the output:
> sh.status();
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" : ObjectId("51c699a7dd9fc53b6cdc4718")
}
shards:
{ "_id" : "shard0000", "host" : "localhost:27023", "tags" : [ "US" ] }
{ "_id" : "shard0001", "host" : "localhost:27024", "tags" : [ "EU" ] }
...
As you can see, our shards now have the US and EU tags against their names, but these alone will do nothing; we
need to tell the MongoS to route data for our given collection to those shards based on some rules. This is where the
tricky part comes in; we need to configure our sharding so that the data we are sharding on contains something we
can perform the rule evaluations against in order to route them correctly. In addition to this, we still want to maintain
the same distribution logic as before. If you recall from the earlier discussion how chunks are broken down, you can
see that for the most part we just need to have this breakdown by region occur “before.”
The solution here is to add to our shard key an extra key that represents the region the data belongs in and have
this as the first element of our shard key. So, now we need to shard a new collection in order to get these tags added:
> sh.shardCollection("testdb.testtagsharding", {region:1, testkey:1})
{ "collectionsharded" : "testdb.testtagsharding", "ok" : 1 }
While the tag portion of a key does not need to be the first element, it is often best that it is; this way, chunks
are first broken down by tag.
Note
At this point we have our tags set up, we have our shard key, which will break our chunks up into nice regional
pieces, and now all we need is the rules! To add these we use the sh.addTagRange command. This command takes the
namespace of our collection, along with minimum and maximum range values, and the tag to which that data should
be sent. MongoDB's tag ranges are minimum-inclusive and maximum-exclusive. Thus if we want anything that has a
 
 
Search WWH ::




Custom Search