Inventory deduction method
By adopting distributed transactions and dynamic inventory migration methods in the inventory management system, the accuracy and consistency of inventory management in high concurrency scenarios are solved, and the stability and efficiency of the system are improved.
Patent Information
- Application Number
- CN202510223911.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-27
- Publication Date
- 2025-05-30
- Estimated Expiration
- 2045-02-27
AI Technical Summary
In high concurrency scenarios, it is difficult for existing inventory management systems to effectively manage prize inventory, and it is prone to over-deducting inventory, data consistency problems and operational risks.
The inventory deduction method based on distributed transactions and dynamic inventory migration is adopted to ensure the precise management of inventory data through the hierarchical storage of the first and second databases, unified processing within the time window, and fault-tolerant mechanism of distributed locks.
In high concurrency scenarios, precise management of inventory data is achieved, system operation efficiency is improved, error rate and super deduction phenomenon is reduced, and solid technical guarantees are provided.
Smart Images

Figure CN120067123A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of inventory deduction, and specifically to an inventory deduction method. Background Art
[0002] In marketing activities, the inventory management of prizes is one of the core links for the smooth progress of the activities. With the rapid development of Internet technology, online lottery activities have become an important means to attract users and increase brand exposure. However, a key problem often faced in such activities is how to effectively manage the inventory of prizes, especially in the case of high concurrency, to prevent the instantaneous inventory deduction from exceeding the actual plan, resulting in an imbalance in prize distribution or even the failure of the activity.
[0003] Currently, most inventory management systems adopt the direct deduction method, that is, directly reducing the inventory when a user wins a prize. This method works well in low-concurrency scenarios, but has significant deficiencies in high-concurrency scenarios. When a large number of users participate in the lottery simultaneously, the problem of over-deducting the inventory may occur, resulting in insufficient prize quantities. In addition, the delay in inventory data update and data consistency issues will also lead to a mismatch between the prize distribution and the actual inventory situation, further exacerbating the operational risks.
[0004] This technical solution proposes an inventory deduction method based on distributed transactions and dynamic inventory migration. Through the hierarchical storage of the first database and the second database, unified processing within a time window, and the fault-tolerant mechanism of the distributed lock, it ensures the accurate management of inventory data in high-concurrency scenarios. At the same time, through the transactional deduction rules and dynamic migration strategies, the safety and real-time nature of inventory operations are combined, which not only improves the operating efficiency of the system but also effectively reduces the error rate and over-deduction phenomenon in the inventory management process. This provides a solid technical guarantee for the smooth development of marketing activities. Summary of the Invention
[0005] The present invention provides an inventory deduction method, which helps to solve the problems mentioned in the above background art.
[0006] In a first aspect, the present application provides an inventory deduction method, adopting the following technical solution: The inventory deduction method includes: For marketing activities that conduct random inspections of prizes; Storing the prize information of the already deducted inventory and the inventory being deducted into the first database; Storing the prize information of the temporarily deducted inventory into the second database; Setting a time window ΔT; Obtaining all requests for inventory deduction within each time window and forming a request set; Counting the quantity of each type of prize in the request set, denoted as the deduction 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; By introducing separate storage of prize information in the first database and the second database, the inventory management process is significantly optimized. This design effectively reduces database operation conflicts and improves the stability of the system in high-concurrency scenarios. At the same time, the setting of the time window centralizes batch requests, reduces the pressure of real-time computing, and ensures the operation efficiency of the system. The introduction of the transactional deduction rule further guarantees data consistency. Even in the case of storage failure, the temporary deduction information can be updated in a timely manner to prevent data chaos. The dynamic inventory migration strategy optimizes the database performance and enhances the system's ability to handle high traffic by adjusting the data storage location in real-time.
[0007] Set a transactional deduction rule, which is that during the process of storing the prize information in the first database, if the storage fails, then 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 whose inventory is being deducted in the first database; Set a warning ratio coefficient α, which is used to determine whether to modify the prize information whose inventory is being deducted to the prize information whose inventory has been deducted; Execute a dynamic inventory migration strategy to change the storage location of the prize information in the first database.
[0008] The step of, for any type of prize, executing a temporary storage judgment strategy 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, then 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 data table to query the deducted quantity of the prize whose inventory has been deducted, denoted as the deducted quantity a; Input a query instruction in the first data table to query the quantity of the prize whose inventory is being deducted, denoted as the real-time deduction quantity b; Input a query instruction in the second data table to query the quantity of the prize whose inventory is temporarily deducted, denoted as the temporary deduction quantity c; Compare the size relationship between a + b + c + y and x; If a + b + c + y > x, then display that the deduction fails; If a + b + c + y ≤ x, then determine the magnitude relationship between c and a + b: If c < a + b, it is indicated that the inventory in the second database is inaccurate and the deduction fails; If c ≥ a + b, the prize information is stored in the second database.
[0009] 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 inconsistencies in 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, while the further comparison between c and a + b avoids abnormal deduction problems caused by inaccurate temporary inventory deduction. This meticulous logical design improves the reliability of the deduction operation and provides accurate data support for subsequent transactional deductions and dynamic migrations.
[0010] Preferably, 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, including: Set a main lock and a backup lock; Obtain the process of storing the prize information in the second database; S1. Execute the lock acquisition instruction. At the end of the time window, the process attempts to acquire the main lock for operating the second database and determines whether the lock acquisition is successful: S2. If the lock acquisition is successful, execute the write instruction for the second database; S3. If the lock acquisition fails, set a fixed waiting interval; 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, repeat S1 - S4 after the fixed waiting interval; S5. If the number of times the lock acquisition instruction ≥ the maximum number of attempts, execute the backup lock upload mechanism.
[0011] The distributed lock fault-tolerant mechanism solves the problem of lock contention in a high-concurrency environment. The dual protection design of the main lock and the backup lock ensures the stable operation of the system in high-load scenarios. Setting a fixed waiting interval and the maximum number of attempts makes the lock competition more efficient and avoids the problem of long-term resource occupation. The introduction of the backup lock provides an additional fault-tolerant means after the main lock acquisition fails, improving the success rate of the operation. Through this mechanism, the system can still maintain the reliability and consistency of the deduction function in extreme cases.
[0012] Preferably, if the number of times of obtaining the lock instruction ≥ the maximum number of attempts, the backup lock upload mechanism is executed, including: S6. Execute the lock acquisition instruction, and the process attempts to obtain the backup lock for operating the second database at the end of the time window, and determine whether the lock acquisition is successful: S7. If the lock is successfully obtained, execute the write instruction for the second database; S8. If the lock acquisition fails, count the number of times of executing the lock acquisition instruction. If the number of times of the lock acquisition instruction < the maximum number of attempts, repeat S6 - S8 after a fixed waiting interval; S9. If the number of times of the lock acquisition instruction ≥ the maximum number of attempts, display that the deduction fails.
[0013] Through the backup lock mechanism, by intervening in time when the main lock acquisition fails, it provides additional security for the system. The fixed retry strategy and clear failure prompt avoid the system falling into an infinite competition dilemma. The introduction of the backup lock not only improves the success rate of lock acquisition, but also effectively shortens the response time in high-concurrency scenarios. This design enables the inventory deduction operation to maintain high reliability even when the traffic surges, and at the same time provides new ideas and practical solutions for improving the robustness of the distributed system.
[0014] Preferably, the step of, if the lock is successfully obtained, executing the write instruction for the second database, includes: The write instruction is: Query the temporary deduction quantity c of the prize in the second database; Execute the incre instruction of the second database to increase the temporary deduction quantity c in the second database by the deduction quantity y; Query the temporary deduction quantity d in the second database, where d = c + y; Compare the magnitude relationship between a + b + d and x; If a + b + d > x, display that the deduction fails; If a + b + d ≤ x, store the prize information in the second database successfully.
[0015] Through the refined data write instruction, it significantly improves the accuracy and robustness of the system's operation on the second database. This multi-level verification mechanism ensures that the inventory data always maintains consistency in high-concurrency scenarios, avoiding over-deduction or missed-deduction phenomena. Even if an exception occurs during the writing process, the clear verification conditions can terminate the deduction process in time and display that the deduction fails, thus effectively avoiding data pollution and system chaos. Through the precise write logic and verification mechanism, this method enhances the operation reliability of the second database and provides stronger security and operation guarantee for inventory management.
[0016] Preferably, the step of, according to the transactional deduction rule, executing the transactional deduction strategy and storing the prize information in the first database, includes: Obtain the deduction details of the prize; Execute the insert instruction of the first database to add the deduction details of the prize to the first database, and determine whether the addition is successful; If the addition fails, reduce the temporary deduction quantity d of the prize in the second database by the deduction quantity y; If the addition is successful, add the deduction quantity y to the real-time deduction quantity of the prize in the first database.
[0017] By introducing a transactional deduction strategy, the accuracy and consistency of data processing are significantly improved. The operations of obtaining and writing the deduction details are controlled by transactions, ensuring the atomicity of the operations. If the storage fails, the temporary deduction quantity in the second database is immediately reduced, thus avoiding the disorder of inventory data caused by abnormal operations. This design provides reliable fault tolerance in high-concurrency or abnormal situations, ensuring the accuracy of the deduction quantity in the first database. At the same time, this mechanism simplifies the tracing and correction operations of the deduction process, further improving the stability and maintenance efficiency of the system.
[0018] Preferably, the execution of the dynamic inventory migration strategy to change the storage location of the prize information in the first database includes: In the first database, store the prize information with the inventory already deducted in the first data table; Store the prize information with the inventory being deducted in the second data table; Obtain the record capacity p of the second data table; Obtain the current query traffic R 0 ; Obtain the smoothed average traffic R 1 , which is used to determine whether the traffic condition for migrating data is met; Determine whether to trigger the migration of the records in the first data table to the second data table: m≥α×p and R 0 ≥R 1 , then it meets the condition for migrating the records in the first data table to the second data table; Calculate the number of records migrated each time q: where β is the migration ratio coefficient.
[0019] Through the dynamic inventory migration strategy, the performance bottleneck problem of the database caused by the query traffic fluctuation is effectively solved. The deducted and deducting prize information is stored separately, and combined with the judgment condition of smoothing the average traffic, the hierarchical management and migration of data are realized. By calculating the number q of each migration record and dynamically adjusting the migration ratio coefficient, the data migration process becomes more flexible. This mechanism not only improves the query performance of the database under high traffic conditions, but also reduces the imbalance of data storage and improves the overall operation efficiency of the system.
[0020] Preferably, executing the dynamic inventory migration strategy and changing the storage location of the prize information in the first database includes: Obtain the current commodity sales rate R 2 ; Obtain the smoothed average value R of the sales rate 3 ; Obtain the initial migration frequency f 0 ; Update the migration frequency to Calculate the migration interval Update the smoothed average traffic R 1 =λ 1 ×R 1 +(1 - λ 1 )×R 0 , where λ 1 is the smoothing coefficient of the smoothed average traffic; Update the smoothed average value R of the sales rate 3 =λ 3 ×R 3 +(1 - λ 3 )×R 2 , where λ 3 is the smoothing coefficient of the smoothed average value of the sales rate.
[0021] Through the dynamic calculation of the sales rate and its smoothed average value, the adjustment method of the data migration frequency is optimized. The setting of the initial migration frequency combined with the real-time update of the sales rate enables the system to quickly respond to market changes. At the same time, 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 the efficient handling of complex inventory management scenarios.
[0022] The present invention has the following beneficial effects: 1. This inventory deduction method effectively improves the flexibility and security of inventory management by introducing a hierarchical storage design of the first database and the second database. 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 a time window and a distributed lock fault tolerance mechanism, the system can effectively avoid data competition problems in high-concurrency scenarios and ensure the accuracy of inventory information writing. In addition, by setting transactional deduction rules, even in the case of storage failure, the deduction records can be updated in a timely manner, reducing inventory deviation in abnormal situations and further enhancing the robustness and stability of the system.
[0023] 2. This inventory deduction method effectively improves the accuracy of inventory verification by introducing a temporary storage judgment strategy. The system ensures the legality of inventory operations by querying and verifying the relationships among the total inventory quantity x of prizes, the deducted quantity a, the real-time deducted quantity b, the temporarily deducted quantity c, and the newly added deducted quantity y layer by layer. 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.
[0024] 3. This inventory deduction method significantly improves the data writing success rate in high-concurrency scenarios through a distributed lock fault tolerance upload mechanism. This mechanism ensures that the prize information can be written into the second database in a timely manner 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.
[0025] 4. This inventory deduction method further improves the distributed lock strategy and enhances the reliability of data writing through a backup lock upload mechanism. When the main lock fails after multiple attempts, the backup lock can take over the upload task to avoid data writing interruption caused by the unavailable lock. Even if the backup lock operation fails, the system can still feedback the operation status through timely prompts to prevent the problem from spreading. By increasing the flexibility and fault tolerance of the backup lock, the stability of the system in multi-threaded and high-concurrency scenarios is significantly improved, thereby reducing the risk of writing failure and enhancing the user experience and system efficiency.
[0026] 5. The inventory deduction method effectively improves the accuracy and security of inventory deduction operations by refining the write instructions. When performing a write, the system queries the temporary deduction quantity c in the second database, dynamically updates it to d = c + y, and compares the relationship between a + b + d and the total inventory x in real time to ensure the legality of the deduction operation. If it is found that the inventory is insufficient or the data is abnormal, the system will interrupt the operation and display a deduction failure prompt. Through this dynamic adjustment and strict verification mechanism, the inventory deduction operation is more secure and reliable, effectively reducing the problem of over-deduction of inventory caused by incorrect operations, and at the same time improving the stability and accuracy of database writing.
[0027] 6. The inventory deduction method significantly improves the stability and fault tolerance of the inventory deduction process through a transactional deduction strategy. During the process of storing the prize information in the first database, the system ensures the integrity of the operation through clear transactional rules. If the write fails, the system can automatically reduce the corresponding temporary deduction quantity in the second database, thereby avoiding the impact of cumulative data errors on the system. Through this compensatory transaction processing mechanism, the system can effectively handle abnormal situations, ensure data consistency and operation reliability, and provide a solid guarantee for inventory management.
[0028] 7. The inventory deduction method improves the storage efficiency and query performance of the database through a dynamic inventory migration strategy. The prize information with deducted inventory and the inventory being deducted is stored separately, reducing the impact of high-frequency queries on the main table. At the same time, the system monitors the query traffic and the smoothed average traffic to determine whether to trigger a data migration operation, and dynamically adjusts the number of migrated records according to the migration ratio coefficient to ensure the efficiency and smoothness of the migration process. Through this dynamic migration mechanism, the database storage resources are reasonably utilized, and the system performance is further optimized.
[0029] 8. The inventory deduction method further optimizes the efficiency and accuracy of data migration by introducing a dynamic migration strategy based on the sales rate. The system dynamically adjusts the migration frequency and migration interval by real-time monitoring of the commodity sales rate and the smoothed average value to ensure that the data migration matches the sales trend. By adjusting the smoothing coefficient, the system can respond to sales fluctuations in a timely manner and optimize the rhythm and efficiency of migration. This adaptive migration strategy improves the operating efficiency of the database while reducing resource consumption in high-load scenarios, providing higher flexibility and stability for the system. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] Figure 1 It is a schematic diagram of the method of the present invention.
[0031] Figure 2 It is a schematic diagram of the process of the present invention. DETAILED DESCRIPTION OF THE INVENTION
[0032] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the protection scope of the present invention.
[0033] Embodiment 1. Refer to Figure 1 , an inventory deduction method for a marketing activity of randomly inspecting prizes; Store the prize information of the already deducted inventory and the inventory being deducted into the first database; Store the prize information of the temporarily deducted inventory into the second database; By introducing a two - layer database storage mechanism, the efficiency and reliability of inventory deduction operations are significantly improved. The first database focuses on storing the prize information of the already deducted and the inventory being deducted, ensuring real - time management of key data; the second database is used to temporarily store the deduction information, providing a flexible buffer to avoid resource conflicts caused by directly operating on the main database. The setting of the time window effectively reduces the system concurrency pressure through batch - processing requests, while ensuring the timeliness of data processing. The distributed lock fault - tolerance mechanism further guarantees the security and accuracy of database operations, reducing lock - conflict problems caused by concurrency. The application of the transactional deduction rule can quickly roll back and update the second database when the storage in the main database fails, improving data consistency. In addition, the dynamic inventory migration strategy optimizes the database load by reasonably allocating storage locations, improving query efficiency and system response speed. This method overall balances the accuracy of inventory management and operation efficiency, providing reliable support for the smooth progress of marketing activities.
[0034] Set a time window ΔT; Obtain all requests for deducting inventory within each time window to form a request set; Count the quantity of each type of prize in the request set, denoted as the deduction 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 to be stored in the second database: Then at the end of the time window, execute a distributed lock fault - tolerance upload mechanism to store the prize information in the second database; Set a transactional deduction rule. The transactional deduction rule is that during the process of storing the prize information in the first database, if the storage fails, update the deduction 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 count m of the prize information whose inventory is being deducted in the first database; Set the warning ratio coefficient α, which is used to determine whether to modify the prize information whose inventory is being deducted to the prize information whose inventory has been deducted; Execute the dynamic inventory migration strategy to change the storage location of the prize information in the first database.
[0035] For any type of prize, execute the temporary storage judgment strategy to determine whether to store the prize information in the second database, including: Enter 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 enter a query instruction in the first data table to query the quantity of the prize whose inventory has been deducted, denoted as the deducted quantity a; Enter a query instruction in the first data table to query the quantity of the prize whose inventory is being deducted, denoted as the real-time deduction quantity b; Enter a query instruction in the second data table to query the quantity of the prize whose inventory is temporarily deducted, denoted as the temporary deduction quantity c; Compare the size relationship between a + b + c + y and x; If a + b + c + y > x, display that the deduction fails; If a + b + c + y ≤ x, then judge the size relationship between c and a + b: If c < a + b, display that the inventory in the second database is inaccurate and the deduction fails; If c ≥ a + b, store the prize information in the second database.
[0036] 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 prize and comprehensively calculates 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 maintaining high efficiency, the system significantly improves the security and reliability of data, meeting the requirements of inventory management in complex scenarios.
[0037] Then at the end moment of the time window, execute the distributed lock fault-tolerant upload mechanism to store the prize information in the second database, including: Set the main lock and the backup lock; Obtain the process of storing the prize information into the second database; S1. Execute the lock acquisition instruction. At the end of the time window, the process attempts to acquire the main lock for operating on the second database, and determine whether the lock acquisition is successful: S2. If the lock acquisition is successful, execute the write instruction for the second database; S3. If the lock acquisition fails, set a fixed waiting interval; 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 is executed < the maximum number of attempts, repeat S1 - S4 after the fixed waiting interval; S5. If the number of times the lock acquisition instruction is executed ≥ the maximum number of attempts, execute the backup lock upload mechanism.
[0038] By introducing a distributed lock fault tolerance mechanism, the reliability problem of database operations in a multi-threaded environment is solved. At the end of the time window, the system attempts to acquire the main lock for operating on the second database, and decides subsequent operations according to the acquisition result. If the lock acquisition is successful, directly execute the write instruction; if it fails, set a fixed waiting interval and the maximum number of attempts to avoid resource deadlocks. Through this step-by-step concession design, the system can flexibly handle the lock contention problem in a concurrent scenario, significantly improving the operation success rate. In addition, if the main lock fails after multiple attempts, the system automatically switches to the backup lock upload mechanism to further ensure the success of the operation and data consistency. This distributed lock fault tolerance mechanism enhances the system's fault tolerance ability to unexpected situations through the cooperation of the main lock and the backup lock, ensuring the stability and efficiency of the inventory deduction operation.
[0039] If the number of times the lock acquisition instruction is executed ≥ the maximum number of attempts, execute the backup lock upload mechanism, including: S6. Execute the lock acquisition instruction. At the end of the time window, the process attempts to acquire the backup lock for operating on the second database, and determine whether the lock acquisition is successful: S7. If the lock acquisition is successful, execute the write instruction for the second database; S8. If the lock acquisition fails, count the number of times the lock acquisition instruction is executed. If the number of times the lock acquisition instruction is executed < the maximum number of attempts, repeat S6 - S8 after the fixed waiting interval; S9. If the number of times the lock acquisition instruction is executed ≥ the maximum number of attempts, display the deduction failure.
[0040] Based on the backup lock mechanism, the fault tolerance design has been further improved, significantly increasing the success rate of database operations. After the main lock attempt fails, the system immediately switches to the backup lock for writing operations and repeats the lock acquisition instruction until successful or the maximum number of attempts is reached. This design reduces the risk of operation failure caused by resource conflicts in lock contention scenarios. By setting a fixed retry interval and maximum number of attempts, the system avoids resource waste while ensuring efficiency. In addition, if the backup lock also fails after multiple attempts, the system promptly displays the deduction failure message, which helps to quickly identify and handle abnormal situations. This backup lock fault tolerance mechanism significantly enhances the robustness of the system, providing double protection for inventory deduction operations, especially applicable to high-concurrency marketing activity scenarios.
[0041] If the lock is successfully acquired, execute the write instruction for the second database, including: The write instruction is: Query the temporary deduction quantity c of the prize in the second database; Execute the incre instruction of the second database to increase the temporary deduction quantity c in the second database by the deduction quantity y; Query the temporary deduction quantity d in the second database, d = c + y; Compare the size relationship between a + b + d and x; If a + b + d > x, display deduction failure; If a + b + d ≤ x, store the prize information in the second database successfully.
[0042] The write instruction for the second database has been optimized to ensure the accuracy and consistency of inventory data operations. During the write operation, the system first queries the temporary deduction quantity and dynamically updates it by increasing the deduction quantity, avoiding errors caused by manual operations. In addition, the system performs a secondary verification on the updated total inventory data to ensure that the total inventory quantity after deduction does not exceed the actual inventory limit. If the verification passes, the prize information is successfully written into the database; if the verification fails, the system promptly displays the deduction failure message and terminates the operation. This step-by-step verification and update mechanism significantly reduces the risk of errors in data writing, while improving the transparency and controllability of operations, providing stable and reliable support for inventory management.
[0043] According to the transactional deduction rules, execute the transactional deduction strategy to store the prize information in the first database, including: Obtain the deduction details of the prize; Execute the insert instruction of the first database to add the deduction details of the prize into the first database and determine whether the addition is successful; If the addition fails, reduce the temporary deduction quantity d of the prize in the second database by the deduction quantity y; If the addition is successful, add the deducted quantity y to the real-time deducted quantity of the prize in the first database.
[0044] Through the transactional deduction rule, the integrity and consistency of the prize information storage operation are ensured. When writing data to the first database, the system first determines whether the operation is successful. If it fails, immediately reduce the temporary deducted quantity in the second database to avoid data inconsistency; if it is successful, update the deducted quantity to the real-time deducted data in the first database. This design ensures the success of data update while reducing the impact of operation failure on the system. Through the transactional rollback mechanism, the system can quickly restore to the initial state, improving the ability to handle abnormal situations. In addition, this rule makes the inventory deduction process safer and more reliable, especially suitable for high-frequency operation scenarios, further enhancing the overall stability of the system.
[0045] In this embodiment, refer to Figure 2 .
[0046] First, the inventory is divided into two parts in terms of business: the first database, where the business library actually deducts the inventory, using the mysql database; The second database is the Redis temporary deducted inventory.
[0047] If the sum of these two inventories exceeds the total inventory of the prize, it is considered that the remaining inventory is already 0.
[0048] The specific execution logic is: before deducting the inventory, first query the deducted quantity of the prize through the inventory, and then query the temporary deducted inventory in redis. If the difference between the sum and the total inventory of the commodity is greater than the current deducted quantity, it is considered that the inventory is sufficient and the deduction logic starts.
[0049] First, one, use the incre method of redis to increase the deducted quantity in redis and record the return value of incre (this return value represents the value of the key in redis after increasing the deducted quantity and has atomicity) Second, compare the return value of incre with the business inventory. If it exceeds the total inventory, the current deduction fails.
[0050] Thirdly, on the basis of the success of Step 2, the quantity of this deduction will be formed into a deduction detail in the first database by using the insert method. In this step, the insert method is mainly used to replace the update in the conventional solution, avoiding the situation of lock competition. If an error occurs in the execution of this step, an exception will be captured globally, and the key row in redis will be incremented (0 - a) to restore the quantity that has been deducted in redis. If Step 3 is successfully executed, the inventory deduction records generated by the insert in the module will be monitored, and the deduction records will be migrated to the sold field of the commodity in batches in real time. This process mainly ensures that the quantity of deduction records in a single table will not continue to increase, ensuring the stable performance of the insert.
[0051] Execute the dynamic inventory migration strategy to change the storage location of the prize information in the first database, including: In the first database, store the prize information with the inventory already deducted in the first data table; Store the prize information with the inventory being deducted in the second data table; Obtain the record capacity p of the second data table; Obtain the current query traffic R 0 ; Obtain the smoothed average traffic R 1 , which is used to determine whether the traffic condition for migrating data is met; Judge whether to trigger the migration of the records in the first data table to the second data table: m ≥ α × p and R 0 ≥ R 1 , then it meets the condition for migrating the records in the first data table to the second data table; Calculate the quantity q of each migration record: where β is the migration ratio coefficient.
[0052] Through the dynamic inventory migration strategy, the storage structure and query efficiency of the first database are optimized. In the design, the system dynamically judges whether to trigger the migration operation according to the comparison between the query traffic and the smoothed average traffic, and calculates the quantity of each migration record, ensuring that the migration process is efficient and has the least impact on the business operation. This design migrates the inventory information that has been deducted from the table with high-frequency queries to the secondary table by staging the migration of the prize information, reasonably allocating storage resources and reducing the query pressure. At the same time, the introduction of the migration ratio coefficient enables the system to flexibly adjust the migration strategy according to the actual business needs, providing higher adaptability for data management in complex scenarios.
[0053] Execute the dynamic inventory migration strategy to change the storage location of the prize information in the first database, including: Obtain the current commodity sales rate R2 ; Obtain the smoothed average value R of the sales rate 3 ; Obtain the initial migration frequency f 0 ; Update the migration frequency to Calculate the migration interval Update the smoothed average flow rate R 1 = λ 1 ×R 1 +(1 - λ 1 )×R 0 , where λ 1 is the smoothing coefficient of the smoothed average flow rate; Update the smoothed average value R of the sales rate 3 = λ 3 ×R 3 +(1 - λ 3 )×R 2 , where λ 3 is the smoothing coefficient of the smoothed average value of the sales rate.
[0054] On the basis of the dynamic inventory migration strategy, a calculation mechanism for the sales rate and the smoothed average flow rate is further introduced, optimizing the adjustment of the migration frequency and interval. By obtaining the commodity sales rate in real time and comparing it with the smoothed average value, the system can dynamically adjust the migration strategy to meet the requirements 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 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 and high-load scenarios.
[0055] It should be noted that in this article, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the term "comprising", "including" or any other variant thereof is intended to cover non-exclusive inclusion, so that a process, method, article or device comprising a series of elements not only includes those elements, but also includes other elements not expressly listed, or further includes elements inherent to such process, method, article or device.
[0056] The above are only the preferred embodiments of the present invention. It should be pointed out that for those of ordinary skill in the art, without departing from the technical principle of the present invention, several improvements and refinements can be made, and these improvements and refinements should also be regarded as the protection scope of the present invention.
Claims
1. Inventory deduction method, characterized in that: Including: For the marketing activity of randomly inspecting 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, then 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 step of, for any type of prize, executing a temporary storage judgment strategy 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, then 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 data table to query the quantity of the prize with inventory already deducted, denoted as the deducted quantity a; Input a query instruction in the first data table to query the quantity of the prize with inventory being deducted, denoted as the real-time deducted quantity b; Input a query instruction in the second data table to query the quantity of the prize with temporarily deducted inventory, denoted as the temporarily deducted quantity c; Compare the size relationship between a + b + c + y and x; If a + b + c + y > x, then display that the deduction fails; If a + b + c + y ≤ x, then judge the size relationship between c and a + b: If c < a + b, then display that the inventory in the second database is inaccurate and the deduction fails; If c ≥ a + b, then store the prize information in the second database.
3. The inventory deduction method according to claim 1, characterized in that: The step of then, at the end of the time window, executing a distributed lock fault-tolerant upload mechanism to store the prize information in the second database includes: Set a main lock and a backup lock; Obtain the process of storing the prize information in the second database; S1. Execute a lock acquisition instruction, and 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: S2. If the lock acquisition is successful, then execute the write instruction for the second database; S3. If the lock acquisition fails, then set a fixed waiting interval; S4. Set a 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; S5. If the number of times the lock acquisition instruction ≥ the maximum number of attempts, then execute the backup lock upload mechanism.
4. The inventory deduction method according to claim 3, characterized in that: If the number of times the lock instruction is obtained is greater than or equal to the maximum number of attempts, the backup lock upload mechanism is executed, including: S6. Execute the lock acquisition instruction. The process attempts to acquire the backup lock for the second database at the end of the time window and determines whether the lock acquisition is successful: S7. If the lock is acquired successfully, the write instruction of the second database is executed; S8, if the lock acquisition fails, count the number of executions of the lock acquisition instruction, if the number of times the lock acquisition instruction is executed is less than the maximum number of attempts, then repeat S6-S8 after a fixed waiting interval; S9. If the number of times the lock command is obtained is greater than or equal to the maximum number of attempts, the deduction failure is displayed.
5. The inventory deduction method according to claim 4, characterized in that: If the lock is acquired successfully, the write instruction of the second database is executed, including: The write instruction is: Query the temporary deduction quantity c of the prize in the second database; Execute the incre instruction of the second database to increase the temporary deduction amount c in the second database by the deduction amount y; Query the temporary deduction quantity d in the second database, d=c+y; Compare the size relationship between a+b+d and x; If a+b+d>x, the deduction fails; If a+b+d≦x, the prize information is successfully stored in the second database.
6. The inventory deduction method according to claim 1, characterized in that: The step of executing the transactional deduction strategy according to the transactional deduction rule and storing the prize information in the first database includes: Get the deduction details for the prize; Execute the insert command of the first database to add the deduction details of the prize to the first database, and determine whether the addition is successful; If the addition fails, the temporary deduction amount d of the prize in the second database is reduced by the deduction amount y; If the addition is successful, the deduction amount y is added to the real-time deduction amount of the prize in the first database.
7. The inventory deduction method according to claim 1, characterized in that: The executing of the dynamic inventory migration strategy to change the storage location of the prize information in the first database includes: In the first database, the prize information whose inventory has been deducted is stored in the first data table; storing the prize information of the inventory being deducted in the second data table; Obtain the record capacity p of the second data table; Get the current query flow R0; Obtain the smoothed average flow R1 to determine whether the flow conditions for migrating data are met; Determine whether to trigger the migration of records in the first data table to the second data table: m≥α×p and R0≥R1, then the records in the first data table are migrated to the second data table; Calculate the number of migration records per time q: Among them, β is the migration ratio coefficient.
8. The inventory deduction method according to claim 7, characterized in that: The executing of the dynamic inventory migration strategy to change the storage location of the prize information in the first database includes: Get the current commodity sales rate R2; Get the smoothed average R3 of the sales rate; Get the initial migration frequency f0; Update migration frequency is Calculating migration interval Update the smoothed average flow R1 = λ1 × R1 + (1-λ1) × R0, where λ1 is the smoothing coefficient of the smoothed average flow; Update the smoothed average value of the sales rate R3 = λ3×R3+(1-λ3)×R2, where λ3 is the smoothing coefficient of the smoothed average value of the sales rate.
Citation Information
Patent Citations
Inventory management method and device based on cloud service and electronic device
CN109377120A
Method for realizing distributed lock based on database
CN112241400A
Data processing method and device, electronic equipment and storage medium
CN115131067A
Commodity inventory data processing method and device
CN115391364A
Data hierarchical storage and query processing method and device, equipment and medium
CN119415568A