SSM-based management system design and report

Introduction: Another problem that IM messages need to face: how to ensure that the received messages are not out of order. This article first analyzes the causes of disorder in detail, and provides corresponding solutions for each cause. And at the end of the article, a once and for all design plan is given.

Another problem that IM messages need to face: how to ensure that the received messages are not out of order. Let's start with a look at the obstacles to solving this problem.

The reason for the out-of-order message
time is difficult to guarantee

When it comes to "sequence", there must be a measurement standard, but neither client time nor server time is used as this standard to measure the order of messages.
IM server design-how to solve the disorder of messages

As shown in the figure above, an IM system is connected to multiple clients at different access gateways, and then processed on different logical processing servers, whether it is the client itself or the server (network, logical server), The time on each machine is different, so the local time of the machine cannot be used as a standard to measure the order of messages.

Network order cannot be guaranteed

Considering the scenario where only one client connects to one gateway, even in such a scenario, the order of messages is difficult to guarantee due to network factors.
IM server design-how to solve the disorder of messages

As shown in the figure above, the gateway tries to send the two messages message 1 and 2 to the client in turn, the following problems may occur:

The gateway sends message 1 to the client. At this time, the client's network condition is not good, causing the message to be lost or retransmitted.
The gateway did not wait for the sending result of message 1, and continued to send message 2. At this time, the client's network condition became better, and this message was received by the client faster than message 1.
In the above scenario, some people may think of a processing mode: the gateway will continue to send message 2 only after the client responds to the receipt of message 1, so that there will be no message disorder caused by network reasons. However, in this case, the message is equivalent to serial transmission, and the efficiency is not high.

Out of order caused by multi-threading factors

Both the client and the server may have multiple sending and receiving threads, which is also one of the reasons for the disorder of messages.

Resolution strategy

The previous analysis of several causes of the disorder of the message, the following analysis should be done one by one.

Message sequence number

The first problem mentioned earlier: the timing standard of the message cannot be measured by the local time of the client or server. At this time, a component that generates incremental IDs can be introduced, and this component can uniformly generate incremental, The sequence number of non-returned messages is used to measure the sequence of messages.

However, there is a part that can be discussed in detail: Does the ID generated by this component need to be globally unique? That is to say, both single chat and group chat need to ensure that the generated serial number is unique.

This global uniqueness is not necessary, because different chats can ensure that the messages are unique and incremented in their own channel. With this premise, the process of generating ID for this component is roughly as follows:

The logical server ID that handled the chat.
Each chat channel (single chat, group chat) has its own independent channel ID.
Inside each channel, it is guaranteed to generate an incremental and non-rewinding serial number.
In this way, the message sequence number actually consists of three parts: logical server ID-channel ID-message sequence number in the channel.

Group chat message processing

With the previous message sequence number, the first problem has been solved: the problem of message timing standards. However, this is not enough, considering the group chat scenario in the figure below:
IM server design-how to solve the disorder of messages

In the picture above:

The two clients send out message A and message B in sequence.
It can be seen from the above that the reason for the disorder of group chat messages is that the messages of the same group chat are finally dispatched to two different logical servers for processing.
Still continue to use the above idea of ​​generating message serial numbers: if it is the same chat channel, they will be processed together. Therefore, it can be changed into the processing method in the following figure:
IM server design-how to solve the disorder of the message

In the above figure, the logical server is selected according to the group ID of the group chat message, so that the messages of the same group can be processed in the same server.

It can be seen that there is no need to use a component such as a "distributed unique incremental ID" to generate an ID, because the problem here is simplified to: only the message sequence number is required to be unique and unique in the chat channel processed by the logical server. Increment is enough. The re-analysis and definition of the problem made this process much easier.

Dealing with network disorder

Then deal with the disorder caused by the network. There are similar methods to deal with the network disorder in the TCP protocol. Simply put:

The TCP protocol stack has a buffer to buffer the received data.
The sender uses the serial number ACK to confirm the data received by the receiver, such as data with three serial numbers of 1, 2, and 3. If it receives 1 first, the sender will receive the ACK 1 message, but after that If message 3 is received by the receiver before message 2, the sender will still ACK message 1, indicating that message 3 is out of order.
With the buffer and confirmation sequence number, we know which data can be provided to the application layer by the protocol stack.
IM server design-how to solve the disorder of messages

As shown in the figure above:

Messages 1 and 3 are stored in the receiver's TCP protocol stack, and message 2 has not been received yet.
Message 1 is confirmed by the sender, and message 1 can be provided to the application layer at this time.
Since message 2 is not received, message 3 is out of order and cannot be provided to the application layer.
The inspiration from this is that the sender can control the sending and receiving queue. The sender knows the order of the messages. Although the order of sending and receiving messages cannot be guaranteed, because of the introduction of a buffer, only confirmed messages can be consumed. In this way, the sender's ACK confirmation can be used to ensure the sequential consumption of messages.

The above ideas can be applied to the processing of network out-of-sequence messages.

final plan

Based on the above analysis, the message disorder problem can be solved in the following way.

Client message cache queue

Inside the client, it maintains a queue of cached messages. Each message has a corresponding message sequence number. After receiving the message, it needs to confirm with the gateway to confirm whether the message is received in order. There is only such a message Can be provided to application layer consumption.
IM server design-how to solve the disorder of messages

In the picture above:

The format of the message serial number is: logical server ID-channel ID-a unique and incremental ID in the channel, so the message in the above figure has a corresponding logical server ID of 1, and a channel ID of 2. The following description uses only the ID in the channel for convenience.
The client receives a message with a message sequence number of 101. At this time, the client will ACK and the gateway will confirm the message. Since the messages before 101 have been confirmed, the gateway responds with ACK 101, which means that the sequence number can be directly provided to the application layer for message.
The client receives a message with a message sequence number of 103. At this time, the client will ACK and the gateway will confirm the message. Since the 102 message before this message did not receive an ACK, the gateway responds with ACK 101, which means that the message notifying the client that it can submit to the application layer message at this time is still 101.
Message serial number and logical server switching

The message sequence number is composed of: logical server ID, channel ID, and unique and incremental ID in the channel.

Logical server ID: The hostname, process number, and listening port number of the service can be combined to generate a logical server ID.
Channel ID: Since the chat channel is only related to the participants in the chat, the single chat channel can combine the client IDs of both ends participating in the chat to generate the channel ID, while the group chat ID uses the group ID to generate the channel ID.
Unique and incremental ID in the channel: This value can even be saved without having to place a disk. It only needs to correspond to an incremental ID corresponding to the channel ID by the chat logic server each time.
When the logical server that processes the message changes, the logical server ID also changes. At this time, whether it is a gateway or a client, once it is found that the logical server ID of the message cache queue maintained by the communication with a certain client has changed, the previously cached messages will be discarded and the communication will be resumed.
We have a professional production team and strict confidentiality system. Our engineers have accumulated rich experience in all areas of software engineering development and design to ensure service levels. Provide them with graduation design and provide each student with an obligation.

In this article, I would like to introduce how computer-related majors complete their graduation design, because I have been engaged in helping to produce computer-related majors for many years. I have a wealth of work experience and would like to graduate soon. Students share.

Contact·Relation·We:. buckle. buckle. number (one zero three two three seven one two two)

Get in touch with us and put forward your writing requirements to us; our consultants will communicate with you on "service content, service requirements, service hours, other requirements" and other aspects according to your service needs. The finished product design is completely based on originality, and it is by no means inferior products modified by other generation counterparts on the market.

As in the above figure, after receiving the message with the serial number 1-2-103, the logical server is down. At this time, the client's chat service is processed by another server, and its logical server ID becomes 2, channel The ID becomes 3. At this time, the gateway detects that the logical server ID and channel ID have changed in the messages sent and received, so it will clear the previous message cache, and notify the client to clear the message cache, and re-communication with the logical server ID and channel ID messages. This means that messages after 101 need to be re-confirmed before they can be provided to the client application layer for messages.

Guess you like

Origin blog.51cto.com/14992191/2547378
Recommended