1. Introduction

When I wrote a post about Redis’s five data structures and their common commands, I got a comment from someone who wanted to know how to use each of these five data structures, but never got around to writing it.

As it happens, during a job interview in March, an interviewer first asked me what data structures Redis had, and after I finished, the interviewer asked me the following question:

How to use Redis to achieve wechat steps leaderboard?

I believe many partners know that you can use the ordered set of Redis ZSET to achieve, this blog is based on this interview question, to explain the use of ZSET, as well as the general implementation of the wechat steps list.

2. Usage scenarios of ZSET

The classic use scenario of ZSET is to achieve leaderboards. To take a few common examples, such as baidu’s hot list, Weibo’s hot search list, and wechat’s number of steps we use every day:

The implementation ideas of the three scenarios are basically the same. Next, we take the wechat step ranking as an example to understand how to use Redis’ ZSET to achieve the wechat step ranking.

3. The general implementation idea of wechat step ranking

** Note: ** This article focuses on the use of Redis ZSET, so it only analyzes the general implementation of wechat steps leaderboard, the actual implementation is certainly more complex than the analysis of the article.

First of all, let’s analyze the demand for the wechat walk ranking:

  1. Leaderboards are based on date, and leaderboards for historical dates are viewable
  2. The leaderboard may not show the number of steps taken by all of your friends (for example, my wechat has 349 friends), but the leaderboard has never shown this many, assuming that only the top 200 steps are displayed at most
  3. The steps are updated asynchronously, so every once in a while the leaderboard changes when the steps are synchronized
  4. In the leaderboard, friends’ profile pictures and wechat nicknames can be understood as constant (with a low probability of change, like titles and urls in the top search list), but the number of steps and likes can be variable

Based on the requirements of the above analysis, the general implementation ideas are as follows:

  1. Use Redis’ ZSET data structure

  2. Set key, based on WeChat ID and date, such as my WeChat zwwhnly, today’s date is 2020-06-01, so the key can be designed to: StepNumberRanking: zwwhnly: 20200601

  3. When setting value, take the nickname of the friend as a member and the number of steps of the friend as the score, as shown below:

  4. Use the HASH data structure of Redis, where key is the key of step 2 + member of step 3, and value stores the profile picture, nickname, number of steps, and number of likes of friends, as shown below:

  5. The following two steps are used to obtain the ranking of wechat steps:

    1) the first query list of friends of micro walk from their nicknames, namely query StepNumberRanking: zwwhnly: the value of 20200601

    2) according to the access to the friends of the nickname, information query and friends steps, namely query StepNumberRanking: zwwhnly: 20200601: the value of yst

4. Run the Redis command

The above analysis of the general implementation of the idea, next we explain the use of Redis command.

4.1 ZADD

Run the following command to initialize the leaderboard of wechat steps. Take the 9 friends in the picture above as an example, initialize the leaderboard in two times:

ZADD StepNumberRanking:zwwhnly:20200602 25452 yst 23683 zq 23599 ljx 20391 yyq 19628 XxZz

ZADD StepNumberRanking:zwwhnly:20200602 18261 lxx 16636 zcc 16555 clc 16098 fl
Copy the code

The result is as follows:

As you can see, the default order is in positive score order, that is, the number of steps from the lowest to the most.

4.2 HMSET

To display the leaderboard of steps, you need to display the nickname, avatar, number of steps, and number of likes, so you can use Redis HASH data structure to store the leaderboard, then use HMSET command:

The result is as follows:

4.3 ZINCRBY

Every once in a while, your friends’ steps are updated, so you can use the ZINCRBY command to update your friends’ steps. Suppose we only update the steps of the top two friends and increase their steps by 10, then we can run the following command:

ZINCRBY StepNumberRanking:zwwhnly:20200602 10 yst

ZINCRBY StepNumberRanking:zwwhnly:20200602 10 zq
Copy the code

The result is as follows:

After updating the number of steps in the leaderboard, don’t forget to run the HMSET command to update your friend’s number of steps:

4.4 HINCRBY

We can use the HINCRBY command to increase likeNum by 1 when we like our friends in the steps leaderboard:

HINCRBY StepNumberRanking:zwwhnly:20200602:zq likeNum 1
Copy the code

4.5 ZRANGE

After all the data is in place, all that remains is the query. We can use the ZRANGE command to get information about friends in the leaderboard:

ZRANGE StepNumberRanking:zwwhnly:20200602 0 -1
Copy the code

As you can see, the list of friends is sorted by number of steps, whereas the leaderboard should be sorted by number of steps, using the following ZREVRANGE command.

4.6 ZREVRANGE

The ZREVRANGE command is similar to the ZRANGE command, but in reverse order of score, which matches the leaderboard scenario.

Such as executing commands:

ZREVRANGE StepNumberRanking:zwwhnly:20200602 0 -1 WITHSCORES
Copy the code

It can be seen that the queried friend information is sorted according to the number of steps from large to small, which is exactly the order to be displayed in the leaderboard.

However, leaderboards usually don’t show all the data. Here we have a small amount of data. If we only get friends with the top5 steps, we can run the following command:

ZREVRANGE StepNumberRanking:zwwhnly:20200602 0 4 WITHSCORES
Copy the code

If you want top200, change the top 4 to 199.

4.7 HGETALL

After obtaining the information of friends in the leaderboard, the last step is to obtain the information such as the number of steps, likes, avatar and nickname of these friends, which is the information stored by using HASH data structure before. At this point, we can use HGETALL command, as shown below:

HGETALL StepNumberRanking:zwwhnly:20200602:yst
Copy the code

If you’re not familiar with these commands, take a look at my previous blog: Redis Series ii: The five Data structures of Redis and their common commands.

5. To summarize

Redis ZSET data structure is very suitable for use in leaderboard scenarios, such as Baidu hot search, Weibo hot search, game leaderboard, wechat steps leaderboard, the interviewer will not ask you what commands ZSET has, the details of each command and so on, but ask you how to use Redis to achieve wechat steps leaderboard, You can see how well you understand Redis data structures.

Therefore, it is important to learn the basis of the five data structures of Redis, but it is more important to know how to use these data structures and what scenarios are most suitable for each data structure. After all, it is necessary to learn for practical use.

Note: If you think this blog has any mistakes or better suggestions, please leave a comment, I will follow up and correct the blog content in time!

This article continues to be updated, please pay attention to the wechat public number “Shencheng Strangers” for the first time to read!