Check in function, MySQL or redis?


Now in the development of website and app, sign in is a very common function, such as microblog sign in, send points, sign in leaderboard.




Such as mobile app, check-in and delivery of traffic.



User check-in is an effective means to improve user stickiness, good use can get twice the result with half the effort!

Let’s take a look at the common implementation methods from the technical aspect

1、 Option 1

Save directly to MySQL

The user list is as follows:



last_ checkin_ Last check in time


checkin_ Count continuous check in times

Record the check-in information of each user

Check in process

1. The user signs in for the first time
last_checkin_time = time()


2. The user does not sign in for the first time and has signed in on the same day

Don’t do anything, return to check in.

3. The user does not sign in for the first time and has not signed in on the same day

a. There was also a sign in yesterday

last_checkin_time = time()
checkin_count= checkin_count+1

b. I didn’t sign in yesterday

last_checkin_time = time()

The code using Yii is as follows:

$today_0 = strtotime(date('y-m-d'));
//It was 0 o'clock yesterday
$yesterday_0 = $today_0-24*60*60;
$last_checkin_time = $model->last_checkin_time;if(empty($last_checkin_time)){
 //first checkin
 $model->last_checkin_time = time();
 $model->checkin_count = 1;  
 if($today_0 < $last_checkin_time){
  //Checked in on the same day
  return json_ Encode (['code '= > 0,' MSG '= >'check in succeeded'];
 }// checked in yesterday if ($last)_ checkin_ time < $today_ 0 && $last_ checkin_ time > $yesterday_ 0){
  $model->last_checkin_time = time();
  $model->checkin_count = $model->checkin_count + 1; 
  //I didn't sign in yesterday. Count again
  $model->last_checkin_time = time();
  $model->checkin_count = 1;
 }}$rs = $model->save();


2、 Option 2

Redis implementation scheme is implemented by using bitmap. Bitmap is a function supported by redis version 2.2, which is generally used to identify the status,

In addition, it’s very convenient to use bitmap to check in how many people there are on the same day. Use bitcount


Check in process

Set two bitmaps,

One takes the daily date as the key and each uid as the offset

One takes the user uid as the key and the index of the day in the year as the offset,

In this way, only 365 * 1bit is needed to record a user’s check-in situation in one year

Here is the sign in code

//One key a day
  $key = 'checkin_' . date('ymd');
  if($redis->getbit($key, $uid)){
   //Signed in return JSON_ Encode (['code '= > 0,' MSG '= >'check in succeeded'];
   //Check in $redis - > setbit ($key, $uid, 1);
   $redis->setbit('checkin_'.$uid , date('z'), 1);

 The following is the calculation of user continuous check-in

public static function getUserCheckinCount($uid){
  $key =  'checkin_'.$uid;
  $index = date('z');
  $n = 0;
  for($i = $index; $i>=0;$i--){
   $bit = Yii::$app->redis->getbit($key, $i);   if($bit == 0) break;
   $n++;  }  return $n;

The following is how to calculate the number of check-in users in one day

$key = 'checkin_' . date('ymd');
$redis = Yii::$app->redis;$count = $redis->BITCOUNT($key);

  What else is needed? You can try to do it yourself

3、 Comparison of advantages and disadvantages

1. Direct MySQL

The idea is simple and easy to realize;

Disadvantages: it takes up a lot of space and updates a lot of tables, which affects the performance. When the amount of data is large, cache is needed to assist;

2.Redis bitmap

The advantages are:

Small footprint, pure memory operation, high speed;

The disadvantages are:

The recorded information is limited, and there is only one identification bit;
The offset can’t be greater than 2 ^ 32512m; it can identify 500 million bits, which is enough for most applications;

When the offset is large, the redis server may be blocked, so segmentation should be considered.

Well, that’s all. Each has its own advantages and disadvantages. Which way do you like?

Welcome to discuss!


Author: Web programming together


Recommended Today

Jupyterlab environment of tensorflow

Tensorflow prepares jupyterlab interactive notebook environment, which is convenient for us to write code and take notes at the same time. Basic environment The following is the basic environment of this article, do not detail the installation process. Ubuntu Ubuntu 18.04.5 LTS (Bionic Beaver) ubuntu-18.04.5-desktop-amd64.iso CUDA CUDA 11.2.2 cuDNN 8.1.1 libcudnn8_8.1.1.33-1+cuda11.2_amd64.deb libcudnn8-dev_8.1.1.33-1+cuda11.2_amd64.deb libcudnn8-samples_8.1.1.33-1+cuda11.2_amd64.deb Anaconda […]