CN111177233A - CAS inventory control method - Google Patents
CAS inventory control method Download PDFInfo
- Publication number
- CN111177233A CN111177233A CN201811332849.3A CN201811332849A CN111177233A CN 111177233 A CN111177233 A CN 111177233A CN 201811332849 A CN201811332849 A CN 201811332849A CN 111177233 A CN111177233 A CN 111177233A
- Authority
- CN
- China
- Prior art keywords
- inventory
- deduction
- cas
- memory
- control method
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06Q—INFORMATION AND COMMUNICATION TECHNOLOGY [ICT] SPECIALLY ADAPTED FOR ADMINISTRATIVE, COMMERCIAL, FINANCIAL, MANAGERIAL OR SUPERVISORY PURPOSES; SYSTEMS OR METHODS SPECIALLY ADAPTED FOR ADMINISTRATIVE, COMMERCIAL, FINANCIAL, MANAGERIAL OR SUPERVISORY PURPOSES, NOT OTHERWISE PROVIDED FOR
- G06Q10/00—Administration; Management
- G06Q10/08—Logistics, e.g. warehousing, loading or distribution; Inventory or stock management
- G06Q10/087—Inventory or stock management, e.g. order filling, procurement or balancing against orders
Landscapes
- Business, Economics & Management (AREA)
- Economics (AREA)
- Engineering & Computer Science (AREA)
- Marketing (AREA)
- Quality & Reliability (AREA)
- Finance (AREA)
- Entrepreneurship & Innovation (AREA)
- Human Resources & Organizations (AREA)
- Accounting & Taxation (AREA)
- Operations Research (AREA)
- Development Economics (AREA)
- Strategic Management (AREA)
- Tourism & Hospitality (AREA)
- Physics & Mathematics (AREA)
- General Business, Economics & Management (AREA)
- General Physics & Mathematics (AREA)
- Theoretical Computer Science (AREA)
- Management, Administration, Business Operations System, And Electronic Commerce (AREA)
Abstract
The invention discloses a CAS inventory control method, which belongs to the technical field of CAS inventory control, and adopts a CAS lock-free algorithm to realize inventory deduction by atomic operation in an internal memory; adopting Mysql as the persistence of inventory and adopting golang as a development language; when the inventory service is started, loading all inventory from a database into a memory, when the inventory is deducted, directly deducting the inventory in the memory, and after the deduction is successful, issuing an inventory deduction event; the program asynchronously fetches inventory deduction events from the message queue, asynchronously writing data to the database. The invention finishes the stock deduction operation in the memory, has high efficiency, can well meet the stock deduction in a high concurrency scene, can keep the stock deduction to be written back to the database in a complete asynchronous mode by utilizing the message subscription, and can still ensure the accuracy of the stock after the restart under the condition that the service fails.
Description
Technical Field
The invention relates to an inventory control method, in particular to a CAS inventory control method, and belongs to the technical field of CAS inventory control.
Background
At present, under the scenes of large flow and high concurrency, an e-commerce system faces many challenges, the biggest challenge is how to ensure that stocks are not over sold, and at present, the main practice in the industry is to utilize a locking mechanism, including a database lock, a file lock and the like, which is simple to implement, but has high cost, and can cause serious performance degradation of the system, and under the condition of large promotion, avalanche of the whole system can be easily caused.
Disclosure of Invention
The invention mainly aims to provide a CAS inventory control method, which adopts a CAS lock-free algorithm to realize inventory deduction by using atomic operation in an internal memory.
The purpose of the invention can be achieved by adopting the following technical scheme:
a CAS inventory control method, comprising the steps of:
step 1: adopting CAS lock-free algorithm, and realizing stock deduction by atomic operation in the memory;
step 2: adopting Mysql as the persistence of inventory and adopting golang as a development language;
and step 3: when the inventory service is started, loading all inventory from a database into a memory, when the inventory is deducted, directly deducting the inventory in the memory, and after the deduction is successful, issuing an inventory deduction event;
and 4, step 4: the program asynchronously fetches inventory deduction events from the message queue, asynchronously writing data to the database.
Further, in step 3, the inventory deduction event includes: an inventory unique identifier, an inventory deduction amount, and an inventory deduction source.
Further, in step 1, the CAS no-lock algorithm includes a CAS inventory deduction.
Further, the CAS inventory deduction comprises the following process:
step 11: the client requests to deduct the stock;
step 12: the inventory service deducts the inventory in the memory through a cpu primitive instruction;
step 13: notifying the deduction success;
step 14: the inventory service issues an inventory deduction event;
step 15: the inventory service subscribes to inventory events;
step 16: and acquiring a subscription message, and writing the stock back to the database.
Further, the core algorithm for CAS inventory deduction comprises the following steps:
step 21: inputting a numerical value C to be modified;
step 22: acquiring inventory data N of a memory to be changed;
step 23: calculating a changed value A, wherein A is N + C, and recording a value O before changing is N;
step 24: inventory deduction, if A is less than 0, returning deduction failure;
otherwise, CAS (Compare and Swap) calculates (N, O, A) the lock-free algorithm, returns A if the method returns true, the inventory deduction is successful, otherwise, it returns to step 22 for recalculation.
Further, the value C may be a positive or negative number.
The invention has the beneficial technical effects that:
1. the CAS inventory control method provided by the invention completes inventory deduction operation in the memory by utilizing the atomic instruction of the CPU, has extremely high efficiency, and can well meet inventory deduction in a high-concurrency scene.
2. The CAS inventory control method provided by the invention can keep all inventory deductions asynchronously written back to the database by using message subscription, and can still ensure the accuracy of inventory after restarting under the condition that service fails.
3. The CAS inventory control method provided by the invention has extremely high performance, so that the patent can save a great deal of IT cost for enterprises on the basis of meeting the stability in large enterprises.
Drawings
FIG. 1 is a CAS inventory deduction timing diagram of a preferred embodiment of a CAS inventory control method in accordance with the present invention;
FIG. 2 is a timing diagram illustrating the order placement for a secondary confirmation in accordance with a preferred embodiment of the CAS inventory control method of the present invention.
Detailed Description
In order to make the technical solutions of the present invention more clear and definite for those skilled in the art, the present invention is further described in detail below with reference to the examples and the accompanying drawings, but the embodiments of the present invention are not limited thereto.
As shown in fig. 1 and fig. 2, the CAS inventory control method provided in this embodiment includes the following steps:
step 1: adopting CAS lock-free algorithm, and realizing stock deduction by atomic operation in the memory;
step 2: adopting Mysql as the persistence of inventory and adopting golang as a development language;
and step 3: when the inventory service is started, loading all inventory from the database into the memory, when the inventory is deducted, directly deducting the inventory in the memory, and after the deduction is successful, issuing an inventory deduction event, wherein the inventory deduction event comprises: the inventory is only marked, the inventory deduction quantity and the inventory deduction source are obtained;
and 4, step 4: the program asynchronously fetches inventory deduction events from the message queue, asynchronously writing data to the database.
In the present embodiment, as shown in fig. 2, the CAS no-lock algorithm includes a CAS inventory deduction, which includes the following processes: the client requests to deduct the stock; the inventory service deducts the inventory in the memory through a cpu primitive instruction; notifying the deduction success; the inventory service issues an inventory deduction event; the inventory service subscribes to inventory events; and acquiring a subscription message, and writing the stock back to the database.
In this embodiment, as shown in fig. 2, the core algorithm of CAS inventory deduction includes the following steps: inputting a numerical value C to be modified, wherein the numerical value C can be a positive number or a negative number; acquiring inventory data N of a memory to be changed; calculating a changed value A, wherein A is N + C, and recording a value O before changing is N; inventory deduction, if A is less than 0, returning deduction failure; otherwise, the CAS (Compare and Swap) lock-free algorithm computes the atomic. Compare and Swap Int64(& N, O, A), if the method returns true, the inventory deduction is successful, return to A, otherwise, go back to step 22 for recalculation.
In summary, in the CAS inventory control method provided in this embodiment, the atomic instruction of the CPU is used to complete the inventory deduction operation in the memory, so that the efficiency is very high, and the inventory deduction in the high concurrency scenario can be well satisfied.
In this embodiment, the CAS inventory control method provided in this embodiment may keep all inventory deductions asynchronously written back to the database by using the message subscription, and may still ensure the accuracy of the inventory after restarting under the condition that the service fails.
In this embodiment, the CAS inventory control method provided by this embodiment has extremely high performance, so that this patent can save a large amount of IT costs for an enterprise on the basis of satisfying stability in a large enterprise.
The above description is only for the purpose of illustrating the present invention and is not intended to limit the scope of the present invention, and any person skilled in the art can substitute or change the technical solution of the present invention and its conception within the scope of the present invention.
Claims (6)
1. A CAS inventory control method, comprising the steps of:
step 1: adopting CAS lock-free algorithm, and realizing stock deduction by atomic operation in the memory;
step 2: adopting Mysql as the persistence of inventory and adopting golang as a development language;
and step 3: when the inventory service is started, loading all inventory from a database into a memory, when the inventory is deducted, directly deducting the inventory in the memory, and after the deduction is successful, issuing an inventory deduction event;
and 4, step 4: the program asynchronously fetches inventory deduction events from the message queue, asynchronously writing data to the database.
2. The CAS inventory control method as recited in claim 1, wherein the inventory deduction event in step 3 comprises: an inventory unique identifier, an inventory deduction amount, and an inventory deduction source.
3. The CAS inventory control method as recited in claim 1, wherein in step 1, the CAS no-lock algorithm comprises a CAS inventory deduction.
4. The CAS inventory control method as recited in claim 3, wherein the CAS inventory reduction comprises the following process:
step 11: the client requests to deduct the stock;
step 12: the inventory service deducts the inventory in the memory through a cpu primitive instruction;
step 13: notifying the deduction success;
step 14: the inventory service issues an inventory deduction event;
step 15: the inventory service subscribes to inventory events;
step 16: and acquiring a subscription message, and writing the stock back to the database.
5. The CAS inventory control method as recited in claim 3, wherein the core CAS inventory deduction algorithm comprises the steps of:
step 21: inputting a numerical value C to be modified;
step 22: acquiring inventory data N of a memory to be changed;
step 23: calculating a changed value A, wherein A is N + C, and recording a value O before changing is N;
step 24: inventory deduction, if A is less than 0, returning deduction failure;
otherwise, CAS (Compare and Swap) calculates (N, O, A) the lock-free algorithm, returns A if the method returns true, the inventory deduction is successful, otherwise, it returns to step 22 for recalculation.
6. The CAS inventory control method of claim 5, wherein the value C is either a positive or negative number.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201811332849.3A CN111177233A (en) | 2018-11-09 | 2018-11-09 | CAS inventory control method |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201811332849.3A CN111177233A (en) | 2018-11-09 | 2018-11-09 | CAS inventory control method |
Publications (1)
Publication Number | Publication Date |
---|---|
CN111177233A true CN111177233A (en) | 2020-05-19 |
Family
ID=70647921
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201811332849.3A Pending CN111177233A (en) | 2018-11-09 | 2018-11-09 | CAS inventory control method |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN111177233A (en) |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN113077220A (en) * | 2021-04-16 | 2021-07-06 | 北京京东拓先科技有限公司 | User request processing method and device |
Citations (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN101685406A (en) * | 2008-09-27 | 2010-03-31 | 国际商业机器公司 | Method and system for operating instance of data structure |
CN104077113A (en) * | 2014-07-10 | 2014-10-01 | 中船重工(武汉)凌久电子有限责任公司 | Method for achieving unlocked concurrence message processing mechanism |
CN105468690A (en) * | 2015-11-17 | 2016-04-06 | 中国建设银行股份有限公司 | Inventory data processing method and device |
CN107463593A (en) * | 2016-12-13 | 2017-12-12 | 阿里巴巴集团控股有限公司 | Stock reduces method and its equipment |
CN108133399A (en) * | 2016-11-30 | 2018-06-08 | 北京京东尚科信息技术有限公司 | The second of high concurrent fast-response kills the method, apparatus and system that inventory precisely reduces |
-
2018
- 2018-11-09 CN CN201811332849.3A patent/CN111177233A/en active Pending
Patent Citations (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN101685406A (en) * | 2008-09-27 | 2010-03-31 | 国际商业机器公司 | Method and system for operating instance of data structure |
CN104077113A (en) * | 2014-07-10 | 2014-10-01 | 中船重工(武汉)凌久电子有限责任公司 | Method for achieving unlocked concurrence message processing mechanism |
CN105468690A (en) * | 2015-11-17 | 2016-04-06 | 中国建设银行股份有限公司 | Inventory data processing method and device |
CN108133399A (en) * | 2016-11-30 | 2018-06-08 | 北京京东尚科信息技术有限公司 | The second of high concurrent fast-response kills the method, apparatus and system that inventory precisely reduces |
CN107463593A (en) * | 2016-12-13 | 2017-12-12 | 阿里巴巴集团控股有限公司 | Stock reduces method and its equipment |
Non-Patent Citations (1)
Title |
---|
JUVAL LOWY: "《COM与.NET组件服务》", 30 September 2002 * |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN113077220A (en) * | 2021-04-16 | 2021-07-06 | 北京京东拓先科技有限公司 | User request processing method and device |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN105718244B (en) | A kind of streamlined data are shuffled Spark task schedulings and the execution method of transmission | |
US9304863B2 (en) | Transactions for checkpointing and reverse execution | |
US8694733B2 (en) | Slave consistency in a synchronous replication environment | |
CN108615184B (en) | Accounting method and device | |
US8413169B2 (en) | Time-based event processing using punctuation events | |
US20140040208A1 (en) | Early release of transaction locks based on tags | |
US20080249988A1 (en) | Computer programming method and system for performing a reversal of selected structured query language operations within a database transaction | |
CN109033359A (en) | A kind of method of multi-process secure access sqlite | |
CN111639965B (en) | Integral updating method and device | |
CN107798111B (en) | Method for exporting data in large batch in distributed environment | |
US9940168B2 (en) | Resource sharing using process delay | |
CN112035230A (en) | Method and device for generating task scheduling file and storage medium | |
CN111026758B (en) | Electronic ticket issuing method and device | |
CN111177233A (en) | CAS inventory control method | |
CN107818475B (en) | Coupon issuing method, device and computer-readable storage medium | |
CN112883041A (en) | Data updating method and device, electronic equipment and storage medium | |
US9652766B1 (en) | Managing data stored in memory locations having size limitations | |
CN114490856A (en) | Database WAL (Web independent language) disk-dropping method and system based on IOURING technology | |
CN109034982B (en) | Method and device for accelerating processing of order information in ten-million-level order form | |
WO2020181798A1 (en) | Data processing method and device, server and computer-readable storage medium | |
CN113722321A (en) | Data export method and device and electronic equipment | |
CN105683961B (en) | Method and system for managing data for write back of hypothesis analysis | |
CN111367625B (en) | Thread awakening method and device, storage medium and electronic equipment | |
US10810662B1 (en) | Utilizing time-series tables for tracking accumulating values | |
CN111259023B (en) | Data scheduling method and device, electronic equipment and storage medium |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
RJ01 | Rejection of invention patent application after publication |
Application publication date: 20200519 |
|
RJ01 | Rejection of invention patent application after publication |