Like is an effect we often see now , Like a circle of friends , Weibo has a little like effect , The following article mainly shares with you based on
redis Relevant information about the design idea of the realized like function , The introduction is very detailed , It has certain reference and learning value for everyone to realize the like function value , Friends in need, let's have a look .


Like is actually a very interesting function . There are two basic design ideas , One kind of nature is to use mysql etc.
Direct floor storage of database , The other is to use the business features of likes to throw redis( or memcache) in , Then offline brush back mysql etc .

Direct write Mysql

Direct write Mysql Is the simplest way .
Just make two tables ,
Record the number of times the article is liked , How many people have praised this kind of data can be directly found in the table ;
Record which articles the user likes , When opening the article list , The data that shows whether there are likes or not is here ;
1, High database read / write pressure
Popular articles will be liked by many users , Even a lot of likes in a short time , Direct database operation is not an ideal approach in the long run .

redis Store and then batch brush back to the database

redis The main feature is fast , After all, the main data is in memory ; And why did I choose redis instead of memcache The main reason is that redis Support more data types ,
for example hash, set, zset etc . The following specific types will be used .

* advantage
1, High performance
2, Relieve database read / write pressure
In fact, I am more concerned with relieving the pressure of writing , Really read pressure , adopt mysql Master and slave even join redis Cache hot data , Writing pressure is really not good for the previous scheme .
* shortcoming
1, Complex development
This is better than writing directly mysql Our plan is much more complicated , There are many things to consider ;
2, Data security cannot be guaranteed
redis Data will be lost when you hang up , Not synchronized in time redis Data in , May be in redis Memory replacement is eliminated ; But just like us ,
A little data loss is not a problem ;
Specific design

* Mysql Design
This block and write write write mysql It's the same , After all, it needs to be stored on the ground .
So the same needs post_like, user_like_post These two tables store the number of articles that are liked ( Equal statistics ), Users like those articles ( Cancel like ).
These two tables are passed respectively post_id, user_id Associate .
* redis Design part :
stay redis One of them set Store all likes
For each post with post_id As key, Make one set Store all post Like users ;
Assign each user to each post Put your likes into one hash Go inside , hash The field of
Just follow up the demand and handle it at will .
Why hash
Only for this purpose hash Because it can be used hash To store a like object , A row of records corresponding to the database .
Of course, some students will say it key, value it's fine too , Serialize all data (json_encode etc. )
After all value Go inside . Repeated serialization is also a big overhead. No , hash Can be very
Easily modify a field , And the operation of serialization and deserialization .
For each post Maintain a counter , Used to record the current redis Likes in , Here we only use counter Records have not been synchronized to mysql Likes in ( Can be negative ), every time
Brush back mysql Medium time will counter Add the data in and the existing likes in the database .
User likes / Cancel like

obtain user_id, post_id, Query whether the user has liked , Once clicked, you are not allowed to click like again ,
Or it is designed as the front end to allow user points , Only background calculation is not repeated ;
It should be noted that the records of user likes may be in the database , May also be in cache , So when querying
Both cache and database should be queried , The cache does not query the database again .
Like the user / Cancellation of likes is recorded in redis in , Specifically :
1, write in post_set
take post_id write in post_set
2, write in post_user_like_set_{$post_id}
take user_id write in post_user_like_set_{$post_id}
3, write in post_user_like_{$post_id}{$user_id}
User likes data , For example, like status , post_id, user_id, ctime( Operation time ), mtime( Modification time ) write in post_user_like
{$post_id}{$user_id} in 4, to update post{$post_id}counter
to update post{$post_id}_counter, The update here is a little later and more complicated , As before, you need to get whether the current user is satisfied with this post Like it
If you click , And this time is to cancel like , counter Minus one , If not , This time is like , counter add one-tenth .
If you cancel the like , This time is like , counter add one-tenth .

Synchronous brush back to database

Loop from post_set in pop Come out one post_id To to empty
according to {$post_id} , Every time from post_user_like_set_{$post_id} in pop Come out one user_id Until empty according to post_id,
user_id, Directly obtain the corresponding hash Table contents (post_user_like_{$post_id}_{$user_id}
take hash Data write in table user_like_post In the table

take post_{$post_id}counter Data and post_like Data addition in , Write results to post_like In the table Page display
1, Query user likes
As already said , Need to query at the same time redis and mysql
2, query post Like statistics
Query is also required redis Medium post{$post_id}_counter and mysql of post_like surface , And add the two The result is the correct result


Solved mysql Reading and writing problems
However, the sub table design is not considered for scenarios with a large number of users , May consider targeting user_id perhaps post_id Perform sub table
okay , That's all for this article , I hope the content of this article can bring some help to your study or work , If you have any questions, you can leave a message communication , Thank you for your support .

©2019-2020 Toolsou All rights reserved,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?