Interactive Viewing Experience for the World Cup Using Redis

Interactive Viewing Experience

You may have noticed that compared to the previous World Cups, content platforms and providers have introduced novel ways for viewers to be a part of the action. In China, viewers can interact with other viewers through live chat. They can also take part in several activities to win “red envelopes”, all directly through the app client.

Business Architecture

Application Scenarios

Redis can be widely used in enterprise architecture as an indispensable component. One important reason is that it has rich data structures, which is also why it is becoming more popular and gradually replacing Memcached. It supports data structures including: String, List, Hash, set, Sorted set, bitmap, bit field, hyperLogLog, and Geospatial Index.

  1. Session cache
  2. Full Page Cache (FPC)
  3. Mobile phone verification code
  4. Access frequency limit/blacklist/whitelist
  5. Message queueing
  6. Merchandise inventory management
  7. GEO function added with LBS (location-based service) development

Live Chat

RANGEBYSCORE userID:10000:operation:comment 61307510405600 +inf //Get the operation records within 1 minute
Redis> ZADD userID:10000:operation:comment 61307510402300 "This is a comment" //score is timestamp (integer) 1
Redis> ZRANGEBYSCORE userID:10000:operation:comment 61307510405600 +inf //Get the operation records within 1 minute
# KEYS[1] indicates the limited number, passed in by the caller
local key1 ,key2 = 'access:limit' ,'access:expire'
if redis.call('EXISTS' ,key2) > 0 then
return redis.call('DECR' ,key1) ;
else
redis.call('SET' ,key2 ,1)
redis.call('EXPIRE' ,key2 ,60)
redis.call('SET' ,key1 ,KEYS[1])
return KEYS[1]
end

Game Status Live Update

Message Notifications (Number of Unread Messages)

To implement this, you can use hash to store the time the user last viewed a message, use sorted set to store the time generated for each message of each module (comment area, group chat), and record the number of unread messages.

Likes (Number of Likes)

The number of likes is easy to implement through string or hash data structure. For likes that are not retracted, just use the command “incr”. Assuming that the key is the game number “active16”, we can record the number of likes as follows:

Red Envelope Inventory Management

The following describes a “red envelope” implementation based on Redis.

  1. Small red envelopes are pre-generated and inserted into the database. The user IDs corresponding to the red envelopes are null.
  2. Each big red envelope corresponds to two Redis queues, one is for the unconsumed red envelopes, and the other is for the consumed red envelopes. At the beginning, all small red envelopes are put into the unconsumed red envelope queue. The unconsumed red envelope queue is a Json string, the activeID is the game number, the money is the red envelope amount, and the product is the number of products. For example: {activeId:’16', money:’300'} or {activeID:’16',product:’50'}
  3. Use a map in Redis to filter users who have already snatched a red envelope.
  4. When a user snatches the red envelope, first determine whether or not the user has already snatched a red envelope. If not, then take a small red envelope from the unconsumed red packet queue, then push it to the other queue, and finally put the user ID into the deduplicated map.
  5. Use a single thread to batch take the red envelopes from the consumed queue, and then batch update the user IDs of the red envelopes in the database.
-- Function: Try to get the red envelope, if it succeeds, return the Json string; if not, return null
-- Parameters: red envelope queue name, consumed queue name , deduplicated map name, user ID
-- Return value: nil or Json string, including user ID: userId, red envelope ID: id, red envelope amount: money
-- If the user has already snatched a red envelope, return nil
if redis.call('hexists', KEYS[3], KEYS[4]) ~= 0 then
return nil
else
-- take a small red envelope
local hongBao = redis.call('rpop', KEYS[1]);
if hongBao then
local x = cjson.decode(hongBao);
-- add user ID information
x['userId'] = KEYS[4];
local re = cjson.encode(x);
-- put the user ID to the deduplicated set
redis.call('hset', KEYS[3], KEYS[4], KEYS[4]);
-- put the red envelope to the consumed queue
redis.call('lpush', KEYS[2], re);
return re;
end
end
return nil

Message Push

High Availability and Elastic Scaling

During the World Cup, traffic is not only high but also bursty. Because of the unpredictability of traffic loads, responding to the impact of sudden traffic surges while ensuring the stability of online services is a challenging and meaningful challenge. To achieve this goal, we need a comprehensive, stable, reliable, and robust database O&M system to provide support and management.

High Availability: Remote Data Disaster Recovery and Multiple Active Standbys

“Don’t put all your eggs in one basket.” I believe that many cloud architects will definitely think so and do so. You can build a high availability architecture, or you can directly use the remote data disaster recovery and multiple active standbys provided by Alibaba Cloud Redis service. The instances are deployed across regions and are automatically synchronized in both directions.

Elasticity: Resource Scaling, Read/Write Splitting

When access in a region is abnormal, a series of measures such as downgrading, traffic switching, and traffic limiting can be used to ensure the stability of the service. In the cloud mode, when the service traffic increases, you can use the Alibaba Cloud Redis service to automatically scale everything. You can also use the read/write splitting function to reduce the read pressure with a low cost or unload the storage through the hybrid storage.

Summary

In short, Redis is an indispensable component for enterprise architecture in a wide variety of scenarios.

About the Author:

Zhang Donghong: Alibaba Cloud MVP, director and technical expert of the Department of Foreign Cooperation of Cloud-Ark, chairman of China Redis User Group, member of the All China MySQL User Group Presidium

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com