GeminiDB new feature: exHASH that Redis advertising frequency control can’t put down

This article is shared from the Huawei Cloud Community " GeminiDB New Features: exHASH That Makes Redis Ad Frequency Control Addictable ", author: GeminiDB-Redis Blog.

The exHash type is a new data type that supports Field expiration. It expands on the original Hash type: in addition to supporting the general functions of the Hash type, the exHash type also supports setting the expiration time and version for the Field, which enhances the data structure. flexibility, thus simplifying business development work in many complex scenarios.

This article takes two common scenarios (frequency control scenario & shopping cart) as examples to implement complex services and simplify development difficulty by using the exHash class commands in the GeminiDB Redis interface.

Introduction to using exHash command

Application scenarios

Frequency control scenario

Frequency control refers to limiting the number of times a user can perform a certain operation within a certain period of time (such as a day, a week, a month). It can control the number of times a specific advertisement or information is displayed on a certain platform within a certain period of time to avoid over-exposure. and advertising fatigue, while optimizing advertising effects and user experience; for advertising, it can also improve advertising effectiveness and conversion rates. In addition, frequency control can also avoid malicious behaviors, such as brushing traffic, brushing comments, brushing likes, etc.

The three elements of frequency control include user ID, advertising ID, and number of triggers. The user ID is the key, the advertising ID is the field, and the number of triggers within the specified time is the value, which exactly constitute the three elements of frequency control. First configure the designated frequency control strategy for each advertisement. As shown in the figure below, frequency control can be achieved in the following ways:

  • The leftmost is implemented through the Hash type. The expiration time of User_1 is set to one day through the expire command. Each push is used to increase the number of pushes of the specified advertisement through hincrby. The number of pushes in a day before each push of the specified advertisement can be obtained through hget. It is judged that the user's data will automatically expire after one day without manual cleaning, so frequency control can be easily achieved. However, the disadvantage of this solution is that only one expiration time can be set for each user (that is, each key), and it is impossible to implement a flexible frequency control strategy within a specified time period, such as three times in 8 hours.
  • In order to configure flexible frequency control within a specified time period for each advertisement, as shown in the middle figure, it can be implemented by splicing the timestamp into the value using the Hash type. However, this solution undoubtedly increases the business side. Development workload.
  • As shown in the rightmost picture, the exHash type that supports setting expiration time for fields can perfectly solve the shortcomings of the Hash type in frequency control scenarios. Since Field supports expiration time settings, in this scenario, the platform can configure frequency requirements for each advertisement in different time periods. Assume that the frequency control policy configured for AD_2 at this time is 2 times within 8 hours, then as shown in the figure Before preparing to push AD_2 ads to User_1 next time, first use the exhget User_1 AD_2 command to obtain that the value is already 2. Then it can be judged that according to the platform frequency control strategy, AD_2 ads should not be pushed to User_1 at this time. When 8 hours have passed and the AD_2 field of User_1 has expired, exhget can no longer obtain the information of this field, and can continue to push AD_2 ads to User_1.

shopping cart scene

During the recent Double Eleven period, I believe that many students’ shopping carts were filled with all kinds of treasures that they wanted to empty. Here we take the shopping cart scenario as an example to introduce several different Redis type implementations of this scenario, and compare these implementations. Advantages and disadvantages of the scheme.

1. Implement shopping cart function based on String

As shown in the figure, the shopping cart function of each user can be easily implemented based on String. This solution requires splicing the user ID and product ID as the key, such as User_1#Earphones_1. The value corresponding to the key is the quantity in the shopping cart that the user is prepared to purchase. Some of the products may be limited-time special purchases, so they have an expiration time, which is the expiration time corresponding to the key.

The commands involved are as follows:

This solution will have the following problems:

  • Additional splicing increases the workload of encoding and decoding development
  • When a user obtains his/her shopping cart list, he also needs to scan all keys through the scan command prefix matching, and obtain the corresponding value through the get command.
  • When you want to directly obtain the list length, you still need to traverse the entire number of prefix keys, which is a complicated method.
  • There are a large number of duplicate username prefixes, which wastes storage space.

2. Implement shopping cart function based on Hash

Shopping cart management can be implemented based on the Hash type as shown in the figure. The user ID is used as the key, the product ID is used as the field, and the value is the number of corresponding products in the shopping cart. For some limited-time special purchase products, the expiration time is put into the value corresponding to the field through splicing.

The commands involved are as follows:

This solution has many optimizations compared to the String type solution:

  • Getting the list of all products in a user's shopping cart only requires one hgetall command
  • When getting the list length of a certain user, just get it directly from hlen
  • There is no problem of a large number of duplicate username prefixes

However, this solution still has an obvious shortcoming, that is, it is complicated to process some limited-time special products: for User_1's Keyboard_1 product, if you want to add another quantity, you cannot use hincrby directly, but need to hget to obtain the value of the Keyboard_1 product first. And decode, plus the specified number and then encode the value corresponding to hset.

3. Implement shopping cart function based on exHash

Shopping cart management is implemented according to the exHash type as shown in the figure. Like the Hash type, the user ID is used as the key, the product ID is used as the field, and the value is the number of the corresponding products in the shopping cart. For some limited-time special items, since the exHash type can set the expiration time for the Field, the expiration time can be set directly through the hset command.

The commands involved are as follows:

The optimization of this solution compared to the Hash type is mainly reflected in the fact that the expiration time can be directly set for each field, making it simple and efficient to use on the business side. It can be seen that the commands related to the exHash type are similar to the Hash type. The learning cost is very low when using it, and the cost of transformation on the business side is relatively low.

Figure 1.1 The use of user ID, product ID, product quantity, and exhash type commands in the Huawei Mall shopping cart.

Summarize

This article introduces the characteristics, usage and application scenarios of the exHash type of the GeminiDB Redis interface. It provides customers with an exHash type whose syntax is similar to the native Redis Hash type, is isolated from the use of the Hash type, and supports setting the expiration time and version separately for the Field as a solution for various complex scenarios. In the future, the GeminiDB Redis interface will continue to develop more useful enterprise-level features to help customers easily operate and maintain and develop efficiently.

If your business needs a stable and reliable KV database, you can try the GeminiDB Redis interface.

Directory of the team’s previous technology sharing: http://3ms.huawei.com/km/blogs/details/13802925 . You can also join our communication group!

Click to follow and learn about Huawei Cloud’s new technologies as soon as possible~

OpenAI opens ChatGPT Voice Vite 5 for free to all users. It is officially released . Operator's magic operation: disconnecting the network in the background, deactivating broadband accounts, forcing users to change optical modems. Microsoft open source Terminal Chat programmers tampered with ETC balances and embezzled more than 2.6 million yuan a year. Used by the father of Redis Pure C language code implements the Telegram Bot framework. If you are an open source project maintainer, how far can you endure this kind of reply? Microsoft Copilot Web AI will be officially launched on December 1, supporting Chinese OpenAI. Former CEO and President Sam Altman & Greg Brockman joined Microsoft. Broadcom announced the successful acquisition of VMware.
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4526289/blog/10150733