Demand cause

When the sender userASends a message to the receiving userBWhen,If the userBonline“, the previous article “Why doesn’t wechat throw” online messages “? Yes, through the confirmation of the application layer, the sender’s timeout retransmission and the receiver’s de-retransmission can ensure that the messages at the business level are not lost or retransmitted.

If user B is not online, how does the system ensure the message is reachable? This is the subject of this article.

 

Question: What is the process of sending messages when the receiver is not online?



Answer: As mentioned above,

(1) userASend a message to the userB

(2The server views the userBThe status ofoffline

(3) the server stores messages toDBIn the

(4) The server returns the userASending successful (For the sender, the message landsDBIt is considered successful.)

 

Question: The design of offline message table, the process of pulling offline?

Receiver_uid, MSg_id, time, sender_UID, MSg_type, MSG_Content…

Access mode: receiverBTo pull the senderAtotaSend offline messages only inreceiver_uid(B), sender_uid(A)Query, then delete the offline message, and then return the messageBCan.

The overall process of viewing the picture is as shown in the picture above,

(1) userBPull the userASent to thetaOffline message of

(2) server fromDBPull offline message

(3) server fromDBTo delete offline messages

(4) the server returns to the userBDesired offline message

 

Question: What is the problem with the above process?

Answer:If the userBHave a lot of good friendsWhen logging in, the client needs to pull offline messages for all friends.The interaction between the client and the server is frequent

Client pseudocode:

For (all uid in B’s friend-list){//All friends should be pulled when logging in

         get_offline_msg(B,uid);   // Interacting with the server

}

Optimization plan 1: first pull the number of offline messages from each friend, reallyThe userBThe pull request is sent to the server only when the offline message is viewed(This is often used on mobile phones to save trafficAccording to the need to pullThe optimization of the)

Optimization solution 2: Pull all offline messages sent by friends to user B at one time and calculate them locally based on sender_UID. In this case, the access mode of the out-of-school message table is changed to -> only the receiver_UID query is required. The number of login interactions with the server is reduced to one.

 

Problem: UserBSend to all your friends at oncetaWhen the message volume is very large, a request packet is very large, the speed is slow, easy to stall how to do?

Answer: Paging pull, according to business requirements, first pull the latest (or oldest) page of messages, and then pull page by page as required.

 

Question: How to ensure reachable, after the third step of the above steps, the fourth step of the offline message is returned to the client, the server hangs, the router loses the message, or the clientcrashIf the database has been deleted, the user has not received the offline message.

Answer: Well, if you follow the above1.2.3.4Step by step, yes, how do you ensure that offline messages are reachable?

Viewing pictures Is similar to the offline message ACK mechanism of the application layer. When user B receives an offline message, the offline message cannot be deleted from the database. The offline message cannot be deleted until the offline message ACK of the application layer indicates that user B has received the offline message.

 

Question: If the userBPull a page offline message, but inACKbeforecrashIt will be pulled the next time you log inRepeated offline messages?

Answer: Pulled offline message but did notACK, the server does not delete the previous offline message, so the system layer will retrieve the message again when you log in to the system next time. But at the business level, it can be based onmsg_idduplicate removalSMCTheory: Message loss and weight cannot be achieved at the system level, but can be achieved at the business level without user awareness.

 

Question: Suppose there are N pages of offline messages, and now each offline message requires an ACK. Wouldn’t the number of interactions between the client and the server double? Is there room for optimization?

Answer: It is not necessary to ACK each page of messages. When pulling the second page of messages, it is equivalent to the ACK of the first page of messages. At this time, the server can delete the first page of offline messages, and ACK the last page of messages again. The effect is that no matter how many pages of offline messages are pulled, there will only be one more ACK request and one more interaction with the server.

 

conclusion

The accessibility of “offline messages” can be more complex than you might think. Common optimizations include:

(1) for the same userB.Pull all users at once to sendtaIt can greatly reduce the interaction times of the server, compared with pulling messages one by one by sender

(2)Paging pull, pull count first and then pull on demand, is a common optimization on the wireless end

(3)The application layer of theACK, the application layer of the deduplicationTo ensure that offline messages are not lost and not heavy

(4)The pull of the next page, as well as the pull of the previous pageACK, can greatly reduce the number of interactions with the server



In instant messaging system, message reachability and state consistency are very interesting topics. Online delivery and offline pull of “group message” have not been introduced yet. If you are interested, you can discuss them together later: