Database inventory synchronization method and device, electronic equipment and storage medium
By recording transaction information in a high-concurrency cache database and synchronizing it to an ordinary database, the problem of inability to synchronize during high-concurrency inventory management is solved, ensuring the normal operation and data consistency of the trading system.
Patent Information
- Application Number
- CN202410656638.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-24
- Publication Date
- 2024-08-27
Smart Images

Figure CN118552126A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of financial technology, and in particular to a method for synchronizing database inventory, a device thereof, an electronic device and a storage medium. Background Art
[0002] With the rapid development of e-commerce, inventory management of item quantities has become critical. Items that are usually sold can generally be managed in inventory through ordinary databases (for example, relational databases), which can meet transaction response requirements. However, for some popular items or items in specific activities, high concurrent requests will occur. At this time, using ordinary databases for inventory management cannot meet transaction response requirements. In order to cope with high concurrent requests, a cache database that can perform high concurrent processing (for example, a high-performance key-value database) can be used for inventory management.
[0003] However, although the cache database can effectively cope with high-concurrency requests, it also has certain limitations. For example, data cannot be persisted and the disaster recovery mechanism is complex, which will lead to the risk of data loss. Therefore, inventory management also needs to use ordinary databases as a backup mechanism. In some specific cases, high-concurrency inventory needs to be downgraded from the cache database to the ordinary database for management. However, during this downgrade process, there will be the problem of inventory synchronization, that is, it is necessary to ensure that the data in the high-concurrency inventory can be synchronized to the ordinary inventory in a timely and accurate manner to ensure the accuracy and reliability of the entire inventory system.
[0004] To address the above-mentioned problems, no effective solution has been proposed yet. Summary of the invention
[0005] The embodiments of the present invention provide a method for synchronizing database inventory and its device, electronic device and storage medium, so as to at least solve the technical problem in the related art that when a high-concurrency cache database is used for inventory management, inventory synchronization with a general database cannot be performed, which easily leads to the risk of data loss.
[0006] According to one aspect of an embodiment of the present invention, a method for synchronizing database inventory is provided, comprising: upon receiving an item transaction request, reading a fuse state and an inventory flag; when the fuse state is an unblown state and the inventory flag is a cache database, performing an inventory deduction operation, wherein the inventory deduction operation at least comprises: an inventory read operation, an inventory deduction operation, and a recording operation; the inventory read operation is used to read the inventory in the cache database; the inventory deduction operation is used to deduct the inventory in the cache database; the recording operation is used to record the deduction information in a preset message queue while deducting the inventory in the cache database; a preset consumer thread is used to obtain the deduction information from the preset message queue; based on the deduction information, the preset consumer thread is used to synchronize the inventory in a common database, wherein the concurrency performance of the common database is lower than the concurrency performance of the cache database.
[0007] Furthermore, when the blown state is an unblown state and the inventory flag is a cache database, the steps for performing an inventory deduction operation include: constructing an inventory read operation statement, an inventory deduction operation statement, and a record operation statement; encapsulating the inventory read operation statement, the inventory deduction operation statement, and the record operation statement to obtain an inventory deduction atomic statement; executing the inventory deduction atomic statement to perform the inventory deduction operation, wherein the inventory deduction atomic statement is used to synchronously perform the inventory read operation, the inventory deduction operation, and the record operation in a synchronous pipeline.
[0008] Furthermore, in the process of performing the inventory deduction operation, it also includes: initiating a connection request to the cache database; when the response result returned by the cache database does not meet the fuse condition, deducting the inventory in the cache database, and recording the transaction details data when the deduction is successful, wherein the response result includes: the response duration returned after each initiation of the connection request; the fuse condition refers to the response duration greater than the preset response threshold returned in the connection request initiated with a preset number of connections being greater than the preset number of responses; when the response result returned by the cache database meets the fuse condition, the fuse state is updated to the fuse state and the inventory flag is updated to the general database, and the deduction inventory downgrade processing is performed, wherein the deduction inventory downgrade processing refers to using the general database to perform inventory deduction.
[0009] Furthermore, after reading the fuse status and the inventory mark, it also includes: when the fuse status is a blown status and the inventory mark is a normal database, executing inventory deduction downgrade processing; when the fuse status is an unblown status and the inventory mark is a normal database, returning transaction suspension information.
[0010] Furthermore, before executing the deduction inventory downgrade processing, it also includes: determining whether the deduction information exists in the preset message queue; if the deduction information exists in the preset message queue, using the preset consumption thread to synchronize the inventory in the general database until the deduction information no longer exists in the preset message queue.
[0011] Furthermore, the method for synchronizing the database inventory also includes: constructing a detector; using the detector to detect the cache database based on a preset time interval; when the detection result indicates that the cache database is available, updating the blown state to an unblown state; synchronizing the inventory in the ordinary database to the cache database, and updating the inventory flag to the cache database.
[0012] Furthermore, based on a preset time interval, the step of using the detector to detect the cache database includes: after the execution time of the inventory downgrade processing is deducted and reaches the preset fuse time, simulating a preset number of transaction requests; sending the preset number of transaction requests to the cache database, and receiving the response time returned by the cache database; and determining that the cache database is available when all the response times are less than or equal to a preset response threshold.
[0013] According to another aspect of an embodiment of the present invention, a synchronization device for database inventory is also provided, including: a reading unit, for reading a fuse state and an inventory mark when an item transaction request is received; an operation unit, for performing an inventory deduction operation when the fuse state is an unblown state and the inventory mark is a cache database, wherein the storage and deduction operation at least includes: an inventory reading operation, an inventory reduction operation, and a recording operation; the inventory reading operation is used to read the inventory in the cache database; the inventory reduction operation is used to deduct the inventory in the cache database; the recording operation is used to record the deduction information in a preset message queue while deducting the inventory in the cache database; an acquisition unit, for acquiring the deduction information from the preset message queue using a preset consumption thread; a synchronization unit, for performing a synchronization operation on the inventory in a common database based on the deduction information using the preset consumption thread, wherein the concurrency performance of the common database is lower than the concurrency performance of the cache database.
[0014] Furthermore, the operation unit includes: a first construction module, used to construct an inventory read operation statement, an inventory deduction operation statement and a recording operation statement; a first encapsulation module, used to encapsulate the inventory read operation statement, the inventory deduction operation statement and the recording operation statement to obtain an inventory deduction atomic statement; a first execution module, used to execute the inventory deduction atomic statement to perform the inventory deduction operation, wherein the inventory deduction atomic statement is used to synchronously perform the inventory read operation, the inventory deduction operation and the recording operation in a synchronous pipeline.
[0015] Furthermore, the synchronization device also includes: a first initiating module, used to initiate a connection request to the cache database during the inventory deduction operation; a first deduction module, used to deduct the inventory in the cache database if the response result returned by the cache database does not meet the fuse condition, and record the transaction details data if the deduction is successful, wherein the response result includes: the response time returned after each initiation of the connection request; the fuse condition refers to the response time returned in the connection request initiated with a preset number of connections, which is greater than a preset response threshold, being greater than a preset number of responses; a first updating module, used to update the fuse state to the fuse state and the inventory flag to the general database if the response result returned by the cache database meets the fuse condition, and perform deduction inventory downgrade processing, wherein the deduction inventory downgrade processing refers to using the general database for inventory deduction.
[0016] Furthermore, the synchronization device also includes: a first processing module, which is used to perform inventory downgrade processing by deducting inventory after reading the fuse status and the inventory mark, when the fuse status is the fuse status and the inventory mark is the ordinary database; a first return module, which is used to return transaction suspension information when the fuse status is the unfused status and the inventory mark is the ordinary database.
[0017] Furthermore, the synchronization device also includes: a first judgment module, used to determine whether the deduction information exists in the preset message queue before executing the deduction inventory downgrade processing; a first synchronization module, used to use the preset consumption thread to synchronize the inventory in the general database when the deduction information exists in the preset message queue, until the deduction information no longer exists in the preset message queue.
[0018] Furthermore, the synchronization device also includes: a second construction module, used to construct a detector; a first detection module, used to use the detector to detect the cache database based on a preset time interval; a second update module, used to update the blown state to an unblown state when the detection result indicates that the cache database is available; a third update module, used to synchronize the inventory in the ordinary database to the cache database, and update the inventory flag to the cache database.
[0019] Furthermore, the first detection module includes: a first simulation submodule, used to simulate a preset number of transaction requests after the execution time of the inventory downgrade processing reaches a preset fuse time; a first sending submodule, used to send the preset number of transaction requests to the cache database and receive the response time returned by the cache database; a first determination submodule, used to determine that the cache database is available when all the response times are less than or equal to a preset response threshold.
[0020] According to another aspect of an embodiment of the present invention, a computer-readable storage medium is provided, wherein the computer-readable storage medium includes a stored computer program, wherein when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute any of the above-mentioned database inventory synchronization methods.
[0021] According to another aspect of an embodiment of the present invention, there is also provided an electronic device, comprising one or more processors and a memory, wherein the memory is used to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement any one of the above-mentioned database inventory synchronization methods.
[0022] In the present invention, when an item transaction request is received, the fuse status and the inventory flag are read, and when the fuse status is an unblown state and the inventory flag is a cache database, an inventory deduction operation is performed, and a preset consumption thread is used to obtain deduction information from a preset message queue. Based on the deduction information, a preset consumption thread is used to synchronize the inventory in the general database.
[0023] In the present invention, when a cache database is used for inventory management, through the inventory deduction operation, the deduction information can be recorded in the message queue while the inventory deduction is performed on the cache database, so that the ordinary database can synchronize the inventory by consuming the deduction message in the message queue, effectively ensuring that the data in the cache database can be synchronized to the ordinary database in a timely and accurate manner, so that when the cache database is fuse-broken and degraded, it can directly switch to the ordinary database to continue the transaction, thereby ensuring the normal operation of the entire transaction system, and further solving the technical problem in the related art that when a high-concurrency cache database is used for inventory management, it is impossible to synchronize the inventory with the ordinary database, which easily leads to the risk of data loss. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] The drawings described herein are used to provide a further understanding of the present invention and constitute a part of the present invention. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the drawings:
[0025] Figure 1 is a flow chart of an optional database inventory synchronization method according to an embodiment of the present invention;
[0026] Figure 2 is a schematic diagram of an optional inventory synchronization and circuit breaker degradation process according to an embodiment of the present invention;
[0027] Figure 3 is a schematic diagram of an optional automatic correction process of cache inventory according to an embodiment of the present invention;
[0028] Figure 4 is a schematic diagram of an optional database inventory synchronization device according to an embodiment of the present invention;
[0029] Figure 5 The present invention is a hardware structure block diagram of an electronic device (or mobile device) for a method for synchronizing database inventory according to an embodiment of the present invention. DETAILED DESCRIPTION
[0030] In order to enable those skilled in the art to better understand the scheme of the present invention, the technical scheme in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.
[0031] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0032] It should be noted that the database inventory synchronization method and device in the present invention can be used in the financial technology field when the database inventory is synchronized, and can also be used in any field other than the financial technology field when the database inventory is synchronized. The present invention does not limit the application field of the database inventory synchronization method and device.
[0033] It should be noted that the relevant information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) collected and involved in the present invention are all information and data authorized by the user or fully authorized by all parties, and the collection, storage, use, processing, transmission, provision, disclosure and application of relevant data are in compliance with the relevant laws, regulations and standards of the relevant regions, necessary confidentiality measures are taken, and public order and good customs are not violated, and corresponding operation entrances are provided for users to choose to authorize or refuse. For example, an interface is set between the system and the relevant users or organizations. Before obtaining relevant information, it is necessary to send an acquisition request to the aforementioned user or organization through the interface, and obtain relevant information after receiving the consent information fed back by the aforementioned user or organization.
[0034] For the following scenarios: (1) When only a cache database is used for inventory management, the data cannot be persisted, so in the case of disaster recovery, there is a risk of data loss and the data cannot be recovered, resulting in losses in transaction business; (2) When only a general database is used for inventory management, due to poor read-write capabilities, it is unable to cope with high concurrency requirements. The present invention proposes an inventory synchronization mechanism that can ensure the timely synchronization and consistency of inventory data when high-concurrency inventory is downgraded from the cache database to the general database for management, thereby ensuring the normal operation of the entire transaction system.
[0035] In the present invention, in order to realize the inventory synchronization mechanism, an inventory synchronization and fuse degradation process and a cache database inventory automatic correction process are proposed, which can ensure that when a cache database is blown, the ordinary database inventory can remain consistent, and when the cache database is restored, automatic switchback can be achieved, thereby realizing a closed loop of inventory management in a disaster recovery state.
[0036] The present invention is described in detail below in conjunction with various embodiments.
[0037] Embodiment 1
[0038] According to an embodiment of the present invention, an embodiment of a method for synchronizing a database inventory is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0039] Figure 1 is a flowchart of an optional database inventory synchronization method according to an embodiment of the present invention, such as Figure 1 As shown, the method comprises the following steps:
[0040] Step S101, when receiving an item transaction request, read the fuse status and inventory flag.
[0041] Step S102, when the fuse state is not blown and the inventory flag is the cache database, perform an inventory deduction operation, wherein the inventory deduction operation at least includes: an inventory read operation, an inventory reduction operation, and a recording operation; the inventory read operation is used to read the inventory in the cache database; the inventory reduction operation is used to deduct the inventory in the cache database; the recording operation is used to record the deduction information into a preset message queue while deducting the inventory in the cache database.
[0042] Step S103: using a preset consumption thread to obtain deduction information from a preset message queue.
[0043] Step S104, based on the deduction information, a preset consumption thread is used to synchronize the inventory in the general database, wherein the concurrency performance of the general database is lower than the concurrency performance of the cache database.
[0044] Through the above steps, when receiving an item transaction request, the fuse state and the inventory mark can be read, and when the fuse state is an unfused state and the inventory mark is a cache database, an inventory deduction operation is performed, and a preset consumption thread is used to obtain the deduction information from the preset message queue. Based on the deduction information, a preset consumption thread is used to synchronize the inventory in the general database. In an embodiment of the present invention, when a cache database is used for inventory management, through an inventory deduction operation, the deduction information can be recorded in the message queue while the cache database is deducted, so that the general database can synchronize the inventory through the deduction message in the consumption message queue, effectively ensuring that the data in the cache database can be synchronized to the general database in a timely and accurate manner, so that when the cache database is fuse-degraded, it is directly switched to the general database to continue the transaction, ensuring the normal operation of the entire transaction system, thereby solving the technical problem in the related art that when a high-concurrency cache database is used for inventory management, it is impossible to synchronize the inventory with the general database, which easily leads to the risk of data loss.
[0045] The embodiment of the present invention is described in detail below in combination with the above steps.
[0046] Step S101, when receiving an item transaction request, read the fuse status and inventory flag.
[0047] In an embodiment of the present invention, in order to determine which type of database (a cache database capable of high concurrency processing or a normal database) the current trading system is using for inventory management, two parameters can be configured: a fuse status parameter and an inventory flag parameter. These two parameters can be stored in the memory to facilitate timely reading of parameter values.
[0048] In the embodiment of the present invention, the fuse state parameter is used to record whether the cache database is in an available state, the parameter value of the fuse state parameter is a fuse state or an unfused state, and the default value is an unfused state, wherein the fuse state indicates that the cache database is in an unavailable state, and the unfused state indicates that the cache database is in an available state. The inventory flag parameter is used to record the database used by the current transaction system, the parameter value of the inventory flag parameter is a cache database and a common database, and the default value is a cache database.
[0049] In step S101, when an item transaction request is received and the inventory of the item is deducted, the parameter values of the two parameters, namely, the fuse status and the inventory flag, can be read from the memory first to determine whether the current transaction system is using the database.
[0050] Optionally, after reading the circuit breaker status and the inventory mark, it also includes: when the circuit breaker status is the circuit breaker status and the inventory mark is the normal database, executing the inventory deduction downgrade processing; when the circuit breaker status is the uncircuited state and the inventory mark is the normal database, returning the suspension transaction information.
[0051] In an embodiment of the present invention, if the parameter values of the two parameters of the fuse state and the inventory mark are the default values (i.e., the fuse state and the cache database), the normal business process is carried out, and the cache database is used for inventory deduction. If the fuse state is the fuse state and the inventory mark is the ordinary database, it indicates that the current cache database is unavailable, and it is necessary to start the downgrade process and perform inventory deduction from the ordinary database (i.e., when the fuse state is the fuse state and the inventory mark is the ordinary database, the deduction inventory downgrade process is executed). If the fuse state is the unfused state and the inventory mark is the ordinary database, it indicates that the current cache database is available, but the inventory of the two databases has not been successfully synchronized, and the transaction needs to be temporarily stopped, for example, the customer is directly prompted "The activity is very popular, please try again later" (i.e., when the fuse state is the unfused state and the inventory mark is the ordinary database, the suspension transaction information is returned).
[0052] Step S102, when the fuse state is not blown and the inventory flag is the cache database, perform an inventory deduction operation, wherein the inventory deduction operation at least includes: an inventory read operation, an inventory reduction operation, and a recording operation; the inventory read operation is used to read the inventory in the cache database; the inventory reduction operation is used to deduct the inventory in the cache database; the recording operation is used to record the deduction information into a preset message queue while deducting the inventory in the cache database.
[0053] In an embodiment of the present invention, if the parameter values of the two parameters, the fuse state and the inventory flag, are the fuse state and the cache database respectively, it indicates that the current cache database is available and the inventories of the two databases are consistent, and the normal business process can be carried out, and the cache database can be used for inventory deduction (that is, when the fuse state is not in the fuse state and the inventory flag is the cache database, the inventory deduction operation is performed).
[0054] In this embodiment, the storage and deduction operations include: inventory reading operation, inventory reduction operation, and recording operation. These operations are encapsulated into atomic operations that are executed in one step to avoid data inconsistency in distributed scenarios. For example, in a distributed scenario, if the three operation processes do not use synchronous process restrictions, there will be a situation where there is a surplus when judging the inventory, but it will be snatched away by other servers during the deduction operation, resulting in data inconsistency.
[0055] In this embodiment, the read inventory operation is used to read the inventory in the cache database, the reduce inventory operation is used to deduct the inventory in the cache database, and the record operation is used to record the deduction information into the preset message queue while deducting the inventory in the cache database.
[0056] In some optional embodiments, the preset message queue may adopt a distributed message queue to handle high-concurrency and high-throughput message transmission.
[0057] Optionally, when the fuse state is an unfused state and the inventory flag is a cache database, the steps for performing an inventory deduction operation include: constructing a read inventory operation statement, a deduction inventory operation statement, and a record operation statement; encapsulating the read inventory operation statement, the deduction inventory operation statement, and the record operation statement to obtain an inventory deduction atomic statement; executing the inventory deduction atomic statement to perform an inventory deduction operation, wherein the inventory deduction atomic statement is used to synchronously perform a read inventory operation, a deduction inventory operation, and a record operation in a synchronous pipeline.
[0058] In an embodiment of the present invention, a read inventory operation statement, a decrement inventory operation statement, and a record operation statement can be constructed first, and then the read inventory operation statement, the decrement inventory operation statement, and the record operation statement can be encapsulated to obtain an inventory deduction atomic statement that is executed in one step. The inventory deduction atomic statement can simultaneously perform the read inventory operation, the decrement inventory operation, and the record operation in the synchronous pipeline. For example, the read inventory operation, the decrement inventory operation, and the record operation of the same server are executed in sequence in the synchronous pipeline, so that the inventory deduction atomic statement of one server can be executed after the synchronization execution is completed before the inventory deduction atomic statement of the next server is executed. Afterwards, the inventory deduction operation is performed by executing the inventory deduction atomic statement. In this way, when the inventory of the cache database is successfully deducted, the deduction information can be written into the message queue in time, so that the ordinary database can synchronize the inventory data in real time by consuming the deduction information in the message queue.
[0059] In some optional embodiments, the inventory deduction operation of the cache database can be implemented by scripting to encapsulate the three processes of reading inventory, reducing inventory, and entering deduction records into a message queue into an atomic operation executed in one step, and then the inventory deduction operation is implemented by executing the script.
[0060] Optionally, during the process of performing the inventory deduction operation, it also includes: initiating a connection request to the cache database; when the response result returned by the cache database does not meet the fuse condition, deducting the inventory in the cache database, and recording the transaction details data when the deduction is successful, wherein the response result includes: the response duration returned after each connection request is initiated; the fuse condition refers to the response duration greater than a preset response threshold returned in the connection request initiated with a preset number of connections is greater than the preset number of responses; when the response result returned by the cache database meets the fuse condition, the fuse state is updated to the fuse state and the inventory flag is updated to the general database, and the deduction inventory downgrade processing is performed, wherein the deduction inventory downgrade processing refers to the use of the general database for inventory deduction.
[0061] In an embodiment of the present invention, during the process of performing an inventory deduction operation, a connection request to the cache database can be initiated to perform a fuse control on the cache database. If the response result returned by the cache database (including the response duration returned after each connection request is initiated) does not meet the fuse condition (i.e., the response duration greater than the preset response threshold returned in the connection request initiated for a preset number of connections is greater than the preset number of responses, for example, more than 3 times out of 10 connection requests are returned with a timeout), it indicates that the current cache database is available and does not need to be fused. The inventory in the cache database can be deducted normally, and if the deduction is successful, the transaction details data are recorded. If the response result returned by the cache database meets the fuse condition, it indicates that the current cache database is unavailable and needs to be fused. The fuse state can be updated to the fuse state and the inventory flag can be updated to the ordinary database, and the deduction inventory downgrade processing is performed, and the deduction inventory downgrade processing refers to using the ordinary database for inventory deduction.
[0062] In some optional embodiments, during the inventory deduction operation, aspect technology (i.e., a programming paradigm that can separate cross-cutting concerns from different parts of the application and modularize these concerns, which are usually irrelevant to business logic, such as connection operations, etc. By using aspect technology, the modularity, maintainability, and reusability of the code can be improved, etc.) can be used to rewrite the connection operation of the cache database and perform fuse control. If the access to the cache database has a connection timeout and the number of response timeouts reaches a threshold, the cache database connection is fused, and the two parameters of the fuse state and inventory flag are synchronously set to the fuse state and ordinary database, respectively. At the same time, according to the setting of the fuse window period, all requests sent to the cache database within the fuse window period can be blocked, and the direct response returns that the fuse has been fused.
[0063] In some optional embodiments, after the two parameters of the fuse state and the inventory flag are set to the fuse state and the ordinary database, the inventory deduction process will be directly downgraded to using the ordinary database to perform inventory deduction operations.
[0064] Optionally, before executing the deduction inventory downgrade processing, it also includes: determining whether there is deduction information in the preset message queue; if there is deduction information in the preset message queue, using a preset consumption thread to synchronize the inventory in the general database until there is no deduction information in the preset message queue.
[0065] In the embodiment of the present invention, before executing the deduction inventory downgrade process, it is also necessary to determine whether the general database has been synchronized. Specifically, it can be determined whether there is deduction information in the preset message queue. If there is deduction information in the preset message queue, the preset consumption thread is continued to be used to synchronize the inventory in the general database until there is no deduction information in the preset message queue. After there is no deduction information in the preset message queue, it indicates that the synchronization of inventory data is completed, and using the general database for inventory deduction will not cause data inconsistency, which can effectively ensure data consistency.
[0066] Step S103: using a preset consumption thread to obtain deduction information from a preset message queue.
[0067] In step S103, a preset consumption thread may be used to obtain deduction information from a preset message queue so as to synchronize inventory data in a timely manner. The preset consumption thread may obtain deduction information from a preset message queue in the form of a scheduled task; the deduction information is the inventory data deducted by a transaction in the cache database.
[0068] Step S104, based on the deduction information, a preset consumption thread is used to synchronize the inventory in the general database, wherein the concurrency performance of the general database is lower than the concurrency performance of the cache database.
[0069] In step S104, the preset consumption thread may perform synchronization operations on the inventory in the general database according to the deduction information, for example, updating the inventory in the general database according to the inventory quantity deducted by a transaction in the cache database.
[0070] In this embodiment, the cache database is a database capable of high-concurrency inventory management, and the common database is a database capable of low-concurrency inventory management. Therefore, the concurrency performance of the common database is lower than that of the cache database.
[0071] In some optional embodiments, the cache database enters the deduction information into the message queue while deducting the inventory, and then the consumer can execute the inventory deduction operation of the ordinary database according to the deduction information, so as to achieve consistent deduction of the inventory. Moreover, since writing to the database is an asynchronous operation, it will not affect the transaction performance of the transaction system.
[0072] Optionally, the method for synchronizing database inventory also includes: constructing a detector; based on a preset time interval, using the detector to detect the cache database; when the detection result indicates that the cache database is available, updating the fuse state to an unfused state; synchronizing the inventory in the ordinary database to the cache database, and updating the inventory flag to the cache database.
[0073] In an embodiment of the present invention, a timer (i.e., a detector) can be set to execute the automatic correction process of the cache database inventory. The timer execution interval can be adjusted according to actual conditions. There are two execution conditions for the timer. One is when the parameter value of the inventory mark is a normal database, and the other is when the cache database is re-determined to be available. When these two conditions are not met, the timer runs empty (i.e., a detector is built, and based on a preset time interval, the cache database is detected using the detector).
[0074] In an embodiment of the present invention, if the detection result indicates that the cache database is available, the blown state can be updated to an unblown state, and then the inventory in the common database is synchronized to the cache database, and the inventory flag is updated to the cache database.
[0075] In some optional embodiments, when the execution condition of the timer is met, it means that the current cache database is available, and the fuse status can be updated to "not blown" first, and the inventory mark is not updated temporarily. At this time, the front end of the page can return a prompt "The activity is very popular, please try again later" to the customer according to the current parameter value, and briefly interrupt the transaction. In addition, when the transaction is briefly interrupted, it is necessary to suspend the inventory deduction operation. At this time, the transaction details are summarized and the inventory data of the cache database is updated according to the summary results. Afterwards, after the inventory synchronization of the cache database is completed, the inventory mark is updated to the cache database. At this time, the two parameter values are "not blown" and "cached database", and the transaction returns to the inventory deduction process of the cache database. In addition, under the inventory deduction process of the cache database, the inventory automatic correction timer of the cache database runs empty.
[0076] Optionally, based on a preset time interval, the step of using a detector to detect the cache database includes: after the execution time of the inventory downgrade processing is deducted and reaches the preset fuse time, simulating a preset number of transaction requests; sending the preset number of transaction requests to the cache database, and receiving the response time returned by the cache database; when all response times are less than or equal to a preset response threshold, determining that the cache database is available.
[0077] In an embodiment of the present invention, after the execution time of the inventory downgrade processing reaches a preset fuse time (which can be set according to actual conditions), a preset number of transaction requests can be simulated (i.e., multiple concurrent transaction requests are simulated), and then the preset number of transaction requests are sent to the cache database, and the response time returned by the cache database is received. If all response times are less than or equal to the preset response threshold, it can be determined that the cache database is available. In this way, the fuse status can be updated in time.
[0078] Another optional specific implementation is described in detail below.
[0079] In an embodiment of the present invention, an inventory synchronization mechanism is proposed, including: inventory synchronization and fuse degradation process, and cache inventory automatic correction process, which can ensure that the database inventory can remain consistent when the cache fuse occurs, and when the cache is restored, automatic switching back is achieved, thereby realizing a closed loop of inventory management in a disaster recovery state.
[0080] Figure 2 is a schematic diagram of an optional inventory synchronization and circuit breaker degradation process according to an embodiment of the present invention, such as Figure 2 As shown, the inventory synchronization and circuit breaker downgrade process is as follows:
[0081] (1) Using memory to store two parameters, namely, memory: fuse status and inventory flag, the fuse status includes fuse and unfuse; the inventory flag includes cache inventory and database inventory;
[0082] (2) When initiating an item transaction on the front end of the page, the back end can first perform business verification processing and then read the fuse status and inventory flag from the memory;
[0083] (3) Determine the current circuit breaker status and inventory mark. If the circuit breaker is not blown and the database inventory is available, it means that the cached inventory has not been synchronized yet, and the customer is directly prompted "Trading is very popular, please try again later"; if the circuit breaker is blown and the database inventory is available, start the inventory deduction downgrade process (executed when the cache is unavailable) and reduce the database inventory through the ordinary database; if the circuit breaker is not blown and the cache inventory is available, perform the cached inventory deduction process (use scripts to encapsulate reading inventory, reducing inventory, and entering the message queue as atomic operations);
[0084] (4) It is possible to simultaneously perform cache inventory reduction in the cache database and store deduction messages through the message queue. After that, the database inventory reduction can be performed in the ordinary database through a scheduled task and a message consumption thread;
[0085] (5) When performing cache inventory deduction processing, according to the cache fuse mechanism: when the fuse is broken, it is in the fuse state, and the inventory mark is the database inventory, it is determined whether the cache is broken;
[0086] (6) If the fuse is blown, the unblown state is written into the memory, and the inventory deduction downgrade process is performed (executed when the cache is unavailable) to reduce the database inventory through the ordinary database; if the fuse is not blown, it is determined whether the deduction is successful and the item transaction detail record table is recorded;
[0087] (7) If the deduction is not successful, the transaction fails; if the deduction is successful, the item transaction details record table is registered and other transaction logic is performed to confirm that the transaction is successful.
[0088] Figure 3 is a schematic diagram of an optional cache inventory automatic correction process according to an embodiment of the present invention, such as Figure 3 As shown, the automatic correction process of cache inventory is as follows:
[0089] (1) Automatically correct the cache inventory through a timer, and run the cache inventory correction timer (frequency parameter (adjustable): once per second);
[0090] (2) The inventory flag can be first determined from the memory: inventory flag. If it is cache inventory, the cache inventory correction timer continues to run;
[0091] (3) If it is a database inventory, the cache is probed to detect whether the cache is available, that is, a cache availability check is performed;
[0092] (4) Determine whether the cache is available. If not, continue to run the cache inventory correction timer;
[0093] (5) If available, update the fuse status in memory to: not blown;
[0094] (6) Synchronize the database inventory to the cache inventory and update the inventory flag in the memory to: cache inventory.
[0095] In the embodiment of the present invention, a cache inventory and database inventory coordination mechanism is proposed, which can solve the risk of data loss caused by the inability of cache to persist data, and realize the automatic downgrade process of inventory deduction and the automatic correction process of cache inventory, and realize the automatic switching process of inventory management under the disaster recovery state, ensuring data synchronization and consistency during the switching process, thereby ensuring the normal operation of transactions under the disaster recovery state.
[0096] The following is a detailed description in conjunction with another embodiment.
[0097] Embodiment 2
[0098] A database inventory synchronization device provided in this embodiment includes multiple implementation units, each implementation unit corresponds to each implementation step in the above-mentioned embodiment 1.
[0099] Figure 4 is a schematic diagram of an optional database inventory synchronization device according to an embodiment of the present invention, such as Figure 4 As shown, the synchronization device may include: a reading unit 40 , an operating unit 41 , an acquiring unit 42 , and a synchronization unit 43 .
[0100] The reading unit 40 is used to read the fuse status and inventory flag when receiving an item transaction request;
[0101] The operation unit 41 is used to perform an inventory deduction operation when the fuse state is an unfused state and the inventory flag is a cache database, wherein the inventory deduction operation at least includes: an inventory reading operation, an inventory reduction operation, and a recording operation; the inventory reading operation is used to read the inventory in the cache database; the inventory reduction operation is used to deduct the inventory in the cache database; the recording operation is used to record the deduction information into a preset message queue while deducting the inventory in the cache database;
[0102] An acquisition unit 42, configured to acquire deduction information from a preset message queue using a preset consumption thread;
[0103] The synchronization unit 43 is used to synchronize the inventory in the common database using a preset consumption thread based on the deduction information, wherein the concurrency performance of the common database is lower than the concurrency performance of the cache database.
[0104] The synchronization device can read the fuse state and inventory mark through the reading unit 40 when receiving the item transaction request, perform the inventory deduction operation through the operation unit 41 when the fuse state is the unfused state and the inventory mark is the cache database, obtain the deduction information from the preset message queue through the acquisition unit 42 using the preset consumption thread, and perform the synchronization operation on the inventory in the general database based on the deduction information through the synchronization unit 43 using the preset consumption thread. In the embodiment of the present invention, when the cache database is used for inventory management, through the inventory deduction operation, the deduction information can be recorded in the message queue while the cache database is deducted, so that the general database can synchronize the inventory through the deduction message in the consumption message queue, effectively ensuring that the data in the cache database can be synchronized to the general database in a timely and accurate manner, so that when the cache database is fuse-degraded, it is directly switched to the general database to continue the transaction, ensuring the normal operation of the entire transaction system, thereby solving the technical problem in the related art that when a high-concurrency cache database is used for inventory management, it is impossible to synchronize the inventory with the general database, which easily leads to the risk of data loss.
[0105] Optionally, the operation unit includes: a first construction module, used to construct inventory reading operation statements, inventory reduction operation statements and recording operation statements; a first encapsulation module, used to encapsulate the inventory reading operation statements, inventory reduction operation statements and recording operation statements to obtain inventory deduction atomic statements; a first execution module, used to execute the inventory deduction atomic statement to perform inventory deduction operations, wherein the inventory deduction atomic statement is used to synchronously perform inventory reading operations, inventory reduction operations and recording operations in a synchronous pipeline.
[0106] Optionally, the synchronization device also includes: a first initiating module, used to initiate a connection request to the cache database during the inventory deduction operation; a first deduction module, used to deduct the inventory in the cache database when the response result returned by the cache database does not meet the fuse condition, and record the transaction details data when the deduction is successful, wherein the response result includes: the response time returned after each connection request is initiated; the fuse condition refers to the response time greater than a preset response threshold returned in the connection request initiated with a preset number of connections is greater than a preset number of responses; a first updating module, used to update the fuse state to the fuse state and the inventory flag to the general database when the response result returned by the cache database meets the fuse condition, and perform deduction inventory downgrade processing, wherein the deduction inventory downgrade processing refers to using the general database for inventory deduction.
[0107] Optionally, the synchronization device also includes: a first processing module, which is used to perform inventory deduction downgrade processing after reading the fuse status and the inventory mark, when the fuse status is the fuse status and the inventory mark is the normal database; a first return module, which is used to return transaction suspension information when the fuse status is the unfused state and the inventory mark is the normal database.
[0108] Optionally, the synchronization device also includes: a first judgment module, used to determine whether there is deduction information in the preset message queue before executing the deduction inventory downgrade processing; a first synchronization module, used to use a preset consumption thread to synchronize the inventory in the general database when there is deduction information in the preset message queue, until there is no deduction information in the preset message queue.
[0109] Optionally, the synchronization device also includes: a second construction module, used to construct a detector; a first detection module, used to use the detector to detect the cache database based on a preset time interval; a second update module, used to update the blown state to an unblown state when the detection result indicates that the cache database is available; a third update module, used to synchronize the inventory in the ordinary database to the cache database, and update the inventory flag to the cache database.
[0110] Optionally, the first detection module includes: a first simulation sub-module, used to simulate a preset number of transaction requests after the execution time of the inventory downgrade processing reaches a preset fuse time; a first sending sub-module, used to send a preset number of transaction requests to the cache database and receive a response time returned by the cache database; a first determination sub-module, used to determine that the cache database is available when all response times are less than or equal to a preset response threshold.
[0111] The above-mentioned synchronization device may also include a processor and a memory. The above-mentioned reading unit 40, operation unit 41, acquisition unit 42, synchronization unit 43, etc. are all stored in the memory as program units, and the processor executes the above-mentioned program units stored in the memory to implement corresponding functions.
[0112] The processor includes a kernel, and the kernel retrieves the corresponding program unit from the memory. One or more kernels can be set, and the kernel parameters can be adjusted to synchronize the inventory in the common database based on the deduction information using a preset consumption thread.
[0113] The above-mentioned memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash memory (fl ash RAM), and the memory includes at least one storage chip.
[0114] The present invention also provides a computer program product, which, when executed on a data processing device, is suitable for executing a program that is initialized with the following method steps: when an item transaction request is received, the fuse status and the inventory flag are read, and when the fuse status is an unblown state and the inventory flag is a cache database, an inventory deduction operation is performed, and a preset consumption thread is used to obtain deduction information from a preset message queue, and based on the deduction information, a preset consumption thread is used to synchronize the inventory in a common database.
[0115] According to another aspect of an embodiment of the present invention, a computer-readable storage medium is provided. The computer-readable storage medium includes a stored computer program, wherein when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute the above-mentioned database inventory synchronization method.
[0116] According to another aspect of an embodiment of the present invention, there is also provided an electronic device, comprising one or more processors and a memory, wherein the memory is used to store one or more programs, wherein when the one or more programs are executed by one or more processors, the one or more processors implement the above-mentioned database inventory synchronization method.
[0117] Figure 5 1 is a hardware structure block diagram of an electronic device (or mobile device) for a method for synchronizing database inventory according to an embodiment of the present invention. Figure 5 As shown, the electronic device may include one or more processors (e.g., Figure 5 The processor 502a, processor 502b, ..., processor 502n, etc., which may include but are not limited to a processing device such as a microprocessor MCU or a programmable logic device FPGA, etc.), a memory 504 for storing data. In addition, it may also include: a display, an input / output interface (I / O interface), a universal serial bus (USB) port (which may be included as one of the ports of the I / O interface), a network interface, a keyboard, a power supply and / or a camera. It can be understood by those skilled in the art that Figure 5 The structure shown is only for illustration and does not limit the structure of the above electronic device. Figure 5 More or fewer components as shown, or with Figure 5 Different configurations shown.
[0118] The serial numbers of the above embodiments of the present invention are only for description and do not represent the advantages or disadvantages of the embodiments.
[0119] In the above embodiments of the present invention, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0120] In the several embodiments provided by the present invention, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are only schematic. For example, the division of the units can be a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of units or modules, which can be electrical or other forms.
[0121] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple units. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0122] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0123] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), mobile hard disk, magnetic disk or optical disk and other media that can store program codes.
[0124] The above is only a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principle of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.
Claims
1. A method for synchronizing database inventory, characterized in that: include: Upon receiving an item transaction request, read the fuse status and inventory flag; When the fuse state is an unfused state and the inventory flag is a cache database, an inventory deduction operation is performed, wherein the inventory deduction operation at least includes: an inventory reading operation, an inventory reduction operation, and a recording operation; the inventory reading operation is used to read the inventory in the cache database; the inventory reduction operation is used to deduct the inventory in the cache database; the recording operation is used to record the deduction information into a preset message queue while deducting the inventory in the cache database; Using a preset consumption thread to obtain the deduction information from the preset message queue; Based on the deduction information, the preset consumption thread is used to perform synchronization operations on the inventory in the general database, wherein the concurrency performance of the general database is lower than the concurrency performance of the cache database.
2. The synchronization method according to claim 1, characterized in that: When the fuse state is an unfused state and the inventory flag is a cache database, the step of performing an inventory deduction operation includes: Construct statements for reading inventory, reducing inventory, and recording inventory; Encapsulating the inventory reading operation statement, the inventory reduction operation statement, and the recording operation statement to obtain an inventory reduction atomic statement; The inventory deduction atomic statement is executed to perform the inventory deduction operation, wherein the inventory deduction atomic statement is used to synchronously perform the inventory reading operation, the inventory reduction operation, and the recording operation in a synchronous pipeline.
3. The synchronization method according to claim 1, characterized in that: The process of inventory deduction also includes: Initiating a connection request to the cache database; If the response result returned by the cache database does not meet the fuse condition, the inventory in the cache database is deducted, and if the deduction is successful, the transaction details are recorded, wherein the response result includes: the response time returned after each initiation of the connection request; the fuse condition refers to the response time greater than the preset response threshold returned in the connection request initiated with a preset number of connections is greater than the preset number of responses; When the response result returned by the cache database meets the fuse condition, the fuse state is updated to the fuse state and the inventory flag is updated to the general database, and the inventory deduction and downgrade processing is performed, wherein the inventory deduction and downgrade processing refers to the use of the general database for inventory deduction.
4. The synchronization method according to claim 1, characterized in that: After reading the circuit breaker status and inventory flags, also include: When the fuse state is the fuse state and the inventory flag is the common database, performing a deduction inventory downgrade process; When the circuit breaker state is the uncircuited state and the inventory flag is the normal database, the transaction suspension information is returned.
5. The synchronization method according to any one of claims 3 or 4, characterized in that: Before executing the deduction inventory downgrade process, it also includes: Determining whether the deduction information exists in the preset message queue; In the case that the deduction information exists in the preset message queue, the preset consumption thread is used to perform synchronization operation on the inventory in the common database until the deduction information does not exist in the preset message queue.
6. The synchronization method according to claim 1, characterized in that: The database inventory synchronization method also includes: Build the detector; Based on a preset time interval, using the detector to detect the cache database; When the detection result indicates that the cache database is available, updating the blown state to an unblown state; The inventory in the common database is synchronized to the cache database, and the inventory flag is updated to the cache database.
7. The synchronization method according to claim 6, characterized in that: The step of using the detector to detect the cache database based on a preset time interval includes: After the execution time of deducting the inventory downgrade processing reaches the preset circuit breaker time, simulate the preset number of transaction requests; Sending the preset number of transaction requests to the cache database, and receiving a response duration returned by the cache database; In a case where all the response durations are less than or equal to a preset response threshold, it is determined that the cache database is available.
8. A synchronization device for database inventory, characterized in that: include: A reading unit, used for reading a fuse status and an inventory flag when receiving an item transaction request; An operation unit, configured to perform an inventory deduction operation when the fuse state is an unfused state and the inventory flag is a cache database, wherein the inventory deduction operation at least includes: an inventory reading operation, an inventory reduction operation, and a recording operation; the inventory reading operation is used to read the inventory in the cache database; the inventory reduction operation is used to deduct the inventory in the cache database; the recording operation is used to record the deduction information into a preset message queue while deducting the inventory in the cache database; An acquisition unit, configured to acquire the deduction information from the preset message queue using a preset consumption thread; A synchronization unit is used to synchronize the inventory in a common database using the preset consumption thread based on the deduction information, wherein the concurrency performance of the common database is lower than the concurrency performance of the cache database.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a stored computer program, wherein when the computer program is executed, the device where the computer-readable storage medium is located is controlled to execute the database inventory synchronization method according to any one of claims 1 to 7.
10. An electronic device, characterized in that: It comprises one or more processors and a memory, wherein the memory is used to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement the database inventory synchronization method described in any one of claims 1 to 7.
