The background,

Inventory module is a update database intensive modules, all have inventory updates, although the online inventory using the Oracle database independently, already meet the business use of the short term, but as the koala business continues to grow, database performance bottlenecks that nots allow to ignore, the database on the same line of hot update is imminent. Therefore, the inventory has realized a set of sharding scheme for the hot commodity withholding inventory. Although it has not been used yet, as a technical reserve, it provides a guarantee for the possible future growth of koala business.

2. Definition and discovery of hot commodities

Common goods: goods with low concurrency of single withholding inventory; Second kill goods: goods with less inventory and high concurrency; Hot goods: more inventory, the order of high inventory concurrent goods. The significance of differentiating a split-kill item from a hot item is that the split-kill item will soon subtract the inventory to zero, and there will be no more destocking requests after that. There is no need for hot sharding, so the hot sharding discussed below does not include split-kill items. How to find hot commodities can be determined by the following four ways, of course, not all of them are realized: 1. (Realized) Operation provides, before promoting the promotion plan to find the hot commodities, configure the hot commodities white list in advance; 2. 2. (Realized) Statistics of the order flow, statistics of the order flow in the recent period, and find out the records of hot commodities frequently deducted from the inventory; 3. (not yet realized) Statistics of TOP commodities in shopping cart in recent period, and transaction link is provided; 4, (not yet implemented) Statistics of the most recent period of commodity details page visit TOP commodities.

Third, hot commodity fragmentation technical scheme

1. Inventory fragmentation record table. At the same time, there will be a hotspot field to identify in the inventory table


The principle of hot sharding is to split the original record into multiple records, and deduct different records randomly from the single withholding inventory to improve the performance ceiling of the single commodity withholding inventory. Then the records stored in the original inventory table are called master sharding records, and the generation of multiple records in the sharding table is called sub-sharding records, also known as sharding records; When the master shard’s saleable inventory is assigned to the shard table, the master shard’s foreground inventory field is deducted to prevent oversold. Currently, the system is configured with at least 2 shards, because if only 1 shard does not improve performance, the maximum number of shards is 10, and each shard inventory (including the master shard) is at least 100 inventory. If these requirements are not met, the inventory will not be sharded. Shard inventory and shard quantity determination:


3. Data consistency guarantee Data consistency is guaranteed through database transactions from master shard to shard table and from shard table to master shard; Service layer and external through the Dubbo interface inventory query, the Dao layer will directly add fragmented inventory to the front inventory, so that the upper code can not sense the existence of fragmented inventory; Add inventory or cancel order add inventory, directly add inventory to the master sharding record, also do not perceive sharding; If hot fragmented inventory is detected, an exception will be thrown up from the Dao layer to degrade the operation.

4. Open and close hot commodities

1. Start the hot spot timing task. Determine the range of hot commodities by executing the “hot spot commodity discovery”, exclude the hot spot blacklist record, initialize and allocate the inventory fragments that have not been opened and the available inventory meets the fragment quantity requirements. 2. Disable the hotspot scheduled task. Check the hotspot blacklist configuration, recover the fragmented inventory, and close the hotspot record label in the inventory table. 3. Adjust the fragment inventory periodic task to traverse all hot commodities, check the fragment inventory and the main inventory. If the fragment inventory has been reduced to 30%, the inventory allocation will be triggered automatically, and the number of fragments will be considered.

5. Quasi real-time adjustment of single – order fragment inventory and fragment inventory

Order withholding inventory process: 1, check the inventory before placing orders, inventory field because of the front desk is the primary fragmentation and the subdivision of the sum of the inventory, therefore do not need to change the original logic, when it directly prompt stock less than 2, insufficient inventory inventory check the shard hot with no open, if there is no open, is the original logic deduction, directly deduct the shard 3, inventory records for open hot target, 4. If there is not enough sub-fragment inventory, check whether the master fragment inventory is enough; if the master fragment inventory is enough, deduct the master fragment inventory. 5. Is deducting the shards of inventory at the same time 6, deducting the shard stock deduction in shard ID fixed order, guarantee the deadlock problem, if deduct one shard returned to insufficient inventory database, will deduct the time on the primary shard try to deduct once, reduce inventory due to insufficient shard stock concurrent but main divided enough under the condition of order failed


Quasi real-time adjustment of fragment inventory after order withholding inventory: 2. If the shard has inventory and at least one shard has more than 30 inventory, the shard inventory adjustment will not be carried out. 3. But at this time will use distributed lock to ensure that only one machine for a commodity adjustment, and 3 seconds a commodity will only trigger 1 fragmentation inventory adjustment, prevent avalanche 4, if there is inventory, then try to divide inventory allocation, if not much, then try to close the hot spot



Vi. Performance pressure test results (quoting pressure test data of pressure test group)


Test environment:

  • Inventory Deploy a cloud host with 6 cores and 8 GB server configuration. Use the performance environment database for the database

Data model:

  • Select hot commodity and non-hot commodity data for pressure measurement

Performance data:


Conclusion:

  • Because the single withholding inventory is optimized at the same time during the development of hot fragmentation, the performance of the same non-hot commodity inventory is 1800TPS after optimization, which is 1100TPS more than 700 before optimization.
  • After deducting the inventory performance of the same hot spot commodity to be 5700TPS, the performance benefit of hot spot scheme is more than 3 times;
  • Because the TPS of non-hot commodities is significantly higher than before, it is not recommended to start hot sharding even if the inventory quantity is less than 2000 or the deduction of the same commodity is not concurrent; The performance of a single fragment is 5700 and a single fragment supports 1600. Therefore, do not set the number of fragments to too large.


Seven, there are still problems

After the hot spot function is enabled, it will automatically downgrade the automatic disassembling inventory and the loss of purchase, sale and stock. Therefore, the hot spot sharding function cannot be enabled for a long time