Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB shard key

I've been thinking about selecting the best shard key (through a compound index) for my data and thought the combination of the document creation date combined with a customer no. (or invoice no.) would be a good combination. IF MongoDB would consider the customer no as a string backwards ie.:

90043 => 34009
90044 => 44009
90045 => 54009
etc.

Index on the The creation date would ensure that relatively new data are kept in memory and the backward customer no would help MongoDB to distribute the data/load across the cluster.

Is this a correct assumption? and if so... would I need to save my customer no reversed for it to be distributed the way I expect?

like image 265
Jacob Avatar asked Mar 28 '26 08:03

Jacob


1 Answers

Regarding your specific question of "would I need to save my customer no reversed for it to be distributed the way I expect?", no - you would not.

Even with the relatively narrow spread of customer number values you listed, if you use customerNumber in your compound key, MongoDB will break apart the data into chunks and distribute these accordingly. As long as the data associated with customerNumber are relatively evenly distributed (e.g., one user doesn't dominate the system), you will get the shard balancing you desire.

I would consider either your original choice (minus the string reversal) or Dan's choice (using the built-in ObjectId instead of timestamp) as good candidates for your compound key.

like image 199
SethO Avatar answered Mar 31 '26 10:03

SethO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!