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.
During the game, it is estimated that 70% of the live broadcast traffic in China went through Alibaba Cloud. Source in Chinese: https://www.leiphone.com/news/201806/Du10JxOxuJ6Ou782.html
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.
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.
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:
In actual use, you need to choose the correct data type according to your own business scenario.
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.
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
Method 2: Use Redis + Lua to acchieve access frequency control
# 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
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.
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.
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
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.
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.
The following is a Lua script executed on Redis:
-- 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
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.
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.
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.
In short, Redis is an indispensable component for enterprise architecture in a wide variety of scenarios.
You can learn more about Redis on Alibaba Cloud by visiting the ApsaraDB for Redis product page.
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
Interview with the Creator of Redisson – Building an Open Source Enterprise Redis Client
Disaster Tolerance and Remote Multi-Active Regions with MongoShake
Alibaba Clouder - April 30, 2020
Alibaba Clouder - August 16, 2021
Alibaba Clouder - November 30, 2018
Alibaba Clouder - February 26, 2021
Alibaba Clouder - November 8, 2018
Alibaba Clouder - June 26, 2018
Leverage cloud-native database solutions dedicated for FinTech.
Learn MoreMigrate your legacy Oracle databases to Alibaba Cloud to save on long-term costs and take advantage of improved scalability, reliability, robust security, high performance, and cloud-native features.
Learn MoreMigrating to fully managed cloud databases brings a host of benefits including scalability, reliability, and cost efficiency.
Learn MoreDBStack is an all-in-one database management platform provided by Alibaba Cloud.
Learn MoreMore Posts by ApsaraDB