In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to solve the paging Bug caused by the improper use of SortSet in Redis". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to solve the paging Bug caused by the improper use of SortSet in Redis.
First of all, our online e-commerce system is carrying out a big promotion activity. There were so many buyers that there were a lot of comments at one time. Developers use the SortSet data structure in Redis when storing (saving) comments. For each new comment, he saves it to Redis and MySQL through the zadd command, and fetches it directly from the Redis when querying. The code is abstracted as follows:
By the way, System.currentTimeMillis () is milliseconds, and System.currentTimeMillis () / 1000 is equivalent to accurate to seconds.
This activity is a great promotion, participation in the activity needs to be commented on. The activity is hotter than expected, resulting in more comments in the same second, resulting in duplicate data when zrangeByScoreWithScores encounters more than 10 comments in the same second.
The logic for taking out the list of comments is roughly abstracted as follows:
Did you see that? Each time he took the lastScore record, the last piece of data on the previous page of the comment. To get 10 new comment data. As more than 10 comments are recorded in the same second, the probability of obtaining duplicate data every time APP pulls down and refreshes is relatively high. So there are customer complaints, look at the data in the comments are all the same. No matter how you refresh it, it is 10 pieces of the same data, the same as the previous page.
This zrangeByScoreWithScores is actually the ZREVRANGEBYSCORE instruction in Redis. The complete instructions are as follows:
Among them, this limit can be paged. There is also a problem with this page for drop-down refresh, that is, when you drop down and refresh, you just add a few comments, so the refreshed data may also have duplicate data.
Later, the programmer changed the problem and added 1 every time he got the lastScore. After I saw this code, I went to him again, which certainly won't work. This means that every time you get the last lastScore on the previous page, add 1. It is equivalent to adding 1 second to the time, and some data will be missed in the middle.
So what should we do? If you want to use lastScore for paging, you must make sure it is unique. Otherwise, there is a probability that duplicate data will occur when the list is refreshed.
Ours is still a little different from current events news. The probability of repetition of writing time of news is not as high as ours. Especially when we are in the midst of great promotion activities.
Finally, let's talk about the solution to this problem. One is to ensure that the lastScore is unique, or that the time is further accurate. Another is the combination of lastScore and limit to achieve paging. Or simply use limit to achieve, but also be careful not to refresh the duplicate data.
In the end, we made reference to the above picture.
Take the topicId of each topic as the key of set, and the createDate and commentId of the comments associated with the topic as the score and member,commentId of set, respectively. The order is arranged according to the size of the createDate.
When you need to query comments on a page of a topic, the topicId of the topic can find out the commintId of all comments in time order on a page under a topic by instructing zrevrange topicId (page-1) × 10 (page-1) × 10+perPage. Page is the page number of the query page, and perPage is the number of entries displayed on each page.
When you find the commentId of all the comments, you can use these commentId as key to go to the Hash structure to query the corresponding content of the comment.
In this way, the two structures of SortSet and Hash are used to achieve the purpose of paging and sorting in Redis.
Thank you for your reading, the above is the content of "how to solve the paging Bug caused by the improper use of SortSet in Redis". After the study of this article, I believe you have a deeper understanding of how to solve this problem caused by the improper use of SortSet in Redis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.