Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Youtube remember Likes?

So I'm wondering how sites like YouTube remember when a user has "Liked" a video and prevents them from liking it again. Similarly, how a site like Reddit remembers upvotes and downvotes and prevents a user from upvoting a piece of content they already upvoted.

Is it as simple as a database table where the ID of the content is stored along with the ID of the user and their response? I feel like that would become a massively huge table. Is there something trickier going on?

like image 417
John Avatar asked Dec 31 '25 16:12

John


2 Answers

The sites require you to login before clicking on "like" and "upvote". The content document in the DB will have a field which will store the number of likes received. Before the like button is rendered, the sites will check against the logged in user's records in the DB, to check if he has already liked it- accordingly a "like" or "unlike" option is displayed.

If you check here in Stackoverflow, if you click "upvote" on your own question or answer, a message is displayed, that you can't upvote our own post. What happens is when you click "upvote", ajax request will be sent to server with the user ID and other information like question id etc. and the server code will check if you are allowed to upvote i.e your user ID should not be the same as the post creator's ID.

like image 107
Ninja Avatar answered Jan 02 '26 05:01

Ninja


I have a like/dislike system on my page.

Database tables:

1.) One that contains your posts with a unique ID for each post and a user_id of who created it (along with other info like content, tags, etc).

2.) table called likes with AT LEAST the following fields, ID, post_id (corresponds to the post in the post table that was liked or disliked), user_id (corresponds to the user in the users table that did the liking/disliking), status (0 or 1, 0 being liked the post, 1 being disliked the post).

When a user likes a post, insert the row into the likes table with their user_id and the post_id, set the status as 0 (or just leave empty because 0 is the default). When a user dislikes a post, do the same but set the status as 1.

That way, on a post page you can get the count of all the users that liked or disliked a post. On a users's profile page, you can get all of the posts a user either likes or dislikes. You can also rank posts by which has the most likes or dislikes. Or even rank specific users by who has posted content with the most likes or dislikes.

Do not allow a users to like/dislike a post if they already have a record in the database. (Basically just check the count of records in the likes table where the post_id is equal to the current post and user_id is equal to the logged in user)

Cross reference the post table to get the post's author's user_id. If the post author user_id is the same as the logged in user, or the user is NOT currently logged in, do not allow them to vote.

The queries for doing all of those are simple (simply SELECT * or SELECT user_id) but that is the basic idea.

like image 22
bowlerae Avatar answered Jan 02 '26 06:01

bowlerae



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!