How do you rank users like the post, reactions have their own weight?
Every reaction carries +1 weight. The most helpful users are sorted according to the number of reactions they accumulate across all their content in a specific duration.
Are you directly fetching from users collection? do they have a history for their activities in that collection?
No, we have a separate collection specifically for reactions. You really shouldn't bloat a single collection like users with a lot of data. MongoDB anyway has a 16MB limit per document. So, it's best to keep data like this separate.
To store reactions, we've a collection called reactions which looks something like this (minimal version):
We also track the reactions across different tags so that we can find out the most relevant people to follow in specific tags.
When we need to find out the most helpful people in a certain duration, we just run MongoDB aggregation and find the top N number of users from this collection. Naturally, this operation is heavy. So, we cache the result for about an hour before re-querying.
Are you caching results for a specific duration? Currently how long it will take to execute this query without caching?
The first part is answered above. I don't have the exact numbers with me, but I'll find that out once I am back on my desk and update the answer.
Also, provide if you have any interesting facts around it?
Initially, I thought of caching the aggregation results in a redis cache with an expiry of ~1hr. After 5 mins I realized that I was being stupid since we are already using Cloudflare which can cache the result on their CDN and save me a lot of work! :)
Thanks for the answer @sandeep, Its really cool. What about that duration mentioned 7d and 30d? i tried with other days but it didn't work. Are you restricted other days? and how do you convert duration into actual days in querying?
Hi Thamaraiselvam,
Thanks for the question! Here are my thoughts:
Every reaction carries +1 weight. The most helpful users are sorted according to the number of reactions they accumulate across all their content in a specific duration.
No, we have a separate collection specifically for reactions. You really shouldn't bloat a single collection like
userswith a lot of data. MongoDB anyway has a 16MB limit per document. So, it's best to keep data like this separate.To store reactions, we've a collection called
reactionswhich looks something like this (minimal version):We also track the reactions across different tags so that we can find out the most relevant people to follow in specific tags.
When we need to find out the most helpful people in a certain duration, we just run MongoDB aggregation and find the top N number of users from this collection. Naturally, this operation is heavy. So, we cache the result for about an hour before re-querying.
The first part is answered above. I don't have the exact numbers with me, but I'll find that out once I am back on my desk and update the answer.
Initially, I thought of caching the aggregation results in a redis cache with an expiry of ~1hr. After 5 mins I realized that I was being stupid since we are already using Cloudflare which can cache the result on their CDN and save me a lot of work! :)
Hope this helped! :)