0

I've successfully created and deployed a self-managed MongoDB cluster that consists of 3 shards,1 router(mongos), and 1 config-server replica set, using this tutorial.

I want to create a shareded collection, with the next requirements:

  1. documents should distribute evenly across all shards(in respect to total size, not docs count).
  2. documents in this collection can contain any field, and the only field that is guaranteed to exists is the _id field.
  3. the collection should be a time-series collection.
  4. MongoDB version 5.0.9

What I tried :

  1. It is ok to use the _id field exists in any doc in mongodb as your shard key for a sharded collection (not necessary time-series one)?
    I've tested it(because could not find answers online), configuring the the sharded collection with an hashed index created from the _id field:
sh.shardCollection("shardedDb.shardedCollection", { _id : "hashed" } 
)

and streamed a lot of dumy data.
for 4.5TB and 460 Millions records, the distribution was even and each of the 3 shard contained 1.5TB. Worked like charm. yet I need to ask, is it ok? why I don't see this solution proposed as quick solution anywhere else?

  1. When dealing with time-series collections in MongoDB, you can only create indexes from the timeField and the metaField (this is true, for Mongo V5, in V6 there is no limitation, but I can't upgrade my cluster version). in other words, you cannot create index from _id. so I tried using my timeField as the key shard but the data was not distributed evenly across the shards.

so what can I do? I can I reliably choose a shard key on time series predection collection on MongoDB V5 that would distribute the docs across all shards in a sized-even way? I can not guarantee a specific field and I can not create index from _id field on Time-series collection...

1 Answer 1

1

In your case, it's OK to use _id as the sharding field, because there is no other field that exists in all documents.

The reason why _id is not recommended is that it will distribute documents way that you cannot predict where they go. So, when the client makes a query, that query must be sent to all shards, always. The idea of sharding is to make a situation where the query is sent to only one (or a few) shards at time.

That time field is problematic, because you cannot "hook" insert/update operation and add time field there. However, you have there insert time in the document. The first four bytes of your _id are timestamp of creating time of the document.

2
  • Great insights friend, however, I did not asked about time field, I asked how can distribute data evenly on time series collection on my situation Aug 19, 2022 at 8:28
  • 1
    As you said, _id field is only field what is absolutely in all documents. So, it's only field what you can use. Hashing the sharding key is a only way to get evenly distributed shard, when you don't know values of your sharding key. But, as said, hash sharding is not recommended, because it creates a scatter-gatter situation, where all queries are sent to all shards.
    – JJussi
    Aug 20, 2022 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.