I am trying to implement in LIKE and COMMENT system like Instagram and Facebook in DynamoDB
I have 3 tables, user, photo and photo_likes
user table and photo table have user_id and photo_id keys.
and for photo_likes table, I have photo_id as a key and liked_by column where I store user_id as list.
So, If the photo_id 1 is liked by user_id 10, 35, I store it as:
| photo_id | liked_by |
| 1 | {10,35} |
I am really confused if it is the right way to do it? or should I just insert a new row whenever their is a new like.
| photo_id | user_id |
| 1 | 10 |
| 1 | 35 |
I will provide the advantage and disadvantage of the above approaches. You may need to choose the correct approach based on your Query Access Patterns required for your application.
Approach 1:
Advantage & Suggestion:-
liked_byasNSorSSrather than List. So that it will not have duplicates.liked_byare present in the same item. This will help during retrieval and showing the results on GUI if requiredDisadvantage:-
Approach 2:
You can define
photo_idas partition key anduser_idas sort key.Advantage:-
Disadvantage:-
likesshould be done at client side by iterating the values. In approach 1, you can use somearray.lengthto get thelikescountlikescount or any similar sceanrio