1

Lets have two variants of nosql collection for storing discussion comments. Assume discussion, where is only one thread/root comment with 100 replies.

A./

collection: discussion_thread (id, discussion_id, comments: Comment[])

  • comments is recursive tree structure (Comment = {content: string, comments: [...recursion...]}) -For given discussion example, there is only one document in discussion_thread, that holds all comments.

B./

collection: comment (id, discussion_id, root_comment_id, parent_comment_id, content) -- I know there could be slug

  • For given discussion example, there will be 101 documents in comment collection

Lets assume:

  • the database has 500GB and is growing, collections are sharded by discussion_id with master/slave replication
  • we have 1000 request /minute, 90% reads
  • on each write (e.g. upvote of comment) we need to rebuild the cache for whole thread, for fast read/serving

To be reasonably brief, in this question I would like to focus only on cache rebuilding. So we need to read all the comments for given thread / root_comment.

In A./ we need one lookup to get 1 document with all comments. But this structure is quite problematic to write into(at least because potentially all concurent writes to single thread ends up on one document).

In B./ to get all of the 101 comments of the example thread, mongo needs to find and return 101 documents. Because there is index on discussion_id and root_comment_id and sharding on discussion_id, do you think, that the B variant will be significantly slower then the A?

Do You think there is some other major disadvantage in one of the variants? Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.