Design data structure for storing data in key-value databases for blog website

176 views Asked by At

I need to create a blogging website from end to end by using a key-value database as a primary database. But when I create the data structure for the project to store and query data based on project requirements, I cannot think how can I design the data model for 2 functions "vote for the favorite blog post" and "search blog posts by categories". For the function "vote for the favorite blog post", the requirement says "The website allows users to vote favorite blog post, if any blog post reaches 150 votes, it will be ranked as a blog of interest. If the website has about 30 blog posts of interest per day, the website will place these 30 blog posts off the main page and called them are "Top 50 blog posts of interest". Each blog post will has the corresponding score to evaluate later. And the website will not allow user to vote one blog post many times"

Now I need to design the data structure like that for 2 functions above. But I don't have any idea, especially function "vote for the favorite blog post".

For example, with the function "Each blog post will have comments from reader" I will design the data structure in key-value databases like:

CMT:$BLOG_ID:$RATING: [ {"VIEWER_ID":"", "timestamp":"","image":"","comment":""},{..},{...}].

Can anyone give a hint about this? Thank you so much.

1

There are 1 answers

0
Asad Awadia On

vote for the favorite blog post

A counter at blog:id:votes which is incremented when a blog is upvoted

After incrementing if it has reached 150 votes then add the id to the list of 50 which will be at another key(s)

search blog posts by categories

Create an invertex index that goes from categories to the blog post id

So if a blog is created with categories food and music - you should generate 2 keys like music:blogId and food:blogId

Then to search you do a prefix scan over the categories:* and grab the blog ids that match the category