High concurrency flash sale plan: hotspot hashing, inventory bucketing, you need to know about it

2024.01.31

In large-scale flash sales activities, the peak inventory deduction request for a single product can easily reach tens of thousands or even hundreds of thousands of QPS, such as the common Moutai grabbing activities. In this scenario, inventory deduction based on the database is powerless. Remember one key indicator: in MySQL, the current performance of a single row update operation is about 500QPS. For inventory deductions that can easily reach tens of thousands of QPS, this level is definitely on the low side.

Therefore, in order to deal with this high concurrency scenario, the industry has proposed a solution called hotspot hashing, which is the inventory bucketing discussed in the group today.

The solution is shown in the figure below: allocate the inventory of the same product to multiple "buckets" in advance, and route inventory requests to different buckets according to routing rules (random, UID modulus), thereby dispersing requests concentrated on a single instance. This scenario is similar to horizontal scaling.

picturepicture

As for the technical implementation of "bucketing", many technical articles or solutions recommend using Redis for implementation. Specifically, any product in a flash sale can be divided into N parts, each of which corresponds to a cache key. The composition of the cache key must follow certain rules to facilitate routing. Examples are as follows:

key: inventoryId_1, value: inventory quantity 

key: inventoryId_2, value: inventory quantity 

... 

key: inventoryId_N, value: inventory quantity

When deducting inventory, you can use random algorithm, UID modulus, etc. to determine a number based on the Key number range, and then assemble the Key access cache. 

For example, if the inventoryId is 20221821 and the inventory is allocated to 100 buckets, the range according to the corresponding Key is [1,100].

After the server receives the product inventory deduction request, it modulo the UID parameter in the request. Assuming UID % 100 = 67, the assembly key is 20221821_67, and the inventory in the corresponding cache bucket is deducted based on 20221821_67.

The core idea of ​​hotspot hashing is: deduct inventory in the cache to improve the throughput of the system; after the cache deduction is successful, the inventory deduction flow is asynchronously written to the database and the inventory is updated; in addition, mechanisms such as scheduled tasks are also required. Achieve synchronization of cache and database inventory totals.

This solution looks good, but it also has the following three problems:

1. How to ensure idempotence when deducting inventory from cache? If idempotence prevention and control is insufficient, repeated deductions may occur, resulting in less sales.

2. Cache write operations and database write operations cannot ensure strong consistency through transaction mechanisms. So how to effectively ensure the consistency of inventory data?

3. The inventory seen by the user should be the total inventory. Even if the total inventory is sufficient, once it is divided into barrels, how to ensure that the barreled oil to which the user request is routed has sufficient inventory?

Therefore, I personally feel that hotspot hashing is the most effective solution to the inventory hotspot problem in theory, but in practical applications, there are many details that need to be considered. The cache-based inventory reduction solution is relatively crude and only meets the needs of some specific scenarios. For large e-commerce platforms such as Taobao and JD.com, which have billions of products, the problems they face are much more complex. In addition to stability, reliability, and consistency, they also include inventory allocation, inventory fragmentation, inventory Expansion and contraction, traffic tilt, undersold products, oversold products, etc.

When I went to Alibaba last time to communicate, Alibaba expert Tang San said: In Alibaba, the inventory architecture adopts a inventory unit architecture. This solution should be the ultimate solution for the inventory system of large e-commerce platforms.

Editor in charge: Wu XiaoyanSource: JAVA Daily Record