Tuesday, September 10, 2019

MOngoDb : Sharding Keys

Always pick the field with High Cardinality as shard key, so that the data is distributed well among all shard servers.

Sharding can be based on Chunks defines keys  and tags defines keys.

Chunks defines keys  :
the shard key you pick should be such that it provides a god variety over all documents. Ideally we should always be able to take a chunk , a range of keys and split them futhur as more documetns are added. Mongo will automatically split the chunks in smaller chunk when it feels a chunk is too large.

                                             



Pic1: Large chunk: Minkey 1 Max key 4                       Pic 2: Chunk split into 2 chunk, 1-2,2-4


Tags defines keys:
lets you designate a shard for a range of keys . Lets consider where we have one shard in a DataCenter in Amstermdam, other shard in NewZEaland. The latency will be increased. If would be cool so say mongo, if you see a phone prefix from any european country, those should be spread among the shard in Amsterdam DC.


Say the phone number data, all country with european phone prefix goes to Europe,  and American phone prefix in american shard.

Lets say if below exmaple, the applications send a data, mongo inspects the shard key value in that documetns. It finds a tag range defination that covers that shard key value. Using the tag name it will locate a chunk that lives on one of the shards that is marked with the tag. If a chunk dosent exists, it will create one on of the shards that has a specified tag.




Example:


mongos> sh.status()
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("5d75ed206988427493d216bc")
  }
  shards:
        {  "_id" : "r1",  "host" : "r1/localhost:30001,localhost:30002",  "state" : 1 }
        {  "_id" : "r2",  "host" : "r2/localhost:30003,localhost:30004",  "state" : 1 }
  active mongoses:
        "3.6.14" : 1

Lets tag them 

mongos> sh.addShardTag('r1','europe')
{
        "ok" : 1,
        "operationTime" : Timestamp(1568108258, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1568108258, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
mongos> sh.addShardTag('r2','america')
{
        "ok" : 1,
        "operationTime" : Timestamp(1568108273, 2),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1568108273, 2),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
mongos>

mongos> sh.status()
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("5d75ed206988427493d216bc")
  }
  shards:
        {  "_id" : "r1",  "host" : "r1/localhost:30001,localhost:30002",  "state" : 1,  "tags" : [ "europe" ] }
        {  "_id" : "r2",  "host" : "r2/localhost:30003,localhost:30004",  "state" : 1,  "tags" : [ "america" ] }
  active mongoses:
        "3.6.14" : 1


Creating a tag range: it defines a range that is assigned to a tag, helps balancer to automatically assign chunks to desired shards.

I below exmaple, in messages collections, i want my country prefix '1' and '2' to live in the shard america. SO lower bound is 1 and upper bound is country prefix 2.
Note: Lower bound in inclusice, upper bound is exclusive. So upper bound will not fall in this range that is 2. Same logic, 2,3, 4 go to europe. 5,6 again to america. and 6.9 to austratila.

And best is from 9 to infinity.  Any prefix from 9 to anythong will go to europe.



Now will enable sharding and shared messges collection



We see 1 chunk already created.



Once you enable sharding , you would like to move the chunks to there repective shards. and that would the right direction.
I will ask mongo to move the chunk with prefix 0 to america shard. ALso prefix to move to america and so on for europe chunks.


Lets see sh.status, see chunks moved as per the tags






Chunks has the min key and max key.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home