How to implement a comments sections & store comments for different posts in dynamodb?

0

I want to implement a comments section for each post in my social media app using DynamoDB. I tried a few design approaches, but I'm facing some challenges.

1] Storing all comments for all posts in a single table called "comments" is impractical because I would need to scan(which is an expensive operation) the entire table with postID, every time a user opens a comment section. This approach would create a forest of comments.

2] Creating a separate table for each post's comment section is also impractical because I would need to create millions of tables for each post.

3] Storing comments in a single attribute of a post item in postsData table as a map is also impractical because the overall item/attribute size limit is 400 KB, which would definitely be exceeded and I expect some posts to have thousands of comments.

HOW DO I STORE THE COMMENTS & REPLIES THEN? How many tables do I need to have? I already have like 2 tables each for usersData, and postData.

I need a solution that is scalable, cost-friendly and efficient for queries across multiple posts, thanks!

Saurabh
asked 7 months ago185 views
1 Answer
1

Its a pretty simple data model, one table to hold comments and replies for all posts:

PKSKData
POST#123COMMENT#001some comment
POST#123COMMENT#001#REPLY#001some reply to comment 001
POST#123COMMENT#001#REPLY#002some reply to comment 001's reply 001
POST#123COMMENT#002some comment
POST#123COMMENT#003some comment
POST#123COMMENT#003#REPLY#001some reply to comment 003
POST#334COMMENT#001some comment
POST#334COMMENT#001#REPLY#001some reply to comment 001
POST#920COMMENT#001some comment

Here I have shown with incrementing numbers, which is likely not practical, I would suggest using ULIDs as part of the sort key, that will give you unique ID's which are time sortable so you can render a posts comments and replies in the order they were received.

Now to get all the comments and replies for a post its as simple as: SELECT * FROM commentsTable WHERE PK = 'POST#123'

This is both scalable and highly efficient.

profile pictureAWS
EXPERT
answered 7 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions