Mongodb – Ensure Mongo sharding rebalance latest chuck

mongodbsharding

The shard key i use is :

"_id" : {
        "pdm" : "1990-07-11",
        "hdocid" : "f23b7d7b2fc94339aed19983baddaeb3"
    }

pdm is a day level grouping key.
hdocid is a random hash key.

When I add new shard to the existing mongo, it rebalance the old chuck like "1990-07-10" rather then newer data like "1990-07-11".

The data I insert is from 1990-07-10 -> 1990-07-20 by order.

Thanks.

Best Answer

Yes, it is balancing "old" data because your choose bad shard key. Shard key should not start with linearly increasing key like time/date/number. Because (of the key) you add always data to the "end" of chunk, chunk is splitted at middle and lower (older) part is moved away.

If you want to distribute your query/insert load evenly to whole shard you could use {pdm:"hashed"} as shard key. Of course changing order of those two keys {hdocid:1, pdm:1} would distribute load too.