A data synchronization method, apparatus, device and medium
By clearing the Redis cache during the transaction commit process and writing the data to Redis after the transaction commit is completed, the consistency problem between the MySQL database and the Redis database during data synchronization is solved, achieving a more efficient data synchronization effect.
Patent Information
- Application Number
- CN202111627621.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-28
- Publication Date
- 2025-12-02
- Estimated Expiration
- 2041-12-28
AI Technical Summary
In existing technologies, data inconsistency issues can easily occur between MySQL and Redis databases during data synchronization, especially when transaction exceptions occur, making it difficult to maintain data consistency.
During the transaction commit process, the Redis cache data is cleared. After the transaction is committed, the transaction data is written to the MySQL database, and then the data is written to Redis again after the transaction is committed, in order to keep the data in Redis and MySQL database synchronized.
By maintaining data consistency between Redis and MySQL databases during transaction commit, the effectiveness and efficiency of data synchronization are improved, inconsistencies between Redis cache and MySQL database are prevented, and the reliability of data queries is enhanced.
Smart Images

Figure CN114297299B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a data synchronization method, apparatus, device, and medium. Background Technology
[0002] In existing technologies, data from a MySQL database can be synchronized to Redis for data retrieval. However, if an anomaly occurs during the synchronization process, inconsistencies may arise between the data in Redis and the data in the MySQL database.
[0003] Therefore, a more effective and efficient data synchronization solution is needed. Summary of the Invention
[0004] This specification provides a data synchronization method, apparatus, device, and medium to address the technical problem of how to perform data synchronization more effectively and efficiently.
[0005] To address the aforementioned technical problems, the embodiments in this specification provide the following technical solutions:
[0006] This specification provides a data synchronization method, including:
[0007] If it is determined that there are transactions to be committed, then clear the Redis cache data;
[0008] After the transaction is committed, the transaction data generated by the transaction is determined and written into the MySQL database;
[0009] If the transaction is committed, the transaction data generated by the transaction is written to Redis to keep the data synchronized between Redis and the MySQL database.
[0010] Optionally, the method further includes:
[0011] If the transaction fails to commit, the transaction data will not be written to Redis.
[0012] Optionally, the method further includes:
[0013] If the transaction commit fails, the Redis cache data will be cleared again.
[0014] Optionally, the method further includes:
[0015] Set a flag and determine whether to write the transaction data generated by the transaction to Redis based on the value of the flag.
[0016] Optionally, if the transaction is committed, the transaction data generated by the transaction is written to Redis, including: if the transaction is committed and the flag is a preset value, the transaction data generated by the transaction is written to Redis.
[0017] Optionally, the method further includes:
[0018] Before the flag is changed to a preset value, the transaction data generated by the transaction will not be written to Redis.
[0019] Optionally, the method further includes:
[0020] If writing the transaction data generated by the transaction to Redis fails, the transaction data generated by the transaction obtained from the MySQL database will be written to Redis.
[0021] This specification provides a data synchronization device, comprising:
[0022] The synchronization preparation module is used to clear the Redis cache data if it is determined that there are transactions to be committed.
[0023] The first write module is used to determine the transaction data generated by the transaction after the transaction is committed, and write the transaction data into the MySQL database.
[0024] The second write module is used to write the transaction data generated by the transaction to Redis if the transaction is committed, so as to keep the data synchronized between Redis and MySQL database.
[0025] This specification provides a data synchronization device, including:
[0026] At least one processor;
[0027] as well as,
[0028] A memory that is communicatively connected to the at least one processor;
[0029] in,
[0030] The memory stores instructions that can be executed by the at least one processor, which, when executed by the at least one processor, enables the at least one processor to perform the data synchronization method described above.
[0031] This specification provides a computer-readable storage medium storing computer-executable instructions that, when executed by a processor, implement the data synchronization method described above.
[0032] The above-described at least one technical solution adopted in the embodiments of this specification can achieve the following beneficial effects:
[0033] The above technical solution writes transaction data to MySQL during the transaction commit process, and then writes the transaction data to Redis after the transaction commit is completed, ensuring the consistency of data in Redis and MySQL database, and improving the data synchronization effect and efficiency. Attached Figure Description
[0034] To more clearly illustrate the technical solutions in the embodiments of this specification or the prior art, the drawings used in the description of the embodiments of this specification or the prior art will be briefly introduced below. Obviously, the drawings described below are only some of the drawings that may be involved in the embodiments described in this specification. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0035] Figure 1 This is a schematic diagram of the execution entity of the data synchronization method in the first embodiment of this specification.
[0036] Figure 2 This is a flowchart illustrating the data synchronization method in the first embodiment of this specification.
[0037] Figure 3 This is a schematic diagram of the data synchronization process in the first embodiment of this specification.
[0038] Figure 4 This is a schematic diagram of the resource processing device in the second embodiment of this specification. Detailed Implementation
[0039] To enable those skilled in the art to better understand the technical solutions in this specification, the technical solutions of the embodiments of this specification will be clearly and completely described below with reference to the accompanying drawings. Obviously, the embodiments described in this specification are merely some embodiments of this application, and not all embodiments. Based on the embodiments of this specification, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of this application.
[0040] In existing technologies, data from a MySQL database can be synchronized to Redis for data retrieval. However, if a transaction exception occurs during the synchronization process, inconsistencies may arise between the data in Redis and the MySQL database. This is because MySQL databases have built-in transaction rollback functionality, but in Redis, changes are difficult to roll back, leading to inconsistencies between the two databases. This, in turn, hinders data retrieval.
[0041] The first embodiment of this specification (hereinafter referred to as "Embodiment 1") provides a data synchronization method. The executing entity in Embodiment 1 can be a terminal (including but not limited to mobile phones, computers, tablets, and televisions), a server, an operating system, an application, a data synchronization platform, or a data synchronization system, etc. That is, the executing entity can be diverse and can be set, used, or changed as needed. Alternatively, a third-party application can assist the executing entity in executing Embodiment 1. For example... Figure 1 As shown, the data synchronization method in Embodiment 1 can be executed by the server, and a corresponding application can be installed on the terminal (held by the user). Data can be transmitted between the terminal or the application and the server. Data can be collected, input, or output, or pages or information can be processed (to the user) through the terminal or the application, thereby assisting the server in executing the data synchronization method in Embodiment 1.
[0042] like Figure 2 As shown, the data synchronization method provided in Embodiment 1 includes:
[0043] S101: (Execution subject) If it is determined that there are transactions to be committed, then clear the Redis cache data;
[0044] The execution entity in Implementation Example 1 can determine whether there are transactions waiting to be committed. That is, the execution entity in Implementation Example 1 can determine whether there are transactions in the current method, and the transactions in the method are the transactions waiting to be committed.
[0045] If it is determined that there are transactions to be committed, the execution entity in Implementation Example 1 can clear the cached data in Redis.
[0046] S103: (Execution subject) After the transaction is committed, determine the transaction data generated by the transaction and write the transaction data into the MySQL database;
[0047] In Example 1, after the executing entity determines that a transaction (which may be referred to as the target transaction, and can be any transaction) has been committed, the executing entity can determine the transaction data generated by the target transaction and write the transaction data generated by the target transaction into the MySQL database. That is, as the target transaction generates transaction data, the transaction data generated by the target transaction is written into the MySQL database.
[0048] S105: (Execution subject) If the transaction is committed, the transaction data generated by the transaction is written to Redis to keep the data synchronized between Redis and MySQL database.
[0049] The execution entity in Example 1 can determine whether the target transaction has been committed. If the target transaction has been committed, the execution entity in Example 1 can write the transaction data generated by the target transaction (i.e., the transaction data already generated by the target transaction) to Redis. Since the transaction data generated during the target transaction commit process has already been written to the MySQL database, and the transaction data generated by the target transaction has also been written to Redis after the target transaction is committed, data synchronization between Redis and the MySQL database can be maintained.
[0050] In Example 1, if the executing entity of Example 1 determines that the target transaction has failed to commit, the executing entity of Example 1 will not write the transaction data generated by the target transaction to Redis, and the MySQL database can also perform a rollback operation.
[0051] If the execution entity of Example 1 determines that the target transaction has failed to commit, the execution entity of Example 1 can clear the Redis cache data again.
[0052] In Embodiment 1, the executing entity can set a flag corresponding to the target transaction (let's call it the target flag), and determine whether to write the transaction data generated by the target transaction to Redis based on the value of the target flag. Specifically, writing the transaction data generated by the target transaction to Redis if the target transaction has been committed can include: if the target transaction has been committed and the target flag is at a preset value, then the transaction data generated by the target transaction is written to Redis. Alternatively, before the target flag is changed to the preset value, the executing entity in Embodiment 1 does not write the transaction data generated by the target transaction to Redis.
[0053] Specifically, if it is determined that a target transaction is pending commit, the execution entity in Implementation Example 1 can set the target flag to a value different from the preset value and clear the Redis cached data. Assuming the preset value is 0, if it is determined that a target transaction is pending commit, the execution entity in Implementation Example 1 can set the target flag to 1 (or any other non-zero value), indicating that the Redis cached data has been cleared. After the target transaction is committed, the transaction data generated by the target transaction is determined and written to the MySQL database.
[0054] If the target transaction is determined to have been committed successfully, the target flag is set to 0, indicating that the target transaction has been committed successfully. Since the target flag is also set to a preset value, the execution entity in Implementation Example 1 writes the transaction data generated by the target transaction to Redis. If the target transaction is determined to have failed to commit successfully, the target flag is kept at 1, thus preventing the writing of the target transaction data to Redis, and the Redis cache is cleared again.
[0055] In Example 1, the executing entity can use the ThreadLocal principle to extract the transaction data generated by the target transaction in the current thread of the target transaction (that is, the transaction data generated by the target transaction can be stored in ThreadLocal before being written to Redis), and write the transaction data generated by the target transaction to Redis.
[0056] If, during the process of writing the transaction data generated by the target transaction to Redis as described above, the execution entity in Implementation Example 1 encounters a failure to write the transaction data to Redis (e.g., due to a problem with the Redis service), then the execution entity in Implementation Example 1 can retrieve the transaction data generated by the target transaction from the MySQL database. In this way, if Redis can write the data normally, the execution entity in Implementation Example 1 can write the transaction data generated by the target transaction retrieved from the MySQL database to Redis.
[0057] In Example 1, the transaction data generated by the target transaction can be written to Redis after the target transaction is committed by reconstructing the commit method in the Spring transaction manager.
[0058] The following is a specific example (for example) Figure 3 (As shown) Further explanation of Example 1:
[0059] First, let's introduce the various objects that can be used to implement this example:
[0060] SyncWrapper: A wrapper for the business logic of the cache to be synchronized, mainly used to store the business logic in the Redis cache to be synchronized. It has four methods:
[0061] prepareForCommit() prepares for commit. The default processing logic is to perform the first cache clearing (see S101) and set the flag to 1 when the transaction enters the prepare for commit phase (i.e. before the transaction commits).
[0062] doCommit() commits a transaction. The default behavior is to execute the Redis cache data synchronization logic after the transaction is completed and committed.
[0063] The doRollback() function handles transaction rollback logic and sets the flag to 1.
[0064] After doCleanupAfterCompletion() completes the cleanup, a flag is used to determine whether a second cleanup of the Redis cache is needed (see “Clearing Redis cache data again” above).
[0065] SyncHolder: A synchronization resource holder (e.g., ThreadLocal), mainly used to store logical collections to be synchronized in Redis, also known as the SyncWrapper collection.
[0066] SyncExpandTransactionManager: Inherits from Spring Transaction Manager and is used to extend the various stages of Spring transactions.
[0067] SyncExecutorFunction: A synchronous executor function used to notify business logic of the current execution stage of a transaction (preparing to commit, committing, rolling back, or completing).
[0068] AtomicSyncProvider: A synchronizer provider that exposes service interfaces to the outside world, providing an entry point for Redis data caching.
[0069] The SyncExecutorFunction implements the following methods for the synchronous executor: Method 1, the preparation and commit logic (when a transaction enters the preparation and commit phase, the first cache clear is performed on Redis (see S101), and the flag is set to 1); Method 2, the commit logic (Redis cache data synchronization logic); Method 3, the rollback logic (when a transaction enters the rollback phase, the flag is reset to 1 or kept at 1)).
[0070] This example can be executed by following these steps:
[0071] Create a SyncWrapper object and pass the SyncExecutorFunction to SyncWrapper.
[0072] Call the AtomicSyncProvider synchronization method and pass the SyncWrapper parameter.
[0073] AtomicSyncProvider determines whether there is a transaction (i.e., whether there is a transaction to be committed). If there is no transaction, it directly executes the synchronization logic. If there is a transaction, it passes SyncWrapper to the SyncHolder object and calls the add method of SyncHolder.
[0074] After receiving the SyncWrapper, the SyncHolder retrieves the SyncHolder from the current thread through the local ThreadLocal and stores the SyncWrapper in the wrappers collection (the SyncWrapper collection array).
[0075] When a transaction reaches the ready-to-commit phase (i.e. after the transaction is committed), SyncExpandTransactionManager will determine whether the current transaction is a new transaction. If not, it will return directly; if so, it will retrieve the wrappers (used to store the SyncWrapper collection array) from the current thread's SyncHolder and execute the ready-to-commit method of SyncWrapper.
[0076] The SyncWrapper's commit method calls the SyncExecutorFunction function interface to perform the first cache clearing in Redis and sets the flag to 1.
[0077] When a transaction reaches the commit phase, SyncExpandTransactionManager calls the commit method in SyncWrapper and clears the contents of SyncHolder.
[0078] The SyncWrapper submission method calls the SyncExecutorFunction function interface to execute the Redis data synchronization logic and modifies the flag to 0.
[0079] Alternatively, when a transaction reaches the rollback phase, SyncExpandTransactionManager will call the rollback method in SyncWrapper, set the flag to 1, and clear the contents of SyncHolder.
[0080] When a transaction reaches the completion phase (i.e., the transaction is committed), SyncExpandTransactionManager will call the completion method of SyncWrapper.
[0081] The completion method of SyncWrapper will check if the flag is 1. If it is 1, it will clear the Redis cache (see "Clear Redis cache data again" above).
[0082] When a transaction enters the pause phase, SyncExpandTransactionManager retrieves the tasks to be synchronized from SyncHolder and temporarily stores them in the transaction manager. When resuming, these tasks are retrieved from the transaction manager and re-stored in SyncHolder.
[0083] When querying cached data, if data synchronization is successful (i.e., the transaction data generated by the transaction was successfully written to Redis), the synchronized cached data is returned directly. If data synchronization fails (i.e., the transaction data generated by the transaction failed to be written to Redis), the Redis cache is empty, and the transaction data generated by the transaction in the MySQL database is read again and written to Redis.
[0084] The above are merely examples and are not intended to limit the scope of Embodiment 1.
[0085] In Example 1, the transaction data generated during the transaction commit process is written to MySQL, and after the transaction commit is completed, the transaction data generated during the transaction is written to Redis. This ensures the consistency between the data in Redis and the data in the MySQL database, and improves the data synchronization effect and efficiency.
[0086] In Example 1, the Redis cache is cleared before the target transaction is committed. If the target transaction fails to commit, the Redis cache is cleared again. By clearing the cache twice, it prevents threads other than the thread corresponding to the target transaction from writing data to Redis, which could lead to inconsistencies between the Redis cache and the MySQL database, thus further improving the data synchronization effect and efficiency.
[0087] In Example 1, the value of the flag bit can be used to determine whether to write transaction data to Redis, which facilitates the unified handling of Redis execution logic (including the logic of writing transaction data to Redis or clearing the Redis cache). Different code is inserted in different steps (including the step of determining whether the transaction is committed successfully and the step of determining whether the transaction is committed unsuccessfully), which further improves the data synchronization effect, efficiency and flexibility.
[0088] Example 1 writes data to the MySQL and Redis databases based on the transaction commit phases (including pre-commit, post-commit, and commit completion phases). This approach does not restrict the location or order of the code. Even if the code writing data to Redis precedes the code writing data to the MySQL database, the transaction commit phases still apply, with data being written to the MySQL database first, followed by Redis. In other words, regardless of the code's location or order, data consistency between Redis and MySQL databases is guaranteed, further improving data synchronization effectiveness and efficiency while reducing the burden and difficulty of writing code.
[0089] like Figure 4 As shown, the second embodiment of this specification provides a data synchronization device corresponding to the data synchronization method described in Embodiment 1, comprising:
[0090] Synchronization preparation module 202 is used to clear the Redis cache data if it is determined that there are transactions to be committed;
[0091] The first writing module 204 is used to determine the transaction data generated by the transaction after the transaction is committed, and write the transaction data into the MySQL database.
[0092] The second writing module 206 is used to write the transaction data generated by the transaction to Redis if the transaction is committed, so as to keep the data synchronized between Redis and MySQL database.
[0093] Optionally, the second writing module 206 is further configured to not write the transaction data of the transaction to Redis if the transaction commit fails.
[0094] Optionally, the synchronization preparation module 202 is further configured to clear the Redis cache data again if the transaction commit fails.
[0095] Optionally, the device further includes:
[0096] The flag module is used to set a flag bit; the second write module 206 determines whether to write the transaction data generated by the transaction to Redis based on the value of the flag bit.
[0097] Optionally, if the transaction is committed, the transaction data generated by the transaction is written to Redis, including: if the transaction is committed and the flag is a preset value, the transaction data generated by the transaction is written to Redis.
[0098] Optionally, the second write module 206 does not write the transaction data generated by the transaction to Redis before the flag bit is changed to a preset value.
[0099] Optionally, the second writing module 206 is further configured to write the transaction data generated by the transaction obtained from the MySQL database to Redis if writing the transaction data generated by the transaction to Redis fails.
[0100] The third embodiment of this specification provides a data synchronization device, including:
[0101] At least one processor;
[0102] as well as,
[0103] A memory that is communicatively connected to the at least one processor;
[0104] in,
[0105] The memory stores instructions that can be executed by the at least one processor, which, when executed by the at least one processor, enables the at least one processor to perform the data synchronization method described in Embodiment 1.
[0106] The fourth embodiment of this specification provides a computer-readable storage medium storing computer-executable instructions that, when executed by a processor, implement the data synchronization method described in Embodiment 1.
[0107] The above embodiments can be used in combination, and modules with the same name in different embodiments or within the same embodiment can be the same or different modules.
[0108] The foregoing has described specific embodiments of this specification; other embodiments are within the scope of the appended claims. In some cases, the actions or steps described in the claims may be performed in a different order than those shown in the embodiments and may still achieve the desired result. Furthermore, the processes depicted in the drawings do not necessarily have to follow the specific or sequential order shown to achieve the desired result. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0109] The various embodiments in this specification are described in a progressive manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the embodiments of apparatus, devices, and non-volatile computer-readable storage media are basically similar to the method embodiments, so the descriptions are relatively simple; relevant parts can be referred to the descriptions of the method embodiments.
[0110] The apparatus, device, non-volatile computer-readable storage medium and method provided in the embodiments of this specification are corresponding. Therefore, the apparatus, device and non-volatile computer storage medium also have similar beneficial technical effects as the corresponding method. Since the beneficial technical effects of the method have been described in detail above, the beneficial technical effects of the corresponding apparatus, device and non-volatile computer storage medium will not be repeated here.
[0111] In the 1990s, improvements to a technology could be clearly distinguished as either hardware improvements (e.g., improvements to the circuit structure of diodes, transistors, switches, etc.) or software improvements (improvements to the methodology). However, with technological advancements, many methodological improvements today can be considered direct improvements to the hardware circuit structure. Designers almost always obtain the corresponding hardware circuit structure by programming the improved methodology into the hardware circuit. Therefore, it cannot be said that a methodological improvement cannot be implemented using hardware physical modules. For example, a Programmable Logic Device (PLD) (such as a Field Programmable Gate Array (FPGA)) is such an integrated circuit whose logic function is determined by the user programming the device. Designers can program and "integrate" a digital system onto a PLD themselves, without needing chip manufacturers to design and manufacture dedicated integrated circuit chips. Furthermore, nowadays, instead of manually manufacturing integrated circuit chips, this programming is mostly implemented using "logic compiler" software. Similar to the software compiler used in program development, the original code before compilation must be written in a specific programming language, called a Hardware Description Language (HDL). There are many HDLs, such as ABEL (Advanced Boolean Expression Language), AHDL (Altera Hardware Description Language), Confluence, CUPL (Cornell University Programming Language), HDCal, JHDL (Java Hardware Description Language), Lava, Lola, MyHDL, PALASM, and RHDL (Ruby Hardware Description Language). Currently, the most commonly used are VHDL (Very-High-Speed Integrated Circuit Hardware Description Language) and Verilog. Those skilled in the art should understand that by simply performing some logic programming on the method flow using one of these hardware description languages and programming it into an integrated circuit, the hardware circuit implementing the logical method flow can be easily obtained.
[0112] The controller can be implemented in any suitable manner. For example, it can take the form of a microprocessor or processor and a computer-readable medium storing computer-readable program code (e.g., software or firmware) executable by the (micro)processor, logic gates, switches, application-specific integrated circuits (ASICs), programmable logic controllers, and embedded microcontrollers. Examples of controllers include, but are not limited to, the following microcontrollers: ARC 625D, Atmel AT91SAM, Microchip PIC18F26K20, and Silicon Labs C8051F320. A memory controller can also be implemented as part of the control logic of the memory. Those skilled in the art will also recognize that, in addition to implementing the controller in purely computer-readable program code form, the same functionality can be achieved by logically programming the method steps to make the controller take the form of logic gates, switches, application-specific integrated circuits, programmable logic controllers, and embedded microcontrollers. Therefore, such a controller can be considered a hardware component, and the means included therein for implementing various functions can also be considered as structures within the hardware component. Alternatively, the means for implementing various functions can be considered as both software modules implementing the method and structures within the hardware component.
[0113] The systems, devices, modules, or units described in the above embodiments can be implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a computer. Specifically, a computer can be, for example, a personal computer, laptop computer, cellular phone, camera phone, smartphone, personal digital assistant, media player, navigation device, email device, game console, tablet computer, wearable device, or any combination of these devices.
[0114] For ease of description, the above devices are described in terms of function, divided into various units. Of course, in implementing this specification, the functions of each unit can be implemented in one or more software and / or hardware components.
[0115] Those skilled in the art will understand that the embodiments of this specification can be provided as methods, systems, or computer program products. Therefore, the embodiments of this specification can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the embodiments of this specification can take the form of a computer program product implemented on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0116] This specification is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this specification. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create a machine for implementing the flowchart illustrations and / or block diagrams. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0117] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0118] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0119] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0120] Memory may include non-persistent storage in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM. Memory is an example of computer-readable media.
[0121] Computer-readable media includes both permanent and non-permanent, removable and non-removable media that can store information using any method or technology. Information can be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other non-transferable medium that can be used to store information accessible by a computing device. As defined herein, computer-readable media does not include transient computer-readable media, such as modulated data signals and carrier waves.
[0122] It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0123] This specification can be described in the general context of computer-executable instructions that are executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, etc., that perform a specific task or implement a specific abstract data type. This specification can also be practiced in distributed computing environments, where tasks are performed by remote processing devices connected via a communication network. In distributed computing environments, program modules can reside in local and remote computer storage media, including storage devices.
[0124] The various embodiments in this specification are described in a progressive manner. Similar or identical parts between embodiments can be referred to interchangeably. Each embodiment focuses on describing the differences from other embodiments. In particular, the system embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions in the method embodiments.
[0125] The above description is merely an embodiment of this specification and is not intended to limit this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principle of this application should be included within the scope of the claims of this application.
Claims
1. A data synchronization method, comprising: The transaction commit phase is divided into pre-commit, post-commit, and commit completion phases, and data is written to the MySQL database and Redis based on the transaction commit phases. The writing of data to the MySQL database and Redis based on the transaction commit phases includes: If it is determined that there is a transaction to be committed, the Redis cache data is cleared, and the flag corresponding to the transaction is set to a value different from the preset value. After the transaction is committed, the transaction data generated by the transaction is determined, the transaction data is written to the MySQL database, and the transaction data is stored in ThreadLocal; If the transaction is determined to be completed, the value of the flag is set to the preset value; if the transaction is determined to be failed, the value of the flag remains unchanged, and the Redis cache data is cleared again. If the value of the flag corresponding to the transaction is the preset value, then the transaction data is extracted from the current thread of the transaction using ThreadLocal and written to Redis.
2. The method of claim 1, further comprising: If the value of the flag bit is not the preset value, then the transaction data of the transaction will not be written to Redis.
3. The method as described in claim 1 or 2, further comprising: If writing the transaction data generated by the transaction to Redis fails, the transaction data generated by the transaction obtained from the MySQL database will be written to Redis.
4. A data synchronization device, characterized in that, The data synchronization device is used to divide the transaction commit phase into pre-commit, post-commit, and commit completion phases, and write data to the MySQL database and Redis based on the transaction commit phases. The data synchronization device includes: The synchronization preparation module is used to clear the Redis cache data and set the flag corresponding to the transaction if it is determined that there is a transaction to be committed, setting the value of the flag to a value different from the preset value. The first writing module is used to determine the transaction data generated by the transaction after the transaction is committed, write the transaction data into a MySQL database, and store the transaction data in ThreadLocal. The second write module is used to set the value of the flag bit to the preset value if it is determined that the transaction has been completed; if it is determined that the transaction has failed, keep the value of the flag bit unchanged and clear the cached data of Redis again; if the value of the flag bit corresponding to the transaction is the preset value, then use the ThreadLocal to extract the transaction data from the current thread of the transaction and write the transaction data to Redis.
5. The apparatus as described in claim 4, characterized in that, The second writing module is further configured to: If the value of the flag bit is not the preset value, then the transaction data of the transaction will not be written to Redis.
6. The apparatus as described in claim 4 or 5, characterized in that, The second writing module is further configured to: If writing the transaction data generated by the transaction to Redis fails, the transaction data generated by the transaction obtained from the MySQL database will be written to Redis.
7. A data synchronization device, comprising: At least one processor; as well as, A memory that is communicatively connected to the at least one processor; The memory stores instructions that can be executed by the at least one processor, which, when executed by the at least one processor, enables the at least one processor to perform the data synchronization method according to any one of claims 1 to 3.
8. A computer-readable storage medium storing computer-executable instructions that, when executed by a processor, implement the data synchronization method of any one of claims 1 to 3.
Citation Information
Patent Citations
Method for realizing consistency of Redis and MYSQL data based on distributed lock
CN106446037A
Transaction processing method and device and storage medium
CN112631741A