1. The background

In many apps, we can often see a lot of nine grids or golden egg raffle activities, such as the gold nuggets check-in raffle.

The purpose of this demand is to form an internal integral closed-loop. Giving a free lucky draw opportunity every day can not only promote the daily activities of products, but also make users develop habits (good users are nurtured).

Users open the app or web terminal, sign in and get a free lucky draw.

2. Business analysis of lottery activities

Here is a simple example for business analysis, rather than a specific business analysis of the gold lottery:

  1. Dynamic display of prizes.
  2. Dynamic display of the number of points needed to draw.
  3. Show current remaining integrals.
  4. The winning records are displayed in categories, including big prizes and small prizes, and are displayed in the form of a running horse.
  5. Each prize can be configured to win the weight, the number of prizes.

As shown in the following picture (from deepin drawing software)

3. Program planning

  • Main scheme design
    1. The lottery activity system is related to the integral business system, and the single lottery system is relatively independent.
    2. Background can be configured to win prizes and weights, as well as the number of each prize.
    3. Ensure that the number of lucky draw prizes are limited and cannot be sent more
  • Example of the front-end lottery architecture

When the client visits the active lottery system, query the configuration of the prize list, users can use points, active time and other related configuration, lottery page running lamp.

The access to the prize list and the run-around (the raffling log data) can be cached here, because there are no timeliness issues involved, the graph only needs to cache the data between the active system and the database query.

  • The solution

How to ensure that the number of lucky draw prizes is limited and cannot be sent more? In fact, this kind of problem is very common nowadays, and you can find many ideas on the Internet. Here I use the middleware Redis to process the tag prize. First, it is to facilitate the horizontal expansion of the server. My practice is every time the background on the prize when the prize cache into Redis, preloading processing, using Redis Inc and the lock database to ensure the inventory problem.

When the backend server configuration by modifying the prize, prize weight, for example, to cover the cache data in the lottery, the client is the most main dealing with redis, when a user, such as the prize type is you need to determine inventory, then we will through the data comparison, finally change the database, users to winning when database successfully. When the value after Redis Inc is greater than or equal to the inventory of the prize (of course, the prize can also be drawn again when the prize is not mixed into the pool). I believe that a good product manager will take into account that programmers can write bugs, so when the water pushes the boat, a consolation prize will be designed as a lottery accident. For example, when a user draws the “grand prize” in the lottery, a default prize will be returned due to the abnormal logic of awarding the grand prize

  • Award logic scheme

For the lottery, this is very simple, according to the type of lottery, go different logic, the typical if… else if.. , where you can optimize the code using the policy pattern.

4. Database design

  • Active configuration table
field The data type describe
id bigint(19) A primary key
image varchar(512) Activity a larger version
name varchar(128) The name of the event
start_time datetime Activity Start time
end_time datetime End time
consume_type varchar(20) Cost type (Integral, Jewel)
consume_num bigint(19) Quantity consumed (unit)
status tinyint(2) Active status (1 on, 2 off)
create_time datetime Creation time
create_user varchar(20) Create a user
update_time datetime Modify the time
update_user varchar(20) Modify the user
is_delete tinyint(2) Whether or not to delete
  • The prize list
field The data type describe
id bigint(19) A primary key
icon varchar(512) The prize of the icon
name varchar(64) Name of the prize
type tinyint(2) Type of Prize (1 point, 2 members, 3 physical prizes)
stock_count int(11) Inventory (unit)
enable_stock tinyint(2) Enable inventory check (0 off, 1 on)
weight Double (2, 4) The prize weight
activity_id bigInt(19) Id of the binding activity
create_time datetime Creation time
create_user varchar(20) Create a user
update_time datetime Modify the time
update_user varchar(20) Modify the user
is_delete tinyint(2) Whether or not to delete
  • Lucky draw log table (which user drew what prize and when)
field The data type describe
id bigint(19) A primary key
user_id bigint(19) The user id
prize_id bigint(19) The prize id
create_time datetime Creation time
.

5. Background sample

6. The tail

In most cases, the sweepstakes to balance the internal data flow problem, product integration, for example, when a user points too much, will cause the integration of “inflation”, also is the place where it didn’t take out, such as the king of glory, diamond lottery is to balance the diamond, when a large number of users has too much, and don’t take out place, this leads to a particularly cheap diamonds, Like running every day. In Denver, design a daily check lottery, daily sign-in role is to promote, live sign-in and draw, and promote the day live plan, can increase the APP open frequency and web hits (traffic), and easy to form a group of loyal users, the days that followed, exchange shop will make the nuggets, diamond is the only currency inside the product, Given such a large number of diamonds, MY guess is that the users of the diamond should be quickly redeemed for the prize, thus slowly weakening the lottery, as a diamond consumption mouth, behind the diamond may not have so many.

Don’t think about it, just do the code, leave the rest to the data analyser, we just need to make the line normally.