1、 Demand background
- 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)
redis.setbit(key, user_id, 1)
The complexity of such a record is O (1), which is very fast in redis.
127.0.0.1:6379> setbit monday 100 1 (integer) 0
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.