Data synchronization method, apparatus, device, and medium
By initiating delayed messages and message queues on the client to query business status, the problem of inconsistent data synchronization between the client and the server is solved, achieving more efficient data synchronization and system stability.
Patent Information
- Application Number
- CN202310658182.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-05
- Publication Date
- 2025-10-10
- Estimated Expiration
- 2043-06-05
AI Technical Summary
In the prior art, data synchronization between the client and the server is not constrained by the client's business logic, which makes it difficult to achieve data consistency when data interaction fails.
When the client initiates a service request to the server, it first starts a delayed message, then sends the service request, and queries the service status through the message queue. It performs corresponding synchronization operations based on the status, including sending service execution requests or query requests, until the data is synchronized.
Effectively handle communication anomalies with long time intervals, achieve data consistency, improve the stability and practicality of data transmission between systems, and avoid human intervention.
Smart Images

Figure CN116662445B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of communication between a client and a server, and in particular to a data synchronization method, device, equipment and medium. Background Art
[0002] There are many ways to exchange data between systems, and calling interfaces is the most common method on the internet today. This method allows for fast and efficient data exchange. However, communication between clients and servers via interfaces is often synchronous. However, when inter-system communication anomalies occur, system data exchange requests fail, leading to data inconsistencies in bilateral or even multilateral systems.
[0003] Currently, feedback from the server is usually used to determine whether the data between the client and the server is synchronized, and a retry mechanism with a set number or time is used to resolve interaction failures to achieve data consistency. However, these mechanisms are not constrained by the client's business logic and are therefore difficult to meet the client's needs. Summary of the Invention
[0004] The technical problem to be solved by the present invention is to overcome the defect in the prior art that data synchronization is not constrained by the business logic of the client, and to provide a data synchronization method, apparatus, device and medium.
[0005] The present invention solves the above technical problems through the following technical solutions:
[0006] A first aspect of the present invention provides a data synchronization method, applied to a client, the data synchronization method comprising:
[0007] When initiating a service request to a server, first start a delay message, and then send the service request to the server; wherein the delay message is used to delay the start of a message queue including information about the service request after a specific time;
[0008] When the message queue is started, querying the service status of the service request according to the message queue;
[0009] Determining whether data with the server is synchronized according to the business status;
[0010] If it is determined that the data with the server is not synchronized, a corresponding synchronization operation is performed; wherein the synchronization operation is used to achieve data synchronization.
[0011] Preferably, the step of determining whether the data with the server is synchronized according to the business status specifically includes:
[0012] If the service state is the initialization state, it is determined that the data with the server is not synchronized;
[0013] or,
[0014] If the service status is a waiting state, it is determined that the data with the server is not synchronized.
[0015] Preferably, the service request includes a service execution request and a service query request;
[0016] The step of performing a corresponding synchronization operation if it is determined that the data with the server is not synchronized specifically includes:
[0017] If the service state is the initialization state, sending the service execution request to the server;
[0018] or,
[0019] If the service state is a waiting state, a service query request is sent to the server; wherein the service query request is used to query the progress of the service execution request in the server.
[0020] Preferably, the data synchronization method further includes:
[0021] receiving a response signal from the server; wherein the response signal includes a signal from the server responding to the service request;
[0022] Determine, based on the response signal, whether to resend the message queue according to a preset rule corresponding to the service request; wherein the preset rule includes a preset interval time for sending the message queue.
[0023] Preferably, the message queue includes setting tags;
[0024] The step of determining, according to the response signal, whether to resend the message queue according to the preset rule corresponding to the service request includes:
[0025] If the response signal includes the setting tag, synchronizing the data of the service request according to the response signal and terminating sending the message queue;
[0026] or,
[0027] If the response signal does not include the set tag, the message queue is sent again according to the preset rule.
[0028] Preferably, the data synchronization method further includes:
[0029] When the total number of times the message queue is sent reaches a number threshold or the total time reaches a time threshold, sending of the message queue is terminated; wherein the number threshold or time threshold is a preset threshold of the client.
[0030] Preferably, after terminating sending the message queue, the method further includes: sending the message queue to a database for subsequent processing.
[0031] A second aspect of the present invention provides a data synchronization device, applied to a client, the data synchronization device comprising:
[0032] a sending module, configured to, when initiating a service request to a server, first initiate a delay message and then send the service request to the server; wherein the delay message is used to initiate a message queue containing information about the service request after a specific delay; and a query module, configured to query the service status of the service request according to the message queue after the message queue is initiated;
[0033] A determination module, configured to determine whether data with the server is synchronized according to the service status;
[0034] An execution module is used to execute a corresponding synchronization operation if the determination module determines that the data with the server is not synchronized; wherein the synchronization operation is used to achieve data synchronization.
[0035] A third aspect of the present invention provides an electronic device comprising a memory, a processor, and a computer program stored in the memory and used to run on the processor, wherein the processor implements the data synchronization method described in the first aspect when executing the computer program.
[0036] A fourth aspect of the present invention provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the data synchronization method described in the first aspect.
[0037] On the basis of conforming to the common sense in this field, the above-mentioned preferred conditions can be arbitrarily combined to obtain the preferred embodiments of the present invention.
[0038] The positive progress effect of the present invention is that: when initiating a service request to the server, the present invention first starts a delay message and then sends the service request to the server. Therefore, after a specific delay, the client can actively query and judge whether the data with the server is synchronized according to the message queue, and perform corresponding synchronization operations according to the service status. It can not only handle data asynchrony caused by communication anomalies with a long time interval without human intervention, but also the client can perform different synchronization operations according to different service states to achieve data consistency, thereby more effectively meeting the needs of the client and improving the stability and practicality of data transmission between systems. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] Figure 1 This is a flowchart of a data synchronization method provided in Example 1 of the present invention.
[0040] Figure 2 This is an application scenario diagram of a data synchronization method provided in Example 1 of the present invention.
[0041] Figure 3 A module diagram of a data synchronization device provided in Example 2 of the present invention.
[0042] Figure 4 This is a structural diagram of an electronic device provided in Example 3 of the present invention. DETAILED DESCRIPTION
[0043] The present invention is further described below by way of examples, but the present invention is not limited to the scope of the examples.
[0044] Example 1
[0045] This embodiment provides a data synchronization method, which is applied to a client. Figure 1 As shown, the data synchronization method includes:
[0046] S1. When initiating a service request to a server, first start a delay message and then send the service request to the server; wherein the delay message is used to start a message queue containing information about the service request after a specific delay; S2. After the message queue is started, query the service status of the service request according to the message queue;
[0047] S3. Determine whether the data with the server is synchronized based on the business status;
[0048] S4. If it is determined that the data with the server is not synchronized, a corresponding synchronization operation is performed; wherein the synchronization operation is used to achieve data synchronization.
[0049] In practice, service requests can include deductions, refunds, advances, completions, merchant number changes, and more. When a client initiates a service request to a server, it must first initiate a delay message before sending the specific service request to the server. Specifically, the delay message is a message queue containing the specific service request information, which is then processed by the client after a specific delay.
[0050] In this embodiment, taking a deduction request as an example, when the client initiates a deduction request to the server, it first starts a delay message and then sends the deduction request to the server; after a specific delay, the client starts processing the message queue including the deduction request, and queries the business status of the deduction request on the client according to the message queue; determines whether the deduction data with the server is synchronized according to the business status; if the client determines that the deduction data with the server is not synchronized, it performs the corresponding synchronization operation.
[0051] In one feasible solution, Figure 2 As shown, step S3 specifically includes:
[0052] (1) If the service state is the initialization state, it is determined that the data is not synchronized with the server;
[0053] (2) If the service state is the waiting state, it is determined that the data is not synchronized with the server.
[0054] In the embodiment, taking the deduction service as an example, the interaction between the client and the server includes:
[0055] (1) The client initiates a deduction request: first, a delay message is started, and then a deduction request is sent to the payment center of the server;
[0056] (2) The server returns the refund result to the client through asynchronous callback. If the service state of the refund service is the initialization state, the client fails to send the refund request to the server, that is, the data is not synchronized;
[0057] If the service state of the refund service is the waiting state, the client successfully sends the refund request to the payment center of the server, and does not receive the refund result of the server, that is, the data is not synchronized.
[0058] In the specific implementation, after the client starts the message queue, the service state of the deduction service is queried according to the specific information of the deduction service contained in the message queue.
[0059] Specifically, the client can view the interface state of data transmission with the server according to the business name of the deduction service, so as to obtain the service state of the deduction service.
[0060] Specifically, the client can also query the data state of the deduction service in the database. If the flag data is the initialization state, the service state is the initialization state; if the flag data is the waiting state, the service state is the waiting state.
[0061] In the specific implementation, the service request includes a service execution request and a service query request;
[0062] In an implementable scheme, as shown in FIG. 4, step S4 specifically includes: Figure 2
[0063] (1) If the service state is the initialization state, a service execution request is sent to the server;
[0064] (2) If the service state is the waiting state, a service query request is sent to the server; wherein the service query request is used to query the process of the service execution request in the server.
[0065] In an implementable scheme, the data synchronization method further includes:
[0066] S5. Receive a response signal from the server; wherein the response signal includes a signal from the server responding to the service request;
[0067] S6. Determine, based on the response signal, whether to send the message queue again according to a preset rule corresponding to the service request; wherein the preset rule includes a preset interval time for sending the message queue.
[0068] In specific implementations, different business requests correspond to different preset rules to meet the needs of different business requests.
[0069] In a specific implementation, the message queue includes setting a tag.
[0070] In this embodiment, the client adds a set tag to the message queue. The set tag is a unique key for the client to access the server, which can be used as an idempotent checker and also as the main query condition of the polling mechanism.
[0071] In one feasible solution, step S6 includes:
[0072] (1) If the response signal includes a set tag, synchronizing the data of the service request according to the response signal and terminating the sending message queue;
[0073] (2) If the response signal does not include the set tag, the message queue is sent again according to the preset rules.
[0074] In this embodiment, the client sends a message queue with a set tag to the server. When a response signal with the set tag is received, it indicates that the last interaction between the client and the server was successful. The client synchronizes the data of the business request according to the information in the response signal and stops sending the message queue to the server. When the response signal does not carry a set tag, it indicates that the data synchronization between the client and the server still fails, and the message queue is sent again according to the preset rules.
[0075] In this embodiment, if the preset rule of the deduction request is every 2 n-1 The system requests once every minute (n is the number of data retries), and first receives the response signal from the server. If the response signal does not carry the set tag, the data has not been recovered, so the first data retry is performed, and the message queue is sent and the response signal is received every 1 minute. If the response signal still does not carry the set tag, the second data retry is performed, and the message queue is sent every 2 minutes... Continue to retry data according to the preset rules until a response signal with a set tag is received. The data is recovered, the business status is updated according to the information of the response signal with the set tag, and the data retry is terminated.
[0076] In one feasible solution, the data synchronization method further includes:
[0077] S7, when the total number of times of sending the message queue reaches a number threshold or the total time reaches a time threshold, terminating sending the message queue; wherein the number threshold or the time threshold is a preset threshold of the client.
[0078] In an implementable solution, step S7 is followed by further comprising:
[0079] S8, sending the message queue to a database for subsequent processing.
[0080] In a specific implementation, when the total number of times of sending the message queue exceeds a maximum number threshold preset by the client or the total time exceeds a maximum time threshold preset by the client, it is considered that the data retry fails, and the message queue is persisted to the database for manual or Job processing.
[0081] The application initiates a business request to the server, first starts a delay message, and then sends the business request to the server. When the delay time is reached, the client can actively query and determine whether the data of the server is synchronized according to the message queue, and perform corresponding synchronization operations according to the business state. It can not only handle the data asynchronization caused by long time interval communication exception without human intervention, but also can perform different synchronization operations according to different business states to achieve data consistency, so as to more effectively meet the needs of the client and improve the stability and practicability of data transmission between systems.
[0082] Embodiment 2
[0083] This embodiment provides a second aspect of the application, which provides a data synchronization device 20 applied to a client and used to implement the data synchronization method of the preceding embodiment 1, as shown in Figure 3 The data synchronization device 20 comprises:
[0084] The sending module 21 is configured to initiate a business request to the server, first start a delay message, and then send the business request to the server; wherein the delay message is used to start a message queue including information of the business request after a delay time.
[0085] The query module 22 is configured to query the business state of the business request according to the message queue when the message queue is started.
[0086] The determination module 23 is configured to determine whether the data of the server is synchronized according to the business state.
[0087] The execution module 24 is configured to perform corresponding synchronization operations if the determination module determines that the data of the server is not synchronized; wherein the synchronization operations are used to implement data synchronization.
[0088] In a specific implementation, if the service state is the initialization state, the determining module 23 determines that the data of the client and the server is not synchronized; or, if the service state is the waiting state, the determining module 23 determines that the data of the client and the server is not synchronized.
[0089] In a specific implementation, the service request includes a service execution request and a service query request, if the service state is the initialization state, the executing module 24 sends the service execution request to the server; or, if the service state is the waiting state, the executing module 24 sends the service query request to the server; wherein, the service query request is used to query the process of the service execution request in the server.
[0090] In an optional implementation, the data synchronization device further includes a retry module, the retry module is used to receive the response signal of the server, and determine whether to send the message queue again according to the preset rule corresponding to the service request according to the response signal; wherein, the response signal includes the signal of the server responding to the service request, and the preset rule includes the preset interval time of the message queue sending.
[0091] In an optional implementation, the message queue includes a set label, if the response signal includes the set label, the retry module is further used to synchronize the data of the service request and terminate the sending of the message queue according to the response signal; or, if the response signal does not include the set label, the retry module is further used to send the message queue again according to the preset rule.
[0092] In an optional implementation, the data synchronization device further includes a processing module, when the total number of times of sending the message queue reaches a number threshold or the total time reaches a time threshold, the processing module is used to terminate the sending of the message queue; wherein, the number threshold or the time threshold is a preset threshold of the client.
[0093] In an optional implementation, the processing module is further used to send the message queue to a database for subsequent processing.
[0094] Embodiment 3
[0095] The embodiment provides an electronic device, such as Figure 4 As shown in the figure, the electronic device 40 includes a memory, a processor, and a computer program stored in the memory and executable on the processor, and the processor implements the data synchronization method of the foregoing embodiment 1 when executing the computer program. Figure 4 The electronic device 40 shown is only an example, and should not bring any limitation to the function and use range of the embodiments of the present application.
[0096] The electronic device 40 may be a general-purpose computing device, such as a server device. Components of the electronic device 40 may include, but are not limited to, the at least one processor 41, the at least one memory 42, and a bus 43 connecting different system components (including the memory 42 and the processor 41).
[0097] The bus 43 includes a data bus, an address bus, and a control bus.
[0098] The memory 42 may include a volatile memory, such as a random access memory (RAM) 421 and / or a cache memory 422 , and may further include a read-only memory (ROM) 423 .
[0099] The memory 42 may also include a program / utility 425 having a set (at least one) of program modules 424, such program modules 424 including but not limited to: an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment.
[0100] The processor 41 executes various functional applications and data processing by running computer programs stored in the memory 42, such as the data synchronization method of embodiment 1 of the present invention.
[0101] The electronic device 40 may also communicate with one or more external devices 44 (e.g., a keyboard, a pointing device, etc.). Such communication may be performed via an input / output (I / O) interface 45. Furthermore, the model generating electronic device 40 may also communicate with one or more networks (e.g., a local area network (LAN), a wide area network (WAN), and / or a public network, such as the Internet) via a network adapter 46. Figure 4 As shown, the network adapter 46 communicates with other modules of the model-generated electronic device 40 via the bus 43. It should be understood that, although not shown in the figures, other hardware and / or software modules may be used in conjunction with the model-generated electronic device 40, including but not limited to microcode, device drivers, redundant processors, external disk drive arrays, RAID (RAID) systems, tape drives, and data backup storage systems.
[0102] It should be noted that although several units / modules or sub-units / modules of the electronic device are mentioned in the detailed description above, this division is merely exemplary and not mandatory. In fact, according to embodiments of the present invention, the features and functions of two or more units / modules described above may be embodied in a single unit / module. Conversely, the features and functions of a single unit / module described above may be further divided and embodied by multiple units / modules.
[0103] Example 4
[0104] The present invention also provides a computer-readable medium having a computer program stored thereon, and when the computer program is executed by a processor, the data synchronization method of the aforementioned embodiment 1 is implemented.
[0105] The readable storage medium may include, but is not limited to, a portable disk, a hard disk, a random access memory, a read-only memory, an erasable programmable read-only memory, an optical storage device, a magnetic storage device, or any suitable combination thereof.
[0106] In a possible implementation manner, the present invention may also be implemented in the form of a program product, which includes program code. When the program product is run on a terminal device, the program code is used to enable the terminal device to execute the data synchronization method of embodiment 1.
[0107] The program code for executing the present invention may be written in any combination of one or more programming languages, and may be executed entirely on the user device, partially on the user device, as an independent software package, partially on the user device and partially on a remote device, or entirely on the remote device.
[0108] Although specific embodiments of the present invention have been described above, those skilled in the art will appreciate that these are merely illustrative and that the scope of the present invention is defined by the appended claims. Those skilled in the art may make various changes or modifications to these embodiments without departing from the principles and essence of the present invention, and such changes and modifications are intended to fall within the scope of the present invention.
Claims
1. A data synchronization method, characterized in that: Applied to the client, the data synchronization method includes: When initiating a service request to a server, a delay message is first initiated, and then the service request is sent to the server; wherein the delay message is used to delay the start of a message queue containing information about the service request for a specific period of time; the service request includes at least one of deduction, refund, advance payment, completion, and merchant number change; When the message queue is started, querying the service status of the service request according to the message queue; Determining whether data with the server is synchronized according to the business status; If it is determined that the data with the server is not synchronized, a corresponding synchronization operation is performed; wherein the synchronization operation is used to achieve data synchronization; The step of determining whether the data with the server is synchronized according to the service status specifically includes: If the service state is the initialization state, it is determined that the data with the server is not synchronized; Or, if the service state is a waiting state, it is determined that the data with the server is not synchronized; The service request includes a service execution request and a service query request; The step of performing a corresponding synchronization operation if it is determined that the data with the server is not synchronized specifically includes: If the service state is the initialization state, sending the service execution request to the server; Or, if the service state is a waiting state, sending a service query request to the server; wherein the service query request is used to query the progress of the service execution request in the server; The data synchronization method further includes: When the total number of times the message queue is sent reaches a number threshold or the total time reaches a time threshold, sending of the message queue is terminated; wherein the number threshold or time threshold is a preset threshold of the client.
2. The data synchronization method according to claim 1, characterized in that: The data synchronization method further includes: receiving a response signal from the server; wherein the response signal includes a signal from the server responding to the service request; Determine, based on the response signal, whether to resend the message queue according to a preset rule corresponding to the service request; wherein the preset rule includes a preset interval time for sending the message queue.
3. The data synchronization method according to claim 2, wherein: The message queue includes a setting tag; The step of determining, according to the response signal, whether to resend the message queue according to the preset rule corresponding to the service request includes: If the response signal includes the setting tag, synchronizing the data of the service request according to the response signal and terminating sending the message queue; or, If the response signal does not include the set tag, the message queue is sent again according to the preset rule.
4. The data synchronization method according to claim 1, wherein: After terminating sending the message queue, the method further includes: sending the message queue to a database for subsequent processing.
5. A data synchronization device, characterized in that: Applied to the client, the data synchronization device includes: a sending module configured to, when initiating a service request to a server, first initiate a delay message and then send the service request to the server; wherein the delay message is configured to delay the start of a message queue containing information about the service request for a specific period of time; and wherein the service request includes at least one of a deduction, refund, advance payment, completion, and merchant number change; A query module, configured to query the service status of the service request according to the message queue after the message queue is started; A determination module, configured to determine whether data with the server is synchronized according to the service status; an execution module, configured to execute a corresponding synchronization operation if the determination module determines that the data with the server is not synchronized; wherein the synchronization operation is used to achieve data synchronization; If the service state is the initialization state, the determining module determines that the data of the client and the server are not synchronized; Or, if the service state is a waiting state, the determining module determines that data of the client and the server are not synchronized; The service request includes a service execution request and a service query request. If the service state is the initialization state, the execution module sends the service execution request to the server; Alternatively, if the service state is a waiting state, the execution module sends a service query request to the server; wherein the service query request is used to query the progress of the service execution request in the server; The data synchronization device also includes a processing module, which is used to terminate the sending of the message queue when the total number of times the message queue is sent reaches a number threshold or the total time reaches a time threshold; wherein the number threshold or time threshold is a preset threshold of the client.
6. An electronic device comprising a memory, a processor, and a computer program stored in the memory and configured to run on the processor, wherein: When the processor executes the computer program, the data synchronization method according to any one of claims 1 to 4 is implemented.
7. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the data synchronization method according to any one of claims 1 to 4 is implemented.
Citation Information
Patent Citations
Data processing method and device, equipment and storage medium
CN111796946A