Online clothing store inventory management method
By using Redis for inventory management in SaaS applications in clothing stores, overselling and selling problems in inventory updates are solved, performance and scalability are improved, and inventory accuracy and consistency are ensured. sex.
Patent Information
- Application Number
- CN202411134589.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-19
- Publication Date
- 2024-12-03
AI Technical Summary
In the SaaS application of clothing stores, when orders contain multiple products, inventory updates have problems such as oversold or less selling, inventory operation logic is coupled with order logic, poor reusability and scalability, and inventory lock operation uses coarse-grained distribution Type lock affects performance.
Provide a unified inventory modification interface, use Redis to perform incremental/decreasing atomic operations, realize lock-locking inventories, ensure the atomicity and consistency of inventory operations through distributed locking and idempotent control, and improve performance.
It solves the oversold and selling problems caused by the chaos in inventory modification, improves the performance and scalability of inventory operations, and ensures inventory accuracy and consistency.
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of inventory management, and more specifically, relates to an online clothing store inventory management method. Background Art
[0002] In the SaaS (Software-as-a-Service) application of clothing store inventory management, order service and inventory service are essential functions. When an order contains a large number of products (for example, hundreds of products), there is often concurrency. In this case, when adding or subtracting inventory for order products, the current conventional inventory update method has at least the following problems:
[0003] (1) Each business scenario directly operates inventory, and the accuracy of inventory cannot be controlled, which can easily lead to overselling or underselling problems;
[0004] (2) The inventory operation logic is completely coupled with the order placement logic, resulting in poor reusability and scalability;
[0005] (3) The lock inventory operation uses a coarse-grained distributed lock, which affects the performance of the order interface. Summary of the invention
[0006] Aiming at the problems that the accuracy of existing inventory is uncontrollable, the inventory operation logic is completely coupled with the order logic, and the inventory lock operation adopts a coarse-grained distributed lock, the present invention provides an online clothing store inventory management method. First, a unified inventory modification interface is provided to solve the problem of chaotic inventory modification in various business scenarios; then, during inventory management, the inventory is directly increased or decreased through the Redis increment / decrement atomic operation, and the inventory lock is realized without lock, so as to improve performance and solve the overselling and underselling problems.
[0007] To solve the above problems, the present invention adopts the following technical solutions.
[0008] The online clothing store inventory management method includes the following steps:
[0009] Set and store the initial inventory in the Redis cache;
[0010] Get the available memory for products stored in the Redis cache;
[0011] Determine whether the available memory for the product returned by the Redis cache exists;
[0012] If it does not exist, add a distributed lock first and set the timeout release time; then load the inventory from the DB, store the loaded inventory in the Redis cache, and re-obtain the inventory amount on the updated Redis cache;
[0013] Release the distributed lock;
[0014] Detect whether the current inventory operation is to lock the inventory or release the memory;
[0015] If the memory is locked, the method further includes:
[0016] Determine whether there is sufficient available memory on the Redis cache;
[0017] If the answer is no, the system will directly return that the inventory is insufficient. If the answer is yes, the system will first add the order ID to Redis for idempotent control.
[0018] Then deduct the current inventory and update the inventory stored in the Redis cache;
[0019] Then obtain the available memory of the products stored in the Redis cache. If the available memory is greater than 0, the inventory is locked successfully.
[0020] If the available memory is less than 0, the inventory stored in the Redis cache is restored; at this time, the available memory of the product stored in the Redis cache is obtained again. If the available memory is greater than 0, it is determined that the inventory is locked successfully; if the available memory is less than 0, the inventory is locked unsuccessfully;
[0021] Under the condition that all lock inventories are successfully locked, send the lock inventory MQ asynchronously;
[0022] If there is a failed inventory lock operation, the locked inventory is rolled back asynchronously;
[0023] If the purpose is to release inventory, the method further includes:
[0024] Get the Redis locked inventory order ID cache;
[0025] Check whether the lock inventory order ID cache exists; if not, return release failure directly; otherwise:
[0026] Determine whether the locked inventory quantity is consistent with the released inventory quantity; if not, return release failure; if consistent:
[0027] Add order ID to Redis for idempotence control;
[0028] Increase and return the available inventory on Redis;
[0029] Determine whether the release of inventory is successful or failed based on the returned available memory;
[0030] Under the condition that all inventory releases are successful, send the inventory release MQ asynchronously;
[0031] If there is a failed inventory release operation, the released inventory is rolled back asynchronously;
[0032] Preferably, in the scenario where the inventory operation is to lock the inventory, each time the locked inventory MQ is consumed, the locked inventory flow is first added, and then the inventory in the DB is updated; in the scenario where the inventory operation is to release the inventory, each time the locked inventory MQ is consumed, the released inventory flow is first added, and then the inventory in the DB is updated.
[0033] Preferably, a String data structure is used to store the order ID cache of the product.
[0034] Preferably, the value of the order ID needs to be set each time the order ID cache of a product is stored; wherein:
[0035] When the value of the order ID is equal to 0, the value of the order ID represents the initial state of the inventory operation;
[0036] When the value of the order ID is greater than 0, the value of the order ID represents the inventory quantity corresponding to the inventory operation.
[0037] Compared with the prior art, the present invention has the following beneficial effects:
[0038] The online clothing store inventory management method of the present invention first provides a unified inventory modification interface to solve the problem of chaotic inventory modification in various business scenarios; then, during inventory management, the inventory is directly increased or decreased through the Redis increment / decrement atomic operation to realize lock-free inventory, thereby achieving the purpose of improving performance and solving the overselling and underselling problems. DETAILED DESCRIPTION
[0039] The following specific embodiments illustrate the implementation of the present application, and those skilled in the art can easily understand other advantages and effects of the present application from the contents disclosed in this specification. Although the description of the present application will be introduced in conjunction with some embodiments, this does not mean that the features of this application are limited to the implementation. On the contrary, the purpose of introducing the application in conjunction with the implementation is to cover other options or modifications that may be extended based on the claims of the present application. In order to provide a deep understanding of the present application, the following description will include many specific details. The present application can also be implemented without using these details. In addition, in order to avoid confusion or blurring the focus of the present application, some specific details will be omitted in the description. It should be noted that the embodiments in the present application and the features in the embodiments can be combined with each other without conflict.
[0041] The terms “first” and “second” are used for descriptive purposes only and should not be understood as indicating or implying relative importance.
[0042] In the description of this application, it should be noted that, unless otherwise clearly specified and limited, the terms "installed", "connected", and "connected" should be understood in a broad sense, for example, it can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection, or it can be indirectly connected through an intermediate medium, or it can be the internal communication of two components. For ordinary technicians in this field, the specific meanings of the above terms in this application can be understood according to specific circumstances.
[0043] In the description of the present application, it should be understood that "electrical connection" in the present application can be understood as physical contact and electrical conduction between components; it can also be understood as a form in which different components in a circuit structure are connected through physical lines such as printed circuit board (PCB) copper foil or wires that can transmit electrical signals.
[0044] In SaaS applications for inventory management scenarios, order services and inventory services are essential functions. When an order contains a large number of products, there is often concurrency. Various business scenarios directly operate inventory, and the accuracy of inventory is uncontrollable, which can easily lead to overselling or underselling.
[0045] The existing inventory operation logic is fully coupled with the order logic, with poor reusability and scalability.
[0046] Based on this, in order to reduce the pressure of distributed lock services and improve the order processing efficiency, the present invention provides an online clothing store inventory management method.
[0047] The online clothing store inventory management method of the present invention is applied to SaaS application services, and the SaaS application services include order services and inventory services. The online clothing store inventory management method includes:
[0048] Step S1, setting and storing the initial inventory in the Redis cache; obtaining the commodity available memory stored in the Redis cache; determining whether the commodity available memory returned by the Redis cache exists;
[0049] Step S2: If it does not exist, add a distributed lock first and set the timeout release time; then load the inventory from the DB, store the loaded inventory in the Redis cache, and re-obtain the inventory on the updated Redis cache;
[0050] Step S3: Release the distributed lock.
[0051] Step S4: Detect whether the current inventory operation is to lock inventory or release memory. Specifically, a separate inventory operation list may be set, and two inventory operation parameters are set in the inventory operation list, namely, to lock inventory and to release inventory.
[0052] If the memory is locked, the method further includes:
[0053] Step S5: Determine whether the available memory in the Redis cache is sufficient;
[0054] Step S6: If the answer is no, directly return that the inventory is insufficient; if the answer is yes, first add the order ID to Redis for idempotent control;
[0055] Step S7: deduct the current inventory and update the inventory stored in the Redis cache;
[0056] Step S8: Get the available memory of the commodity stored in the Redis cache. If the available memory is greater than 0, the inventory is locked successfully.
[0057] If the available memory is less than 0, the inventory stored in the Redis cache is restored; at this time, the available memory of the product stored in the Redis cache is obtained again. If the available memory is greater than 0, it is determined that the inventory is locked successfully; if the available memory is less than 0, the inventory is locked unsuccessfully;
[0058] Step S9: Under the condition that all lock stocks are successfully locked, send the lock stock MQ asynchronously;
[0059] If there is a failed inventory lock operation, the locked inventory is rolled back asynchronously.
[0060] It is worth noting that in step S7, if idempotency control is not performed but an expiration time is set, it can only prevent overselling due to repeated requests to a certain extent. This application adopts idempotency control to ensure the consistency of the operation results.
[0061] It is worth noting that in step S9, asynchronously rolling back the locked inventory can reduce the RT of the inventory lock operation and improve performance. Because the inventory in the MQ synchronization DB is sent only when all are successful, there is no need to roll back the inventory in the DB when it fails.
[0062] If the purpose is to release inventory, the method further includes:
[0063] Step S10, obtaining the Redis locked inventory order ID cache;
[0064] Step S11, check whether the lock inventory order ID cache exists; if not, directly return release failure; otherwise:
[0065] Step S12: determine whether the locked inventory quantity and the released inventory quantity are consistent; if not, return a release failure; if consistent:
[0066] Step S13: Add the order ID to Redis for idempotence control;
[0067] Step S14: Increase and return the available inventory on Redis;
[0068] Step S15: judging whether the release of inventory is successful or failed according to the returned available memory; Step S16: sending the release inventory MQ asynchronously if all the release of inventory is successful;
[0069] If there is a failed inventory release operation, the released inventory is rolled back asynchronously.
[0070] In the above process, for repeated requests to release inventory, if the inventory has been successfully released, success is directly returned instead of failure to facilitate subsequent logical processing.
[0071] In the above inventory lock and release process, the inventory is increased or decreased directly through the atomic operation of Redis increment / decrement. Each batch inventory lock operation is either all successful or all failed; and the lock inventory MQ can only be sent asynchronously when the batch operation is successful.
[0072] In the scenario where the inventory operation is to lock the inventory, each time the locked inventory MQ is consumed, the locked inventory flow is first added, and then the inventory in the DB is updated; in the scenario where the inventory operation is to release the inventory, each time the locked inventory MQ is consumed, the released inventory flow is first added, and then the inventory in the DB is updated.
[0073] In a preferred implementation, a String data structure is used to store the order ID cache of the product to avoid the problem of hot data in the hash data structure in a flash sale scenario.
[0074] In a preferred implementation, the value of the order ID needs to be set each time the order ID cache of a product is stored; wherein:
[0075] When the value of the order ID is equal to 0, the value of the order ID represents the initial state of the inventory operation;
[0076] When the value of the order ID is greater than 0, the value of the order ID is represented as the inventory quantity corresponding to the inventory operation. A value greater than 0 can be used to determine whether the released inventory quantity is equal to the locked inventory quantity of the lock inventory operation corresponding to the order ID when releasing inventory. By cleverly designing the value, it is possible to avoid directly operating the database when releasing inventory, thereby reducing performance.
[0077] The examples described in the present invention are merely descriptions of the preferred implementation modes of the present invention, and are not intended to limit the concept and scope of the present invention. Without departing from the design concept of the present invention, various modifications and improvements made to the technical solutions of the present invention by engineers and technicians in this field should all fall within the protection scope of the present invention.
Claims
1. An online clothing store inventory management method, characterized by: The following steps are involved: Set and store the initial inventory in the Redis cache; Get the available memory for products stored in the Redis cache; Determine whether the available memory for the product returned by the Redis cache exists; If it does not exist, add a distributed lock first and set the timeout release time; Then load the inventory from the DB, store the loaded inventory in the Redis cache, and re-obtain the updated inventory on the Redis cache; Release the distributed lock; Detect whether the current inventory operation is to lock the inventory or release the memory; If the memory is locked, the method further includes: Determine whether there is sufficient available memory on the Redis cache; If the answer is no, the system will directly return insufficient inventory. If the answer is yes, the system will first add the order ID to Redis for idempotent control. Then deduct the current inventory and update the inventory stored in the Redis cache; Then obtain the available memory of the products stored in the Redis cache. If the available memory is greater than 0, the inventory is locked successfully. If the available memory is less than 0, the inventory stored in the Redis cache is restored; at this time, the available memory of the product stored in the Redis cache is obtained again. If the available memory is greater than 0, it is determined that the inventory is locked successfully; if the available memory is less than 0, the inventory is locked unsuccessfully; Under the condition that all lock inventories are successfully locked, send the lock inventory MQ asynchronously; If there is a failed inventory lock operation, the locked inventory is asynchronously rolled back; If the purpose is to release inventory, the method further includes: Get the Redis locked inventory order ID cache; Check whether the lock inventory order ID cache exists; if not, return release failure directly; otherwise: Determine whether the locked inventory quantity is consistent with the released inventory quantity; if not, return release failure; if consistent: Add order ID to Redis for idempotence control; Increase and return the available inventory on Redis; Determine whether the release of inventory is successful or failed based on the returned available memory; Under the condition that all inventory releases are successful, send the inventory release MQ asynchronously; If there is a failed inventory release operation, the released inventory is rolled back asynchronously.
2. The online clothing store inventory management method according to claim 1, characterized in that: In the scenario where the inventory operation is to lock inventory, each time the locked inventory MQ is consumed, a new locked inventory flow is first added, and then the inventory in the DB is updated; In the scenario where the inventory operation is to release inventory, each time the locked inventory MQ is consumed, a new inventory release flow is first added, and then the inventory in the DB is updated.
3. The online clothing store inventory management method according to claim 1, characterized in that: Use String data structure to store the order ID cache of the product.
4. The online clothing store inventory management method according to claim 1 or 2, characterized in that: Each time you store a product's order ID cache, you need to set the value of the order ID; where: When the value of the order ID is equal to 0, the value of the order ID represents the initial state of the inventory operation; When the value of the order ID is greater than 0, the value of the order ID represents the inventory quantity corresponding to the inventory operation.
