GaussDB (for Redis) game practice: reporting player offline behavior

This article is shared from Huawei Cloud Community "GaussDB (for Redis) Game Practice: Reporting Player Offline Behavior" , author: GaussDB Database

In order to protect the physical and mental health of minors, the country launched an anti-addiction system for online games in 2007 to limit the gaming time of minors. Game manufacturers need to detect users' offline time in a timely manner and report it. Redis is one of the important choices for game databases. When implementing the above functions based on open source Redis, the perceived user offline behavior is delayed greatly, resulting in inaccurate reporting time. As an enterprise-level game database, Huawei Cloud GaussDB (for Redis) has excellent enterprise-level capabilities, can report user offline behavior in a timely manner, and is widely used in various business scenarios such as rankings.

1. Implementation of user offline reporting based on Redis

Common ways to implement users’ offline reporting capabilities

Using the Redis key expiration function, combined with the key space notification function, users can be reported offline. Common usage methods are as follows:

1) After the user logs in, set an expiration time (3-5 minutes) for each user key.

2) The game client regularly reports a heartbeat every minute. After receiving the heartbeat, the server resets the expiration time of the game user key.

3) In order to avoid failure to report in time due to network fluctuations, if a heartbeat is received within 5 minutes, the expiration time will be reset; if not received, the key will be triggered to expire and the system will determine that the user is offline.

Therefore, the Redis key space notification function must detect key expiration in a timely manner to ensure the accuracy of the reporting time.

Redis keyspace notification function

Redis keyspace notification allows users to receive key modification, expiration and other notifications by subscribing to channels or patterns. For each key modification, keyspace notifications will send two different types of events. Taking the expiration of DB0 user mykey as an example, Redis will send two messages, which is equivalent to executing two publish commands:

  • PUBLISH __keyspace@0__:mykey expire
  • PUBLISH __keyevent@0__:expire mykey

By subscribing to channel __keyspace@0__:mykey, you can receive events for all modified keys mykey in database No. 0, and by subscribing to channel __keyevent@0__:expire, you can receive all keys that execute the expire command in database No. 0. The channels prefixed with keyspace are called keyspace notifications, and the channels prefixed with keyevent are called key event notifications.

You can turn on or off the keyspace notification function through the command CONFIGSET notify-keyspace-events [parameter]. If the parameter is empty, it means that the function is turned off. If the parameter is not empty, it means that it is turned on. Usually the parameter is set to "AKE", which means sending all types of notifications.

character

Notifications sent

K

Keyspace notifications, all notifications are prefixed with __keyspace@<db>__

E

Key event notification, all notifications are prefixed with __keyevent@<db>__

g

Notification of type-independent general commands such as DEL, EXPIRE, RENAME, etc.

$

String command notification

l

List command notifications

s

Collection command notification

h

Notification of hash command

z

Notifications for ordered set commands

x

Expiration event: Sent whenever an expired key is deleted

e

Evict event: Sent whenever a key is deleted due to maxmemory policy

A

Alias ​​of parameter g$lshzxe

Through the following command, you can subscribe to all expired user keys of DB0

redis-cli --csv psubscribe '__keyevent@0__:expire'

2. Comparison of expiration key space notification delays between GaussDB (for Redis) and open source Redis key

Redis specifications : all use 4GB specifications

Test steps :

1) Use memtier_benchmark to preset 100,000 keys

2) Use the client's regular key expiration events

3) Use a python script to set an expiration time of 10 seconds for 30,000 keys.

4) Statistics on how long it takes to receive 30,000 key expiration notifications in scenarios with and without business traffic.

Test Results:

It takes time to receive all keys when there is no business traffic and expire.

It takes time for all keys to expire when there is business traffic

GaussDB(for Redis)

9 seconds

9 seconds

Open source Redis

3 minutes and 41 seconds

3 minutes and 44 seconds

It can be seen that in scenarios with or without business traffic, GaussDB (for Redis) only needs 9 seconds to complete the reporting of all key expirations, while community Redis takes about 4 minutes to complete the reporting, which seriously affects the accuracy of user offline behavior reporting. .

3. Principle analysis

The open source Redis key space notification function adopts two strategies: lazy deletion and periodic deletion, that is, expiration check is performed during access, and periodic check tasks are performed in the background at a certain frequency. This can be adjusted by modifying the hz option of the configuration file redis.conf. frequency. Each expired task will be deleted according to the following process:

  1. Randomly check 20 keys from the set of keys with expiration time set.
  2. Delete all expired keys found in the check.
  3. If more than 25% of the keys in the check results have expired, a new round of tasks will start.

It can be noted that open source Redis does not check all keys in all libraries in one run, but randomly checks a certain number of keys, resulting in a long reporting delay. GaussDB (for Redis) has a real-time thread in the background that continuously scans keys and reports expired keys in a timely manner without affecting front-end write operations.

4. Summary

GaussDB (for Redis) is an enterprise-level KV database that surpasses open source Redis. In game scenarios, in addition to being used in game player offline scenarios, it is also widely used in player data storage, rankings, friend relationships, message push, etc. Scenes. Adopting an architecture that separates storage and computing, it can not only meet the high concurrency performance index requirements of the game business, but also reduce costs and increase efficiency. It is favored by game developers.

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

Guess you like

Origin blog.csdn.net/devcloud/article/details/132965912