Compensation method for preventing message loss in cross-machine-room communication scene
By introducing synchronous timestamps and distributed locking mechanisms in the cross-computer room communication scenarios of instant messaging, the problem of message delay or loss is solved, real-time delivery of messages and avoiding duplicate notifications are achieved.
Patent Information
- Application Number
- CN202411920397.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-25
- Publication Date
- 2025-03-28
AI Technical Summary
In cross-computer room communication scenarios of instant messaging, the prior art is difficult to effectively prevent message delay or loss, and may lead to repeated consumption of messages when message queues are delayed recovery.
By introducing a synchronous timestamp and distributed lock mechanism, we listen to events in the database message table, ensuring that messages are consumed only once when passed across computer rooms, and pull offline messages using synchronous timestamps when the client re-logs.
It effectively prevents message delays and loss caused by message queue delays in cross-computer room, ensures that messages are delivered in real time and avoids duplicate notifications.
Smart Images

Figure CN119728620A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of cross-machine-room communication of an instant messaging service end, and in particular to a compensation method for preventing message loss in a cross-machine-room communication scenario. Background Art
[0002] In the cross-computer room communication scenario in the field of instant messaging, since the communicating parties may be in different data centers, a reliable mechanism is needed to ensure that the message transmission between computer rooms is not lost and has low latency. The traditional approach to cross-computer room communication is to use message queues such as RocketMq for cross-computer room message communication. One computer room producer is responsible for sending messages, and the other computer room consumer is responsible for receiving and processing messages. The producer and consumer are decoupled through message queues to ensure reliable transmission of messages, and distributed locks are not used to cache messages. If this solution is used alone, in the cross-computer room instant messaging scenario, once the message queue is delayed, it will cause user message delays or even loss. And when the message queue recovers the delay, due to the lack of a distributed lock mechanism, it will cause repeated message consumption and repeated message push. At the same time, for the special scenario of instant messaging, a reliable message compensation mechanism is needed to ensure that user messages are not lost. Summary of the invention
[0003] The technical problem to be solved by the present invention is to overcome the deficiencies of the prior art, solve the problem of message delay or loss in instant messaging scenarios, and provide a compensation method for preventing message loss in cross-computer room communication scenarios.
[0004] In order to solve the above technical problems, the present invention provides a compensation method for preventing message loss in a cross-computer room communication scenario, which is characterized in that event monitoring, setting distributed locks, caching message IDs, etc. during data synchronization include the following steps:
[0005] Step 1: Introduce synchronized timestamps;
[0006] Step 2: When messages are synchronized to a remote data center, monitor the events in the database message table. Specifically, whenever a new event message is synchronized to a remote data center, the system will determine the event based on the preset conditions after receiving the notification. If the preset conditions are met, the event will be sent to the application for processing.
[0007] Step 3: Deserialize the monitored database change data to obtain the message ID and message recipient ID;
[0008] Step 4: Determine the user status, which includes online and offline;
[0009] Step 5: Add a distributed lock during the consumption process so that only one machine in the cluster can process the message, so that a message is consumed only once;
[0010] Step 6: Send a real-time message to the online device of the user queried in step 3;
[0011] Step 7: Update the synchronization timestamp of the real-time message in step 5;
[0012] Step 7 also includes step 7-1: changing the logic of pulling messages, specifically: when a user logs in to the client and initiates a request to pull messages, the synchronization timestamp is used as a condition to pull messages within the time range from this time point to before this login.
[0013] In step 1, introducing the synchronization timestamp specifically includes: adding a field in the message table to record the time when the message is synchronized to the target computer room.
[0014] In the step 2, the preset condition is: the event is an INSERT INTO statement of the message table.
[0015] In step 4, the user status is determined by searching the user status in the cache by the message recipient ID. If the user is currently online, the online device and channel information are obtained.
[0016] In step 5, a Set structure is used for storage, the Key is the user ID and the message ID, the Value is Equipment, and a one-hour expiration time is set for the cache. If the current machine acquires a distributed lock and starts processing messages, it first checks whether the message ID and Equipment are already in the cache. If so, it is skipped, otherwise a message is sent to the user's Equipment.
[0017] In step 7, the synchronization timestamp of the real-time message in step 5 is delayed in the scheduled task thread pool.
[0018] The beneficial effects achieved by the present invention are:
[0019] The present invention monitors database synchronization and has the opportunity to push the message again, thus preventing message delays caused by cross-machine room message queue delays.
[0020] The present invention modifies the message synchronization timestamp, so that the client will not lose the message after logging in again offline due to network reasons.
[0021] This method targets two scenarios: preventing message loss and resolving message synchronization delays. It places special emphasis on the importance of ensuring real-time message delivery and avoiding duplicate notifications, especially in offline delayed synchronization scenarios, by using tools such as Redis to cache message IDs and synchronize timestamp updates. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] Figure 1 A simplified method flow chart of an exemplary embodiment of the present invention;
[0023] Figure 2 A flowchart of the method steps of an exemplary embodiment of the present invention;
[0024] Figure 3 A real-time message repair flow diagram in an exemplary embodiment of the present invention. DETAILED DESCRIPTION
[0025] The present invention will be further described below in conjunction with the accompanying drawings and exemplary embodiments;
[0026] like Figure 1 The method step flow of an exemplary embodiment of the present invention shown in the figure provides a compensation method for preventing message loss in a cross-computer room communication scenario in a cross-computer room scenario, specifically including:
[0027] Step 1: Introduce synchronization timestamp: Add a field in the message table to record the time when the message is synchronized to the target computer room;
[0028] Step 2: When the message is synchronized to the remote computer room, monitor the events of the database message table. Specifically, whenever a new event message is synchronized to the remote computer room, the system judges the event according to the preset conditions after receiving the notification. If the preset conditions are met, it is sent to the application for processing; the preset conditions are: the event is a message table and is of INSERT type;
[0029] Step 3: Deserialize the monitored database change data to obtain the message ID and message recipient ID;
[0030] Step 4: The user status of the target computer room is divided into two situations: online and offline. When the user is online, the user's real-time message will be delayed due to the delay of the message queue. When the user is offline and the last contact timestamp of the session has been updated before going offline, the next time the user logs in to pull the offline message, the offline message will be pulled according to the last saved erroneous timestamp, which will cause the delayed message to be lost. When the user does not have any device online and has not established a link with the server, the user is judged to be offline. To determine the user status, specifically: search for the user status in the cache by the message recipient ID. If the user is currently online, obtain the online device and Channel information;
[0031] Step 5: Add a distributed lock during the consumption process so that only one machine in the cluster can process the message, so that a message is consumed only once. Specifically, use the Set structure for storage, with the Key being the user ID and message ID, and the Value being Equipment. Set a one-hour expiration time for the cache. If the current machine acquires the distributed lock and starts processing the message, first check whether the message ID and Equipment are already in the cache. If so, skip it.
[0032] Step 6: Send a real-time message to the online device of the user queried in step 3;
[0033] Step 7: Update the message synchronization timestamp: Specifically, adjust the synchronization timestamp of the real-time message in step 5, and delay processing in the scheduled task thread pool;
[0034] Step 7-1: Change the logic of pulling messages. Specifically, when the user pulls messages again, use the synchronization timestamp as a condition to pull messages within the time range from this time point to before the current login.
[0035] Figure 2 This is a method step flow chart of an exemplary embodiment of the present invention. After a user sends a message, the message is stored in the current database 1 and then synchronized to the database 2 of a remote computer room. At this time, it is determined whether it is an INSERT event of the message table. If it meets the conditions, the message is sent to the current computer room user.
[0036] Figure 3 This is a delay scenario repair flow chart in an exemplary embodiment of the present invention. The current user contact sends a message. After being stored in the current computer room database 1, the message is synchronized to the remote computer room database 2. However, due to the message synchronization delay, the message synchronization is completed only after the current user replies to the message offline. When the message is synchronized to the remote computer room, the message synchronization timestamp is updated. When the user logs in again, the synchronization timestamp is used as the query condition to pull the message, and the message is successfully obtained.
[0037] The beneficial effects achieved by the present invention are:
[0038] The present invention monitors database synchronization and has the opportunity to push the message again, thus preventing message delays caused by cross-machine room message queue delays.
[0039] The present invention modifies the message synchronization timestamp, so that the client will not lose the message after logging in again offline due to network reasons.
[0040] This method targets two scenarios: preventing message loss and resolving message synchronization delays. It places special emphasis on the importance of ensuring real-time message delivery and avoiding duplicate notifications, especially in offline delayed synchronization scenarios, by using tools such as Redis to cache message IDs and synchronize timestamp updates.
[0041] The present invention may also have many other implementation modes. The above embodiments do not limit the present invention in any way. Without departing from the spirit and essence of the present invention, technicians familiar with the field may make various corresponding changes and modifications based on the present invention. All other improvements and applications made to the above embodiments in an equivalent transformation manner should fall within the scope of protection of the claims attached to the present invention.
Claims
1. A compensation method for preventing message loss in a cross-computer room communication scenario, characterized in that: The steps include: Step 1: Introduce synchronized timestamps; Step 2: When messages are synchronized to a remote data center, monitor the events in the database message table. Specifically, whenever a new event message is synchronized to a remote data center, the system will determine the event based on the preset conditions after receiving the notification. If the preset conditions are met, the event will be sent to the application for processing. Step 3: Deserialize the monitored database change data to obtain the message ID and message recipient ID; Step 4: Determine the user status, which includes online and offline; Step 5: Add a distributed lock during the consumption process so that only one machine in the cluster can process the message, so that a message is consumed only once; Step 6: Send a real-time message to the online device of the user queried in step 3; Step 7: Update the synchronization timestamp of the real-time message in step 5; Step 7 also includes step 7-1: changing the logic of pulling messages, specifically: when a user logs in to the client and initiates a request to pull messages, the synchronization timestamp is used as a condition to pull messages within the time range from this time point to before this login.
2. A compensation method for preventing message loss in a cross-computer room communication scenario according to claim 1, characterized in that: In step 1, introducing the synchronization timestamp specifically includes: adding a field in the message table to record the time when the message is synchronized to the target computer room.
3. A compensation method for preventing message loss in a cross-computer room communication scenario as claimed in claim 2, characterized in that: In the step 2, the preset condition is: the event is an INSERT INTO statement of the message table.
4. A compensation method for preventing message loss in a cross-computer room communication scenario as claimed in claim 3, characterized in that: In step 4, the user status is determined by searching the user status in the cache by the message recipient ID. If the user is currently online, the online device and channel information are obtained.
5. A compensation method for preventing message loss in a cross-computer room communication scenario as claimed in claim 4, characterized in that: In step 5, a Set structure is used for storage, the Key is the user ID and the message ID, the Value is Equipment, and a one-hour expiration time is set for the cache. If the current machine acquires a distributed lock and starts processing messages, it first checks whether the message ID and Equipment are already in the cache. If so, it is skipped, otherwise a message is sent to the user's Equipment.
6. A compensation method for preventing message loss in a cross-computer room communication scenario as claimed in claim 5, characterized in that: In step 7, the synchronization timestamp of the real-time message in step 5 is delayed in the scheduled task thread pool.