You can use ApsaraDB for Redis to build a live-streaming channel information system that has low latency and can withstand high traffic volumes.

Background information

Live-streaming channels are one of the main features of the live-streaming system. Except for the live-streaming window, live users, virtual gifts, comments, likes, rankings, and other data generated during the live streaming are time-limited, highly interactive, and delay-sensitive. The Redis caching service is a suitable solution to handle such data.

The best practice in this topic demonstrates how to use ApsaraDB for Redis to build a live-streaming channel information system. This topic describes how to build a live-streaming channel information system for three types of information:

  • Real-time ranking information
  • Counting information
  • Timeline information

Real-time ranking information

Real-time ranking information includes an online user list, a ranking of virtual gifts, and live comments. Live comments can be considered as a message ranking that is sorted based on message dimensions. The sorted set structure in Redis is suitable to store the real-time ranking information.

Redis sets are stored in hash tables. The time complexity of create, read, update, and delete (CRUD) operations is O(1). Each member in a set is associated with a score to facilitate sorting and other operations. The following example shows how sorted sets work to build a live-streaming channel information system. The added and returned live comments are used in the example.

  • Use unix timestamp + millisecond as the score format to record the last five live comments in the user55 live-streaming channel:
    redis> ZADD user55:_danmu 1523959031601166 message111111111111
    (integer) 1> ZADD user55:_danmu 1523959031601266 message222222222222
    (integer) 1> ZADD user55:_danmu 1523959088894232 message33333
    (integer) 1> ZADD user55:_danmu 1523959090390160 message444444
    (integer) 1> ZADD user55:_danmu 1523959092951218 message5555
    (integer) 1
  • Return the last three live comments:
    redis> ZREVRANGEBYSCORE user55:_danmu +inf -inf LIMIT 0 3
    1) "message5555"
    2) "message444444"
    3) "message33333"
  • Return three live comments within the specified period of time:
    redis> ZREVRANGEBYSCORE user55:_danmu 1523959088894232 -inf LIMIT 0 3
    1) "message33333"
    2) "message222222222222"
    3) "message111111111111"

Counting information

For user-related data, the counting information includes the number of unread messages, followers, and fans, and the experience value. The hash structure in Redis is suitable to store this type of data. For example, the number of followers can be processed in the following way:

redis> HSET user:55 follower 5
(integer) 1
redis> HINCRBY user:55 follower 1 //Number of followers +1
(integer) 6 
redis> HGETALL user:55
1) "follower"
2) "6"

Timeline information

Timeline information is a list of information sorted in chronological order. Timeline information includes anchor moments and new posts. This information type is arranged in a fixed chronological order and can be stored by using a Redis list or an ordered list. Example:

redis> LPUSH user:55_recent_activity  '{datetime:201804112010,type:publish,title: The show starts, content: Come on}'
(integer) 1
redis> LPUSH user:55_recent_activity '{datetime:201804131910,type:publish,title: Ask for a leave, content: Sorry, I have plans today.}'
(integer) 2
redis> LRANGE user:55_recent_activitiy 0 10
1) "{datetime:201804131910,type:publish,title:\xe8\xaf\xb7\xe5\x81\x87\",content:\xe6\x8a\xb1\xe6\xad\x89\xef\xbc\x8c\xe4\xbb\x8a\xe5\xa4\xa9\xe6\x9c\x89\xe4\xba\x8b\xe9\xb8\xbd\xe4\xb8\x80\xe5\xa4\xa9}"
2) "{datetime:201804112010,type:publish,title:\xe5\xbc\x80\xe6\x92\xad\xe5\x95\xa6,content:\xe5\x8a\xa0\xe6\xb2\xb9}"

Related resources