You joke, but the likes will need to be stored somewhere and it's an O(p*t) problem, where p is the number of players and t is the number of unique things each player can like. Then if you actually want to display the number of likes, you need to count the number likes for each thing, which is an expensive DB operation that you'll probably have to precalculate and cache somewhere (which can then go stale / become desynchronized).
Only if you do things naively. You could instead store the likes as key-values where the keys are item ids and the values are an array of player ids who liked them. Then the storage is O(l), where l is the number of likes given. This will also allow DB operations to be performed quickly.
Well it depends on how you need to use the likes. Every data structure has pros and cons. If what you need is to get this, you can do the same but flipped (player ids as keys, item ids as values). The exact solution depends on your application, but my point is that it's really not that hard.
Its not that hard, but only if you know all requirements beforehands, and they don't change.
What usually happen is this:
Client says: "We need to show the total amount of likes under each item", and you say its easy, and implement key-value pairs with item IDs as keys and actor IDs as values.
Then one month later client says: "Now we also need to show the list of items you liked in your profile", and you say sure, no probs, and add the flipped pairs.
Then two months later client says: "Oh, and can we please show under each item which of your friends liked it too?", and then you say oof.
or you just denormalize it, then it's easy peasy. Sure, the write complexity goes up a bit, but likes can definitely be "available eventually" instead of "available now" as long as you show the like correctly on the client side.
Obviously this shit gets more complicated at truly huge scales, but so does everything
You then would need to store a list of every item a user has liked so they can unlike something which you then could remove a like from it. Which I've seen these operations fall out of sync multiple times due to edge cases like network lag.
Most of the answers here are wrong. Here's how YouTube and TikTok do it. They use non-relational databases, i.e. no joins. All they data they need for every query has to be stored in a single table for performance reasons.
Every table has two levels. First level contains item ID and like counter. Every time someone likes the item, the counter gets incremented by 1 and a row is added under that level with a username, time stamp and whatever else. This row is tombstoned, i.e. it gets inserted into the database with a predetermined deletion date. When the row gets deleted, the counter doesn't get decremented. Have you ever gone on old youtube or tiktok videos that you know you've liked, but your like keeps disappearing? That's what's happening.
For performance reasons, they also keep a table with the list of videos that every user has liked. The rows there are also tombstoned.
Twitter doesn't tombstone anything and keeps the information for likes from both sides of the relation (you can get the list of people who liked a tweet and list of tweets that a person has liked) on a custom database, which is one of the reasons why they needed ten thousand or whatever employees.
Edit: Should probably add that tombstoning and deletion in databases aren't just for managing data storage, they also keep the indexes fast.
5.5k
u/Drastwo Nov 26 '22
Sir, this like button will cost our team 14 months of backlog