Interactive Viewing Experience for the World Cup Using Redis

In this article, we look at how Alibaba Cloud supported 70% of the World Cup live broadcast traffic in China using Redis.

On the evening of June 14, the 2018 FIFA World Cup kicked off in Moscow, Russia. Tens of millions of people in China watched the opening game using live broadcasting services such as Youku, CBox, and Migu Video.

According to a report released by Alibaba Cloud, the first wave of traffic peaks appeared in the 44th minute after the opening of the game. The peak traffic reached 1.5 times of the peak traffic from the 2018 Spring Festival Gala. Because of this, the FIFA World Cup has become the largest online live broadcast in history.

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.

Although these integrated apps sound simple, hosting it in such a massive scale is no easy task. You need to take into consideration several aspects, such as business architecture, application scenarios, high availability construction, and flexible resizing. Let's take a look at how Redis has made all these possible.

Business Architecture

The above picture is an enterprise's live broadcast solution, which mainly uses elastic computing, CDN, smart dynamic encoding technology, video AI, narrowband HD 2.0, and Redis to fully guarantee a high-definition experience while saving bandwidth. This solution optimizes resources and costs, and brings a smooth and rich interactive experience to the users.

In this article, we will not discuss how the video is recorded and broadcast. Instead, we will be focusing on the important roles and applications scenarios, as well as the use of Redis in enriching the live broadcast experience.

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.

Application scenarios of Redis include:

Session cache

Full Page Cache (FPC)

Mobile phone verification code

Access frequency limit/blacklist/whitelist

Message queueing

Merchandise inventory management

GEO function added with LBS (location-based service) development

In actual use, you need to choose the correct data type according to your own business scenario.

Live Chat

Such a chat room can be implemented using Hash, for example: {userID:contents}

User ID

Content

11101000

Broke

Redis can also be used to filter out spammers in the chat room. Of course, this is usually done by a spam filter in addition to Redis. For example, to limit the number of comments by a user within a minute:

Method 1: Implement through sorted set.

Record the user comments on the day, use timestamp as score, and then use the Zset commands RANGEBYSCORE, ZADD, and ZRANGEBYSCORE.

Game Status Live Update

The latest game schedules can be implemented with Redis's List data structure or sorted set data structure, in conjunction with the expiration time. The latest scores, rankings, and other match information, can be easily implemented using this method as well.

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:

String:

INCR active16

GET active16

Hash:

HSET active:active16 likes 0

HINCRBY active:active16 likes 1

HGETALL active:active16

Red Envelope Inventory Management

The following describes a "red envelope" implementation based on Redis.

The original red envelope is called a big red envelope, and the split red envelopes are called small red envelopes.

Small red envelopes are pre-generated and inserted into the database. The user IDs corresponding to the red envelopes are null.

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'}

Use a map in Redis to filter users who have already snatched a red envelope.

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.

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.

Although the above process is straightforward, how can we avoid users from fooling the system? For example in Step 4, if a user taps twice quickly, or opens two browsers to simultaneously snatch red envelopes, is it possible for the user to snatch two red envelopes?

In order to solve this problem, the Lua script is adopted, so that the whole process of Step 4 is performed atomically.

Message Push

The pop-up message in the above picture is not necessarily a Redis implementation but an illustration of what Redis is capable of.

There is no persistence mechanism for such message notification implemented by the pub/sub mechanism, which is a disposable model. Producers don't need to care about how many subscribers they have, or subscriber's detailed information. Online client (consumers) is generally visible, just like a TV program. You can obtain the update notification as long as you are online.

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.

As mentioned earlier in the article, using public cloud is the best solution for video platforms such as Youku, CBox, and Migu Video. Public cloud helps ot ensure business stability and reduce costs.

Let us now look at the specific implementation of HA and elastic scaling for this application.

"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.

With remote data disaster recovery and multiple active standbys, once a failure occurs, you can quickly take over the service remotely to ensure that user experience is not affected. In addition, many of the widely distributed services require users to remotely access across regions. If the access latency is high at this time, the user experience will be directly affected. Multiple active standbys on the cloud provided by ApsaraDB for Redis can also help users eliminate latency when remotely accessing across regions.

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