Inventory deduction method

By employing distributed transactions and dynamic inventory migration for inventory deduction in high-concurrency scenarios, the problems of over-deduction and data consistency in the inventory management system under high-concurrency conditions are solved, achieving accurate inventory data management and improved system stability.

CN120067123BActive Publication Date: 2025-11-25北京蜂创科技有限公司
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510223911.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-02-27
Publication Date
2025-11-25
Estimated Expiration
2045-02-27

AI Technical Summary

Technical Problem

In high-concurrency scenarios, existing inventory management systems are prone to issues such as over-deducting inventory, data update delays, and data inconsistency, leading to an imbalance in prize distribution and affecting the smooth progress of marketing activities.

Method used

An inventory deduction method based on distributed transactions and dynamic inventory migration is adopted. Through hierarchical storage of the first and second databases, unified processing within the time window, and the fault tolerance mechanism of distributed locks, combined with transactional deduction rules and dynamic migration strategies, the accurate management of inventory data is ensured.

Benefits of technology

It improves the stability and data consistency of the system in high-concurrency scenarios, reduces error rates and overcharging, and ensures the smooth operation of marketing activities.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120067123B_ABST
    Figure CN120067123B_ABST
Patent Text Reader

Abstract

The application relates to the technical field of inventory deduction and discloses an inventory deduction method, which comprises the following steps: storing prize information of already deducted inventory and prize information of inventory being deducted into a first database; storing prize information of temporarily deducted inventory into a second database; counting the number of each type of prize in a request set, which is recorded as a deduction number y of each type of prize; executing a temporary storage judgment strategy to judge whether the prize information is stored into the second database; executing a distributed lock fault-tolerant uploading mechanism to store the prize information into the second database at the end moment of a time window; executing a transaction deduction strategy according to a transaction deduction rule to store the prize information into the first database; acquiring a record number m of the prize information of the inventory being deducted in the first database; and executing a dynamic inventory migration strategy to change the storage position of the prize information in the first database. The application can improve the operation efficiency of the system, effectively reduce the error rate and over-deduction phenomenon in the inventory management process.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of inventory deduction technology, specifically to inventory deduction methods. Background Technology

[0002] In marketing campaigns, prize inventory management is a core element for the smooth operation of the campaign. With the rapid development of internet technology, online prize draws have become an important means of attracting users and increasing brand exposure. However, a key issue often faced in these campaigns is how to effectively manage prize inventory, especially under high concurrency, to prevent instantaneous inventory depletion from exceeding the actual plan, leading to prize distribution imbalances or even campaign failure.

[0003] Currently, most inventory management systems use a direct deduction method, which reduces inventory directly when a user wins a prize. This method works well in low-concurrency scenarios, but has significant shortcomings in high-concurrency scenarios. When a large number of users participate in the lottery simultaneously, over-deduction of inventory may occur, leading to insufficient prizes. Furthermore, delays in inventory data updates and data consistency issues can cause discrepancies between prize distribution and actual inventory levels, further exacerbating operational risks.

[0004] This technical solution proposes an inventory deduction method based on distributed transactions and dynamic inventory migration. Through hierarchical storage of a first and second database, unified processing within a time window, and a distributed lock fault-tolerance mechanism, it ensures accurate inventory data management in high-concurrency scenarios. Simultaneously, by combining transactional deduction rules and dynamic migration strategies, it integrates the security and real-time performance of inventory operations, improving system efficiency while effectively reducing error rates and over-deductions in inventory management. This provides a solid technical guarantee for the smooth operation of marketing activities. Summary of the Invention

[0005] This invention provides an inventory deduction method, which helps to solve the problems mentioned in the background art above.

[0006] Firstly, this application provides an inventory deduction method, employing the following technical solution: the inventory deduction method includes:

[0007] For marketing activities that involve random checks of prizes;

[0008] Store the prize information that has been deducted from inventory and is being deducted from inventory into the first database;

[0009] The information on prizes temporarily deducted from inventory will be stored in the second database;

[0010] Set the time window ΔT;

[0011] Collect all requests for deducting inventory within each time window and form a request set;

[0012] The quantity of each type of prize in the statistical request set is recorded as the deduction quantity y for each type of prize;

[0013] For any type of prize, execute a temporary storage decision strategy to determine whether to store the prize information in the second database; if the prize information is stored in the second database:

[0014] At the end of the time window, a distributed lock fault-tolerant upload mechanism is executed to store the prize information in the second database. By introducing separate storage of prize information in the first and second databases, the inventory management process is significantly optimized. This design effectively reduces database operation conflicts and improves the system's stability under high concurrency scenarios. Simultaneously, the time window setting centralizes batch request processing, reducing the pressure on real-time computing and ensuring system efficiency. The introduction of transactional deduction rules further guarantees data consistency; even in the event of storage failure, temporary deduction information can be updated promptly, preventing data corruption. The dynamic inventory migration strategy adjusts the data storage location based on real-time traffic, optimizing database performance and enhancing the system's ability to handle high traffic.

[0015] Set a transactional deduction rule, which is that if the storage of prize information fails during the process of storing it into the first database, the deduction quantity of the prize in the second database will be updated.

[0016] According to the transactional deduction rules, the transactional deduction strategy is executed, and the prize information is stored in the first database;

[0017] Retrieve the number m records of prize information that are being deducted from inventory in the first database;

[0018] Set an early warning ratio coefficient α to determine whether to modify the prize information that is being deducted from inventory to the prize information that has already been deducted from inventory;

[0019] Implement a dynamic inventory migration strategy to change the storage location of prize information in the first database.

[0020] For any type of prize, a temporary storage judgment strategy is executed to determine whether to store the prize information in the second database, including:

[0021] Enter the query command in the prize basic information table to query the total inventory quantity x of the prizes;

[0022] If the query command returns an empty value, it will display a message indicating that the deduction failed.

[0023] If the query command returns the total inventory quantity x;

[0024] Then, enter the query command in the first database to query the number of prizes that have been deducted from the inventory, and record it as the deducted quantity a;

[0025] Input a query instruction in the first database to query the number of prizes whose inventory is being deducted, denoted as the real-time deduction quantity b;

[0026] Input a query instruction in the second database to query the number of prizes with temporarily deducted inventory, denoted as the temporary deduction quantity c;

[0027] Compare the magnitude relationship between a + b + c + y and x;

[0028] If a + b + c + y > x, then display that the deduction fails;

[0029] If a + b + c + y ≤ x, then judge the magnitude relationship between c and a + b:

[0030] If c < a + b, then display that the inventory in the second database is inaccurate and the deduction fails;

[0031] If c ≥ a + b, then store the prize information in the second database.

[0032] By defining a temporary storage judgment strategy, a complete inventory verification mechanism is established before the deduction operation. This mechanism is based on multiple verifications of the total inventory and real-time deduction data, avoiding inconsistent inventory data. Especially in high-concurrency scenarios, the judgment of the relationship between a + b + c + y and x ensures the rationality of the deduction request, and the further comparison between c and a + b avoids abnormal deduction problems caused by inaccurate temporarily deducted inventory. This meticulous logical design improves the reliability of the deduction operation and provides accurate data support for subsequent transactional deductions and dynamic migrations.

[0033] Preferably, at the end of the time window, execute a distributed lock fault-tolerant upload mechanism to store the prize information in the second database, including:

[0034] Set a main lock and a backup lock;

[0035] Obtain the process of storing the prize information in the second database;

[0036] S1. Execute the lock acquisition instruction. The process attempts to acquire the main lock for operating the second database at the end of the time window, and judge whether the lock acquisition is successful:

[0037] S2. If the lock acquisition is successful, then execute the write instruction for the second database;

[0038] S3. If the lock acquisition fails, then set a fixed waiting interval;

[0039] S4. Set the maximum number of attempts, count the number of times the lock acquisition instruction is executed. If the number of times the lock acquisition instruction < the maximum number of attempts, then repeat S1 - S4 after the fixed waiting interval;

[0040] S5. If the number of lock acquisition commands is greater than or equal to the maximum number of attempts, then the backup lock upload mechanism will be executed.

[0041] The distributed lock fault-tolerance mechanism solves the problem of lock contention in high-concurrency environments. The dual protection design of the primary lock and the backup lock ensures stable system operation under high load scenarios. Setting fixed waiting intervals and maximum attempts makes lock contention more efficient while avoiding prolonged resource occupation. The introduction of the backup lock provides additional fault tolerance after the primary lock acquisition fails, improving the success rate of operations. Through this mechanism, the system can maintain the reliability and consistency of the deduction function even under extreme conditions.

[0042] Preferably, if the number of lock acquisition commands is greater than or equal to the maximum number of attempts, then the backup lock upload mechanism is executed, including: S6, executing the lock acquisition command, the process attempts to acquire a backup lock for operating the second database at the end of the time window, and determines whether the lock acquisition was successful:

[0043] S7. If the lock is successfully acquired, execute the write command for the second database;

[0044] S8. If acquiring the lock fails, count the number of times the lock acquisition instruction is executed. If the number of lock acquisition instructions is less than the maximum number of attempts, then repeat S6-S8 after a fixed waiting interval.

[0045] S9. If the number of lock acquisition commands is greater than or equal to the maximum number of attempts, then the deduction failure will be displayed.

[0046] By employing a backup lock mechanism to intervene promptly when the primary lock acquisition fails, the system gains additional security. A fixed retry strategy and explicit failure messages prevent the system from falling into an infinite contention trap. The introduction of backup locks not only improves the success rate of lock acquisition but also effectively shortens response time in high-concurrency scenarios. This design ensures that inventory deduction operations maintain high reliability even under surges in traffic, while providing new ideas and practical solutions for improving the robustness of distributed systems.

[0047] Preferably, if the lock is successfully acquired, the write instruction to the second database is executed, including:

[0048] The write instruction is:

[0049] Query the temporary deduction quantity c of prizes in the second database;

[0050] The `incre` command in the second database is executed to increase the temporary deduction amount `c` in the second database by the deduction amount `y`.

[0051] Query the temporary deduction amount d in the second database, where d = c + y;

[0052] Compare the magnitudes of a+b+d and x;

[0053] If a+b+d>x, then the deduction failed.

[0054] If a+b+d≦x, then the prize information has been successfully stored in the second database.

[0055] By employing refined data write commands, the accuracy and robustness of the system's operations on the second database are significantly improved. This multi-layered verification mechanism ensures that inventory data remains consistent even in high-concurrency scenarios, preventing over- or under-deductions. Even if an anomaly occurs during the write process, explicit verification conditions can promptly terminate the deduction process, displaying a deduction failure message, thereby effectively avoiding data contamination and system chaos. Through precise write logic and verification mechanisms, this method enhances the operational reliability of the second database, providing stronger security and operational assurance for inventory management.

[0056] Preferably, the step of executing a transactional deduction strategy according to transactional deduction rules and storing the prize information in the first database includes:

[0057] Details of deductions for winning prizes;

[0058] Execute the INSERT command in the first database to add the prize deduction details to the first database, and determine whether the addition was successful; if the addition fails, reduce the temporary deduction amount d of the prize in the second database by the deduction amount y.

[0059] If the addition is successful, the deducted quantity y will be added to the real-time deduction quantity of the prize in the first database.

[0060] By introducing a transactional deduction strategy, the accuracy and consistency of data processing are significantly improved. The retrieval and writing of deduction details are controlled by transactions, ensuring the atomicity of the operations. If storage fails, the temporary deduction quantity in the second database is immediately reduced, thus preventing inventory data corruption due to abnormal operations. This design provides reliable fault tolerance under high concurrency or abnormal conditions, ensuring the accuracy of the deduction quantity in the first database. Simultaneously, this mechanism simplifies the tracing and correction of the deduction process, further improving system stability and maintenance efficiency.

[0061] Preferably, the step of executing a dynamic inventory migration strategy to change the storage location of prize information in the first database includes:

[0062] In the first database, the information on prizes that have already been deducted from the inventory is stored in the first database;

[0063] The prize information that is being deducted from the inventory is stored in a second database;

[0064] Get the record capacity p of the second database;

[0065] Get the current query traffic R0;

[0066] Obtain the smoothed average flow rate R1 to determine whether the flow conditions for data migration are met;

[0067] Determine whether to trigger the migration of records from the first database to the second database:

[0068] If m≥α×p and R0≥R1, then the records in the first database can be migrated to the second database.

[0069] Calculate the number of records q for each migration:

[0070] Where β is the migration ratio coefficient.

[0071] A dynamic inventory migration strategy effectively addresses the performance bottleneck caused by database query traffic fluctuations. By storing deducted and ongoing prize information separately and combining this with a smoothing average traffic criterion, hierarchical data management and migration are achieved. Calculating the number of records (q) migrated each time and dynamically adjusting the migration ratio coefficient makes the data migration process more flexible. This mechanism not only improves database query performance under high traffic conditions but also reduces data storage imbalances, thereby enhancing the overall system efficiency.

[0072] Preferably, the step of executing a dynamic inventory migration strategy to change the storage location of prize information in the first database includes:

[0073] Get the current product sales rate R2;

[0074] Obtain the smoothed average of the sales rate, R3;

[0075] Obtain the initial migration frequency f0;

[0076] The update migration frequency is

[0077] Calculate migration interval

[0078] Update the smoothed average flow rate R1 = λ1 × R1 + (1 - λ1) × R0, where λ1 is the smoothing coefficient of the smoothed average flow rate;

[0079] The smoothed average of the updated sales rate is R3 = λ3 × R3 + (1 - λ3) × R2, where λ3 is the smoothing coefficient of the smoothed average of the sales rate.

[0080] The adjustment method of data migration frequency is optimized through the dynamic calculation of the sales rate and its smoothed average value. The initial migration frequency is set in combination with the real-time update of the sales rate, enabling the system to quickly respond to market changes. Meanwhile, the introduction of the smoothed average traffic ensures the stability of the migration strategy and avoids unnecessary operations caused by sudden traffic. This strategy significantly enhances the flexibility and adaptability of the system, provides a reliable dynamic adjustment mechanism for the inventory deduction process, and ensures efficient handling of complex inventory management scenarios.

[0081] The present invention has the following beneficial effects:

[0082] 1. In this inventory deduction method, through the hierarchical storage design of introducing the first database and the second database, the flexibility and security of inventory management are effectively improved. The first database is used to record the inventory information that has been deducted and is being deducted, while the second database is used to store the temporarily deducted inventory data. This design separates inventory data in different states, reduces the probability of data conflicts, and makes data management more efficient. By setting the time window and the distributed lock fault tolerance mechanism, the system can effectively avoid data competition problems in high-concurrency scenarios and ensure the accuracy of writing inventory information. In addition, through the setting of transactional deduction rules, even in the case of storage failure, the deduction record can be updated in time, reducing inventory deviation in abnormal situations and further enhancing the robustness and stability of the system.

[0083] 2. In this inventory deduction method, through the introduction of the temporary storage judgment strategy, the accuracy of inventory verification is effectively improved. The system ensures the legality of inventory operations by layer-by-layer querying and verifying the relationships among the total inventory quantity x of the prize, the deducted quantity a, the real-time deducted quantity b, the temporarily deducted quantity c, and the newly added deducted quantity y. Especially when a + b + c + y > x or c < a + b, the system can accurately feedback that the deduction fails, avoiding inventory over-deduction and data inconsistency problems. Through this dynamic verification mechanism, the system can not only monitor the inventory status in real time but also reduce the impact of incorrect operations on the overall system, providing higher accuracy and security guarantees for inventory management.

[0084] 3. In this inventory deduction method, through the distributed lock fault tolerance upload mechanism, the data writing success rate in high-concurrency scenarios is significantly improved. This mechanism ensures that the prize information can be written into the second database in time at the end of the time window through the cooperation of the main lock and the backup lock. Even if the acquisition of the main lock fails, the backup lock can effectively intervene as a redundant strategy to reduce the probability of writing failure. In addition, by setting a fixed waiting interval and the maximum number of attempts, the system can flexibly handle lock conflict problems in concurrent writing scenarios, optimize resource utilization, and improve the response efficiency of the system. Through this redundant mechanism and distributed design, the system can still operate efficiently under high-load conditions, ensuring the consistency and integrity of inventory data.

[0085] 4. This inventory deduction method, through a backup lock upload mechanism, further improves the distributed lock strategy and enhances the reliability of data writing. When the primary lock fails multiple times, the backup lock can take over the upload task, avoiding data writing interruptions due to lock unavailability. Even if the backup lock operation fails, the system can still provide timely feedback on the operation status to prevent the problem from spreading. By increasing the flexibility and fault tolerance of the backup lock, the system's stability in multi-threaded and high-concurrency scenarios is significantly improved, thereby reducing the risk of write failures and enhancing user experience and system efficiency.

[0086] 5. This inventory deduction method, through refined write instructions, effectively improves the accuracy and security of inventory deduction operations. During write execution, the system queries the temporary deduction quantity *c* in the second database, dynamically updates it to *d* = *c* + *y*, and compares *a* + *b* + *d* with the total inventory *x* in real time to ensure the deduction operation is legitimate. If insufficient inventory or data anomalies are detected, the system will interrupt the operation and display a deduction failure message. Through this dynamic adjustment and rigorous verification mechanism, inventory deduction operations are more secure and reliable, effectively reducing over-deductions due to erroneous operations, while simultaneously improving the stability and accuracy of database writes.

[0087] 6. This inventory deduction method, through a transactional deduction strategy, significantly improves the stability and fault tolerance of the inventory deduction process. During the process of storing prize information into the first database, the system ensures the integrity of the operation through explicit transactional rules. If the write operation fails, the system can automatically reduce the corresponding temporary deduction quantity in the second database, thereby preventing the cumulative impact of data errors on the system. Through this compensatory transaction processing mechanism, the system can effectively handle abnormal situations, ensure data consistency and operational reliability, and provide a solid guarantee for inventory management.

[0088] 7. This inventory deduction method, through a dynamic inventory migration strategy, improves database storage efficiency and query performance. Separating the storage of prize information for items whose inventory has already been deducted from and those currently being deducted reduces the impact of high-frequency queries on the main table. Simultaneously, the system monitors query traffic and smoothed average traffic to determine whether to trigger data migration operations, and dynamically adjusts the number of migration records based on the migration ratio coefficient, ensuring an efficient and stable migration process. Through this dynamic migration mechanism, database storage resources are utilized rationally, and system performance is further optimized.

[0089] 8. This inventory deduction method further optimizes the efficiency and accuracy of data migration by introducing a dynamic migration strategy based on sales rate. The system dynamically adjusts the migration frequency and interval by monitoring the sales rate and smoothing average in real time, ensuring that data migration matches sales trends. By adjusting the smoothing coefficient, the system can respond promptly to sales fluctuations, optimizing the pace and efficiency of migration. This adaptive migration strategy improves database operating efficiency while reducing resource consumption under high load scenarios, providing the system with greater flexibility and stability. Attached Figure Description

[0090] Figure 1 This is a schematic diagram of the method of the present invention.

[0091] Figure 2 This is a schematic diagram of the process of the present invention. Detailed Implementation

[0092] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0093] Example 1, refer to Figure 1 Inventory deduction methods for marketing activities that involve random sampling of prizes;

[0094] Store the prize information that has been deducted from inventory and is being deducted from inventory into the first database;

[0095] The information on prizes temporarily deducted from inventory will be stored in the second database;

[0096] By introducing a two-tiered database storage mechanism, the efficiency and reliability of inventory deduction operations are significantly improved. The first database focuses on storing information about deducted and ongoing prizes, ensuring real-time management of critical data. The second database temporarily stores deduction information, providing a flexible buffer and avoiding resource conflicts caused by direct operations on the main database. Setting a time window effectively reduces system concurrency pressure by batch processing requests while ensuring timely data processing. A distributed lock fault-tolerance mechanism further guarantees the security and accuracy of database operations, reducing lock conflicts caused by concurrency. The application of transactional deduction rules allows for rapid rollback and updating of the second database in the event of a main database storage failure, improving data consistency. Furthermore, a dynamic inventory migration strategy optimizes database load by rationally allocating storage locations, improving query efficiency and system response speed. Overall, this method balances the accuracy and efficiency of inventory management, providing reliable support for the smooth operation of marketing activities.

[0097] Set the time window ΔT;

[0098] Collect all requests for deducting inventory within each time window and form a request set;

[0099] The quantity of each type of prize in the statistical request set is recorded as the deduction quantity y for each type of prize;

[0100] For any type of prize, execute a temporary storage decision strategy to determine whether to store the prize information in the second database; if the prize information is stored in the second database:

[0101] At the end of the time window, the distributed lock fault-tolerant upload mechanism is executed to store the prize information in the second database;

[0102] Set transactional deduction rules. If the storage of prize information fails during the process of storing it into the first database, the deduction quantity of the prize in the second database will be updated.

[0103] According to the transactional deduction rules, the transactional deduction strategy is executed, and the prize information is stored in the first database;

[0104] Retrieve the number m records of prize information that are being deducted from inventory in the first database;

[0105] Set an early warning ratio coefficient α to determine whether to modify the prize information that is being deducted from inventory to the prize information that has already been deducted from inventory;

[0106] Implement a dynamic inventory migration strategy to change the storage location of prize information in the first database.

[0107] For any type of prize, execute a temporary storage decision strategy to determine whether to store the prize information in the second database, including:

[0108] Enter the query command in the prize basic information table to query the total inventory quantity x of the prizes;

[0109] If the query command returns an empty value, it will display a message indicating that the deduction failed.

[0110] If the query command returns the total inventory quantity x;

[0111] Then, enter the query command in the first database to query the number of prizes that have been deducted from the inventory, and record it as the deducted quantity a;

[0112] Enter a query command in the first database to query the number of prizes that are being deducted from the inventory, and record it as the real-time deduction quantity b;

[0113] Enter the query command in the second database to query the number of prizes temporarily deducted from the inventory, and record it as the temporary deduction quantity c;

[0114] Compare the magnitudes of a + b + c + y and x;

[0115] If a + b + c + y > x, then display that the deduction fails;

[0116] If a + b + c + y ≤ x, then determine the magnitude relationship between c and a + b:

[0117] If c < a + b, then display that the inventory in the second database is inaccurate and the deduction fails;

[0118] If c ≥ a + b, then store the prize information in the second database.

[0119] Through the temporary storage judgment strategy, the accuracy and data consistency of the inventory deduction operation are ensured. Before performing the deduction operation, the method first checks the total inventory quantity of the prizes and comprehensively accounts for the deducted, real-time deducted, and temporarily deducted data to ensure that the total deduction quantity does not exceed the inventory upper limit. This multiple verification mechanism effectively prevents the situation of inventory data being tampered with or misoperated. In addition, the strategy dynamically judges the relative relationship between the temporarily deducted quantity and the real-time deducted quantity to ensure that the data stored in the second database is accurate. If the temporarily deducted data meets the standard, the operation passes; otherwise, the deduction failure information is displayed in a timely manner to avoid the expansion of the impact of incorrect operations. Through this strict verification and validation mechanism, while the system maintains efficient operation, it significantly improves the security and reliability of the data, meeting the requirements of inventory management in complex scenarios.

[0120] Then at the end of the time window, execute the distributed lock fault-tolerant upload mechanism to store the prize information in the second database, including:

[0121] Set the main lock and the backup lock;

[0122] Obtain the process of storing the prize information in the second database;

[0123] S1. Execute the lock acquisition instruction. The process attempts to acquire the main lock for operating the second database at the end of the time window and determines whether the lock acquisition is successful:

[0124] S2. If the lock acquisition is successful, then execute the write instruction for the second database;

[0125] S3. If the lock acquisition fails, then set a fixed waiting interval;

[0126] S4. Set the maximum number of attempts, count the number of times the lock acquisition instruction is executed. If the number of times the lock acquisition instruction < the maximum number of attempts, then repeat S1 - S4 after the fixed waiting interval;

[0127] S5. If the number of times the lock acquisition instruction ≥ the maximum number of attempts, then execute the backup lock upload mechanism.

[0128] By introducing a distributed lock fault tolerance mechanism, the reliability issue of database operations in a multi-threaded environment is resolved. At the end of the time window, the system attempts to acquire the primary lock for the second database and determines subsequent operations based on the acquisition result. If the lock acquisition is successful, the write command is executed directly; if it fails, a fixed waiting interval and a maximum number of attempts are set to avoid resource deadlocks. Through this gradual retreat design, the system can flexibly handle lock contention in concurrent scenarios, significantly improving the operation success rate. Furthermore, if the primary lock fails multiple times, the system automatically switches to the backup lock upload mechanism, further ensuring the success of operations and data consistency. This distributed lock fault tolerance mechanism, through the collaborative work of the primary and backup locks, enhances the system's fault tolerance capability for unexpected situations, ensuring the stability and efficiency of inventory deduction operations.

[0129] If the number of lock acquisition commands is greater than or equal to the maximum number of attempts, then the backup lock upload mechanism is executed, including:

[0130] S6. Execute the lock acquisition instruction. At the end of the time window, the process attempts to acquire a spare lock for operating the second database and determines whether the lock acquisition was successful.

[0131] S7. If the lock is successfully acquired, execute the write command for the second database;

[0132] S8. If acquiring the lock fails, count the number of times the lock acquisition instruction is executed. If the number of lock acquisition instructions is less than the maximum number of attempts, then repeat S6-S8 after a fixed waiting interval.

[0133] S9. If the number of lock acquisition commands is greater than or equal to the maximum number of attempts, then the deduction failure will be displayed.

[0134] Building upon the backup lock mechanism, the fault-tolerance design has been further improved, significantly enhancing the success rate of database operations. After a failure to acquire the primary lock, the system immediately switches to the backup lock for write operations, repeatedly executing the lock acquisition command until success or the maximum number of attempts is reached. This design reduces the risk of operation failure due to resource conflicts in lock contention scenarios. By setting fixed retry intervals and maximum attempts, the system avoids resource waste while ensuring efficiency. Furthermore, if the backup lock also fails multiple times, the system promptly displays deduction failure information, facilitating rapid identification and handling of anomalies. This backup lock fault-tolerance mechanism significantly improves the system's robustness, providing double protection for inventory deduction operations, and is particularly suitable for high-concurrency marketing campaign scenarios.

[0135] If the lock is successfully acquired, the write instructions for the second database are executed, including:

[0136] The write command is:

[0137] Query the temporary deduction quantity c of prizes in the second database;

[0138] The `incre` command in the second database is executed to increase the temporary deduction amount `c` in the second database by the deduction amount `y`.

[0139] Query the temporary deduction amount d in the second database, where d = c + y;

[0140] Compare the magnitudes of a+b+d and x;

[0141] If a+b+d>x, then the deduction failed.

[0142] If a+b+d≦x, then the prize information has been successfully stored in the second database.

[0143] The write commands for the second database have been optimized to ensure the accuracy and consistency of inventory data operations. During write operations, the system first queries the temporary deduction quantity and dynamically updates it by increasing the deduction quantity, avoiding errors caused by manual operations. Furthermore, the system performs a secondary verification of the updated total inventory data to ensure that the total inventory after deduction does not exceed the actual inventory limit. If verification passes, the prize information is successfully written to the database; if verification fails, the system promptly displays a deduction failure message and terminates the operation. This step-by-step verification and update mechanism significantly reduces the risk of errors during data writing, while improving operational transparency and controllability, providing stable and reliable support for inventory management.

[0144] According to the transactional deduction rules, the transactional deduction strategy is executed, and the prize information is stored in the first database, including:

[0145] Details of deductions for winning prizes;

[0146] Execute the INSERT command in the first database to add the prize deduction details to the first database, and determine whether the addition was successful; if the addition fails, reduce the temporary deduction amount d of the prize in the second database by the deduction amount y.

[0147] If the addition is successful, the deducted quantity y will be added to the real-time deduction quantity of the prize in the first database.

[0148] By employing transactional deduction rules, the integrity and consistency of prize information storage operations are ensured. When writing data to the first database, the system first determines whether the operation is successful. If it fails, the temporary deduction quantity in the second database is immediately reduced to prevent data inconsistency; if successful, the deduction quantity is updated in the real-time deduction data of the first database. This design ensures successful data updates while minimizing the impact of operation failures on the system. Through a transactional rollback mechanism, the system can quickly recover to its initial state, improving its ability to handle abnormal situations. Furthermore, this rule makes the inventory deduction process safer and more reliable, particularly suitable for high-frequency operation scenarios, further enhancing the overall stability of the system.

[0149] In this embodiment, refer to Figure 2 .

[0150] First, from a business perspective, the inventory is divided into two parts: the first database, which is the business database where inventory is actually deducted, and uses a MySQL database.

[0151] The second database is Redis, which temporarily deducts inventory.

[0152] If the sum of these two inventories exceeds the total prize inventory, then the remaining inventory is considered to be 0.

[0153] The specific execution logic is as follows: before deducting inventory, first query the inventory to find the quantity of the prize that has already been deducted, then query the Redis to temporarily deduct inventory. If the difference between the sum and the total inventory of the product is greater than the quantity to be deducted, then the inventory is considered sufficient and the deduction logic begins.

[0154] First, it will use Redis's `incre` method to increase the amount deducted from the key in Redis and record the return value of `incre` (this return value represents the value of the key in Redis after the deduction, and it is atomic).

[0155] Second, compare the return value of increment with the business inventory. If the value exceeds the total inventory, the deduction will fail.

[0156] Third, based on the success of step two, the deduction quantity will be inserted into the first database to generate deduction details. This step emphasizes using the insert method instead of the update method in the conventional approach to avoid lock contention. If an error occurs in this step, exception handling will be performed globally, and the key row in Redis will be incremented (0-a) to restore the already deducted quantity. If step three executes successfully, the module will monitor the inventory deduction records generated by the insert and migrate them to the "sold" field in batches in real time. This process primarily ensures that the number of deduction records in a single table does not continuously increase, guaranteeing stable insert performance.

[0157] Implement a dynamic inventory migration strategy to change the storage location of prize information in the first database, including:

[0158] In the first database, the information on prizes that have already been deducted from the inventory is stored in the first database;

[0159] The prize information that is being deducted from the inventory is stored in a second database;

[0160] Get the record capacity p of the second database;

[0161] Get the current query traffic R0;

[0162] Obtain the smoothed average flow rate R1 to determine whether the flow conditions for data migration are met;

[0163] Determine whether to trigger the migration of records from the first database to the second database:

[0164] If m≥α×p and R0≥R1, then the records in the first database can be migrated to the second database.

[0165] Calculate the number of records q for each migration:

[0166] Where β is the migration ratio coefficient.

[0167] A dynamic inventory migration strategy optimizes the storage structure and query efficiency of the primary database. In its design, the system dynamically determines whether to trigger a migration operation based on a comparison of query traffic with the smoothed average traffic, and calculates the number of records to be migrated each time, ensuring a highly efficient migration process with minimal impact on business operations. This design migrates prize information in stages, moving deducted inventory information from frequently queried tables to secondary tables, rationally allocating storage resources and reducing query pressure. Furthermore, the introduction of a migration ratio coefficient allows the system to flexibly adjust the migration strategy according to actual business needs, providing greater adaptability for data management in complex scenarios.

[0168] Implement a dynamic inventory migration strategy to change the storage location of prize information in the first database, including:

[0169] Get the current product sales rate R2;

[0170] Obtain the smoothed average of the sales rate, R3;

[0171] Obtain the initial migration frequency f0;

[0172] The update migration frequency is

[0173] Calculate migration interval

[0174] Update the smoothed average flow rate R1 = λ1 × R1 + (1 - λ1) × R0, where λ1 is the smoothing coefficient of the smoothed average flow rate;

[0175] The smoothed average of the updated sales rate is R3 = λ3 × R3 + (1 - λ3) × R2, where λ3 is the smoothing coefficient of the smoothed average of the sales rate.

[0176] Building upon the dynamic inventory migration strategy, a calculation mechanism based on sales rate and smoothed average flow is further introduced to optimize the adjustment of migration frequency and interval. By acquiring product sales rate in real time and comparing it with the smoothed average, the system can dynamically adjust the migration strategy to adapt to the needs of different business scenarios. The optimized design of the initial migration frequency, combined with the smoothing coefficient update mechanism, makes the migration process smoother and more scientific, avoiding the waste of system resources caused by frequent migrations. This dynamic adjustment method based on real-time data significantly improves the efficiency of data storage and management, providing reliable support for inventory management in high-concurrency, high-load scenarios.

[0177] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article, or apparatus.

[0178] The above are merely preferred embodiments of the present invention. It should be noted that those skilled in the art can make various improvements and modifications without departing from the technical principles of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.

Claims

1. An inventory deduction method, characterized in that, Include: For the marketing activity of randomly checking prizes; Store the prize information of the prizes whose inventory has been deducted and is being deducted into the first database; Store the prize information of the prizes with temporarily deducted inventory into the second database; Set a time window ΔT; Within each time window, obtain all requests for deducting inventory and form a request set; Count the quantity of each type of prize in the request set, denoted as the deducted quantity y of each type of prize; For any type of prize, execute a temporary storage judgment strategy to determine whether to store the prize information in the second database; If the prize information is stored in the second database: Then at the end of the time window, execute a distributed lock fault-tolerant upload mechanism to store the prize information in the second database; Set a transactional deduction rule, where the transactional deduction rule is that during the process of storing the prize information in the first database, if the storage fails, update the deducted quantity of the prize in the second database; According to the transactional deduction rule, execute a transactional deduction strategy to store the prize information in the first database; Obtain the record quantity m of the prize information with inventory being deducted in the first database; Set an early warning ratio coefficient α, which is used to determine whether to modify the prize information with inventory being deducted to the prize information with inventory already deducted; Execute a dynamic inventory migration strategy to change the storage location of the prize information in the first database.

2. The inventory deduction method according to claim 1, characterized in that, The above-mentioned execution of the temporary storage judgment strategy for any type of prize to determine whether to store the prize information in the second database includes: Input a query instruction in the prize basic information table to query the total inventory quantity x of the prize; If the return value of the query instruction is empty, display that the deduction fails; If the return value of the query instruction is the total inventory quantity x; Then input a query instruction in the first database to query the quantity of the prizes whose inventory has been deducted, denoted as the deducted quantity a; Input a query instruction in the first database to query the quantity of the prizes with inventory being deducted, denoted as the real-time deducted quantity b; ​ ​ ​ ​ ​ ​ 3. The inventory deduction method according to claim 2, characterized in that, ​ ​ ​ ​ ​ ​ ​ ​ 4. The inventory deduction method according to claim 3, characterized in that, If the number of lock acquisition commands is greater than or equal to the maximum number of attempts, then the backup lock upload mechanism is executed, including: S6. Execute the lock acquisition instruction. At the end of the time window, the process attempts to acquire a spare lock for operating the second database and determines whether the lock acquisition was successful. S7. If the lock is successfully acquired, execute the write command for the second database; S8. If acquiring the lock fails, count the number of times the lock acquisition instruction is executed. If the number of lock acquisition instructions is less than the maximum number of attempts, then repeat S6-S8 after a fixed waiting interval. S9. If the number of lock acquisition commands is greater than or equal to the maximum number of attempts, then the deduction failure will be displayed.

5. The inventory deduction method according to claim 4, characterized in that, If the lock is successfully acquired, the write instruction for the second database is executed, including: The write instruction is: Query the temporary deduction quantity c of prizes in the second database; The `incre` command in the second database is executed to increase the temporary deduction amount `c` in the second database by the deduction amount `y`. Query the temporary deduction amount d in the second database, where d = c + y; Compare the magnitudes of a+b+d and x; If a+b+d>x, then the deduction failed. If a+b+d≦x, then the prize information has been successfully stored in the second database.

6. The inventory deduction method according to claim 5, characterized in that, The step of executing a transactional deduction strategy according to transactional deduction rules and storing prize information in the first database includes: Details of deductions for winning prizes; Execute the INSERT command in the first database to add the prize deduction details to the first database, and then determine whether the addition was successful. If the addition fails, the temporary deduction quantity d of the prize in the second database will be reduced by the deduction quantity y. If the addition is successful, the deducted quantity y will be added to the real-time deduction quantity of the prize in the first database.

7. The inventory deduction method according to claim 1, characterized in that, The execution of the dynamic inventory migration strategy, which changes the storage location of prize information in the first database, includes: In the first database, the information on prizes that have already been deducted from the inventory is stored in the first database; The prize information that is being deducted from the inventory is stored in a second database; Get the record capacity p of the second database; Get the current query traffic R0; Obtain the smoothed average flow rate R1 to determine whether the flow conditions for data migration are met; Determine whether to trigger the migration of records from the first database to the second database: If m≥α×p and R0≥R1, then the records in the first database can be migrated to the second database. Calculate the number of records q for each migration: Where β is the migration ratio coefficient.

8. The inventory deduction method according to claim 7, characterized in that, The execution of the dynamic inventory migration strategy, which changes the storage location of prize information in the first database, includes: Get the current product sales rate R2; Obtain the smoothed average of the sales rate, R3; Obtain the initial migration frequency f0; The update migration frequency is Calculate migration interval Update the smoothed average flow rate R1 = λ1 × R1 + (1 - λ1) × R0, where λ1 is the smoothing coefficient of the smoothed average flow rate; The smoothed average of the updated sales rate is R3 = λ3 × R3 + (1 - λ3) × R2, where λ3 is the smoothing coefficient of the smoothed average of the sales rate.

Citation Information

Patent Citations

  • Inventory management method and device based on cloud service and electronic device

    CN109377120A

  • Commodity inventory data processing method and device

    CN115391364A