# Redis learning notes – (2) – bitmap usage – Statistics on the number of continuous active users of more than 200 million users in one week

Time：2022-5-25

Previous: redis learning notes – (1) – bitmap usage 1 – count the login days of all users in one year

## 2. Bitmap usage 2: the number of continuously active users with hundreds of millions of users in one week

### 2.1 ideas

Demand 2: hundreds of millions of users, counting the continuously active users in a week

100000000 / 8 / 1024 / 1024 = 11.9m. A bitmap occupies less than 12m

Fortunately, there are only seven days a week. We can use the bitmap of seven keys to store the status,

Add the bitmap of res in the last result: 12 * 8 = 100m memory!

Follow these steps:

1. The user number is the premise, and the number of each user ranges from 1 to n (n = the maximum value of hundreds of millions);
2. Declare 7 bitmaps from Monday to Sunday: mon Tue wed Thur fri sat sun;
3. The offset of each user number is 1 if you log in on Monday, and 0 if you don’t log in;
4. All 7 bitmaps have set value records;
5. usebitopBit for 7 bitmapsANDOperation, the bit on the day when you log in for 7 days is 1
6. The final results are analyzed`bitcount`Operation means hundreds of millions of users within a week`continuity`Number of people active!
7. If you want to know who are the continuously active users, go through`Getbit key of each day (Mon / Tue /...) id`Yes!

Example: let’s simulate 5 users:

User ID mon tue wed thur fri sat sun
001 1 1 1 1 1 1 1
002 0 0 0 0 0 1 1
003 1 1 1 1 1 1 1
004 1 1 1 1 1 1 1
005 0 1 0 1 0 1 1

### 2.2 first 4 steps: initialize 7 bitmaps

Monday’s key:mon, login status records of all users on Monday:

``````127.0.0.1:6379> setbit mon 1 1
(integer) 0
127.0.0.1:6379> setbit mon 2 0
(integer) 0
127.0.0.1:6379> setbit mon 3 1
(integer) 0
127.0.0.1:6379> setbit mon 4 1
(integer) 0
127.0.0.1:6379> setbit mon 5 0
(integer) 0
``````

Tuesday’s key:tue, login status records of all users:

``````127.0.0.1:6379> setbit tue 1 1
(integer) 0
127.0.0.1:6379> setbit tue 2 0
(integer) 0
127.0.0.1:6379> setbit tue 3 1
(integer) 0
127.0.0.1:6379> setbit tue 4 1
(integer) 0
127.0.0.1:6379> setbit tue 5 1
(integer) 0
``````

Wednesday’s key:wed, login status records of all users:

``````127.0.0.1:6379> setbit wed 1 1
(integer) 0
127.0.0.1:6379> setbit wed 2 0
(integer) 0
127.0.0.1:6379> setbit wed 3 1
(integer) 0
127.0.0.1:6379> setbit wed 4 1
(integer) 0
127.0.0.1:6379> setbit wed 5 0
(integer) 0
``````

Thursday’s key:thur, login status records of all users:

``````127.0.0.1:6379> setbit thur 1 1
(integer) 0
127.0.0.1:6379> setbit thur 2 0
(integer) 0
127.0.0.1:6379> setbit thur 3 1
(integer) 0
127.0.0.1:6379> setbit thur 4 1
(integer) 0
127.0.0.1:6379> setbit thur 5 1
(integer) 0
``````

Key on Friday:fri, login status records of all users:

``````127.0.0.1:6379> setbit fri 1 1
(integer) 0
127.0.0.1:6379> setbit fri 2 0
(integer) 0
127.0.0.1:6379> setbit fri 3 1
(integer) 0
127.0.0.1:6379> setbit fri 4 1
(integer) 0
127.0.0.1:6379> setbit fri 5 0
(integer) 0
``````

Saturday’s key:sat, login status records of all users:

``````127.0.0.1:6379> setbit sat 1 1
(integer) 0
127.0.0.1:6379> setbit sat 2 1
(integer) 0
127.0.0.1:6379> setbit sat 3 1
(integer) 0
127.0.0.1:6379> setbit sat 4 1
(integer) 0
127.0.0.1:6379> setbit sat 5 1
(integer) 0
``````

Sunday key:sun, login status records of all users:

``````127.0.0.1:6379> setbit sun 1 1
(integer) 0
127.0.0.1:6379> setbit sun 2 1
(integer) 0
127.0.0.1:6379> setbit sun 3 1
(integer) 0
127.0.0.1:6379> setbit sun 4 1
(integer) 0
127.0.0.1:6379> setbit sun 5 1
(integer) 0
``````

I’ll go. The data is finally entered. It’s hard~~

### 2.3 bitop is used to bit 7 bitmapsANDoperation

``````127.0.0.1:6379> bitop and res mon tue wed thur fri sat sun
(integer) 1
``````

### 2.4 bitcount bitmap to the result. It’s time to witness the miracle

``````127.0.0.1:6379> bitcount res
(integer) 3
``````

### 2.5 who are the continuously active users

`getbit res 1`Indicates the result with user number 1: 1 indicates that the user with user id = 1 is active for 7 days!

The results of the five users are as follows:

``````127.0.0.1:6379> getbit res 1
(integer) 1
127.0.0.1:6379> getbit res 2
(integer) 0
127.0.0.1:6379> getbit res 3
(integer) 1
127.0.0.1:6379> getbit res 4
(integer) 1
127.0.0.1:6379> getbit res 5
(integer) 0
``````

### 2.6 expansion:

If the demand is: find the number of active users in a week: just add the above command to the list`bitop and`Replace with:`bitop or`Just~

continuityAnd is required,ActiveOne day, just one day

over!

### 2.7 summary

Bitmap operation command:

• setbit key offset value
• getbit key offset
• bitcount key [start end]
• bitop operation destkey [key …]

Operation: four operations are supported: and / or / not / XOR. Except not, other operations can accept one or more keys as input

Destkey: all subsequent keys ([key…]) of`And or not`The result of the operation is stored in a target key and given a name!

key… Can be done with n keys`And or not`Operation, pass in the key list of these bitmaps

## Note1: ubuntn system parameter view command

Check whether the system is 32-bit or 64 bit getconf LONG_BIT 32: 32 bits 64: 64 bit View the kernel information of the current system uname -a View the release version of the system cat /etc/issue View CPU model cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c View CPU profile Small […]