The like function is a basic function in current app development. Today we will talk about the database design issues for scenarios such as likes, comments, and favorites. 1. Let's first look at the requirements of the scenario:
Let’s first look at the examples of Toutiao and Weibo. Likes on headlines Likes on Weibo Both of these have top-level traffic, and the backend must have a complex architecture. Today we will only talk about popular solutions. 2.1 MySQL solution MySQL solution: With the popularity of NoSQL and the continued popularity of big data, MySQL is still irreplaceable. For most small and medium-sized projects with data volumes below 10 million, MySQL sharding + cache is fully competent, and its stability is unmatched by other solutions:
Commonly used queries:
The number of likes can be updated to the post and user tables through scheduled asynchronous statistics. When the amount of data is not large, this design can basically meet the needs. shortcoming:
2.2 redis solution When the amount of data reaches hundreds of millions, caching is a necessary stage. Since the action of clicking "like" is very casual, many people want to click "like" when they see the thumbs-up, so the amount of data grows very fast. When the data scale increases, there is a lot of pressure on MySQL reading and writing. At this time, you need to consider memcache or redis for storage or caching. Why do people generally choose redis? As a popular NoSQL, redis has rich data types and can adapt to the needs of multiple scenarios. There are two uses for redis, one is storage, the other is pure cache, which needs to be used with MySQL. Pure cache means writing data from MySQL to redis first, and the user reads the cache first, and then pulls it from MySQL after a miss, while the cache is synchronized. cache In most scenarios, the two are used at the same time and do not conflict. Here's how to use redis as a storage solution: Scenario a: Display the number of likes In the like area, only the number of likes is displayed to distinguish whether the user has liked it. Generally, users do not care about this list. In this scenario, only a number is needed. When the number is relatively large, it is generally displayed as "7k", "10W" or the like. Use article id as key
Scenario b: Remove duplicate likes to avoid repeated likes To achieve this requirement, there must be a list of uids that liked the article, with uid as the key. Scenario C: Generally, in the user center, you can see the user's own like list This requirement can be achieved using the data from scenario b. User Center Like List Scenario d: Like list of an article, similar to scenario b, with article id as key
If you have liked a place, it will be displayed in red, if not, it will be displayed in black and white. There is no place to see the likes list on Toutiao, but when you click on Weibo, you can see the likes list on the details page, but only the most recent few dozen will be displayed, without pagination. As shown below, I chose a hot spot, "Pig" with many fans. Post likes list Some people may think that no one cares about the likes list, and storing it will waste a lot of resources, so it is better not to store it! However, this data is necessary. Two points: a. Deduplication. The number of likes can be inaccurate, but deduplication must be accurate. b. For another social product, every bit of user behavior needs to be recorded, which is meaningful for subsequent user behavior analysis and data mining. The number of user likes stored in string above can also be stored in hash. The article ids are divided into blocks, and every 100 are stored in a hash table. Each article id is a key of the hash, and the value stores the user id of the user who liked the article. If there are many users who like the article, in order to avoid performance problems caused by too many ids, they can be listed separately and saved in a sorted set structure. After all, the hot spots are only a few. hash Comparison of the advantages and disadvantages of the solutions Hash: Uses fewer global keys, saving memory space; but it also brings problems How to route to the corresponding hash based on the article id? Is the user ID in the hash or set? There is uncertainty Although using hash saves space, it increases complexity. The choice depends on personal needs. Do you have any other methods besides this? 3. Data consistency When redis is used as storage, data persistence must be ensured, and RDB and AOF must be enabled. This will result in the business only being able to use half of the machine's memory, so capacity monitoring must be done and capacity expansion must be done in a timely manner. In addition, as long as there is data copy, there will be consistency issues, which is another very important topic. Let's talk about it in detail later! In conclusion: It is not easy to write the problem clearly. Please pay more attention and leave a message. Thank you! An article I wrote a few days ago received enthusiastic responses from many colleagues. I feel honored to be able to communicate with so many colleagues! There is no standard solution to engineering problems. A thousand people have a thousand solutions. Only you know which one is best for you! I look forward to your better ideas and methods. |
>>: The premature birth and premature death of China Mobile's "5G message"
Today we will talk about some ways of playing tha...
1. What is Juliang Qianchuan ? Bytedance Qianchua...
1.Q: The business entity or special qualification...
Author: Chen En (Purple Mountain Observatory, Chi...
When it comes to mouth-watering childhood memorie...
Nowadays, we are immersed in a world composed of ...
With the rapid development of mobile Internet , a...
The college entrance examination every year is an...
Which online variety show is the best in China in...
This type of Android system customized by mobile ...
Apple spared no effort to solve the production ca...
Usually, when we fall in love with someone, it se...
"12 million units shipped in 3 years". ...
During this Spring Festival holiday, many young p...
As the saying goes, a white complexion hides a hu...