[Mr. Zhao Qiang] redis case analysis: using setbit to count active users

Time:2021-10-21

1、 Demand background

First, let’s take a look at the requirements: the website counts the number of user logins, as follows:

  • The website has 100 million users, including those who log in frequently and those who log in infrequently
  • How to record user login information
  • How to query active users: for example, log in three times a week

2、 Using traditional relational databases

We use a traditional relational database (such as Oracle) to store such data, as shown in the figure below.

700 million pieces of data are generated every week, which is very difficult to maintain for any relational database and will have a great impact on performance.

3、 Use the setbit operation of redis

Redis supports binary bit based setting of value of string type. By matching a user’s ID to a bit on value and setting the bit corresponding to active users, the information of all active users can be recorded with one value. As shown in the figure below, 9 bits of bitmap in the figure below are set to 1, indicating that the corresponding users in these 9 bits are today’s active users. The 15th bit represents the user with uid of 15, and the first bit represents the user with uid of 0. (if your uid does not start from 1, such as 100000, in fact, you can also use uid to subtract the initial value to represent the number of bits, such as 1000000 users corresponding to the first bit of bitmap)

The specific code is similar to the following:

redis.setbit(key, user_id, 1)

The complexity of such a record is O (1), which is very fast in redis.

The following code is a demo demonstration executed in redis cli. For example, when a user with user ID 100 logs in to the website on Monday, we set the value of 100 bits corresponding to Monday to 1 through setbit operation,
127.0.0.1:6379> setbit monday 100 1
(integer) 0
We save the daily active user status records separately by changing a different key every day. And statistics such as n-day active users and connected n-day active users can be calculated through some and or operations.

As shown in the figure below, the first line represents active users on Monday, the second line represents active users on Tuesday, and so on. In this way, we can get the list of active users in n days by merging the records of active users in n days.

 

 

Recommended Today

Go reflection class instance

Internal mechanism of variables Type information: static meta information, predefined Value information: it is dynamically changed during program operation Use of reflection Get type information: reflect.typeof, which is static Get value information: reflect.valueof, which is dynamic Get interface value information by reflection package main import ( “fmt” “reflect” ) //Get interface value information by reflection […]