How to Ensure the order in which consumers receive messages and the order in which consumers receive messages
Background
I was asked about how mq ensured the order of messages during the previous interview. At that time, I didn't answer well, and I couldn't find a satisfactory answer on the Internet. So I thought of
Problem description
Assume that A and B communicate through the message queue, and A sends two messages, m1 and m2. The order in which A sends messages is m1 and m2, and the result m2 first reaches the queue and m1 is followed. in the queue, if m2 is followed by m1, it is assumed that the two messages are dependent, it must be m1 first and then m2. Then B may receive m2 first and then m1. What should I do at this time?
My thoughts
1. The order in which B receives messages is irrelevant to the order in which A sends messages and the order in which messages are sent in the queue. The order in which messages arrive in the queue cannot be guaranteed. Similarly, the order in which messages are received after messages are delivered by the queue cannot be guaranteed. Therefore, I think that no matter how the consumer B is, it must ensure the correctness of the business. No matter how the producer sends messages or how the queue delivers messages, the consumer must realize that the received messages are unordered, the service must be properly handled to ensure normal business.
2. I think this is a common problem and must be considered as a consumer.
3. The order of messages is not important. It is important to ensure the final consistency of services.
Solution 1: local message table + scheduled scan
1. After receiving the message, save the message to the table in the local database and mark it as unprocessed.
2. If the processing conditions are met, directly process the message. After successful, mark the records in the table as processed and confirm the message to the queue.
3. If the processing conditions are not met
4. Periodically scan the local message table and repeat the unprocessed messages.
5. idempotence judgment is very important
Example
Assume that points are sent when you sign in.
Assuming that the sign-in and send points are separated, the logic of sign-in is to insert a record into the sign-in table, and the logic of sending points is to determine whether the sign-in record corresponding to the points exists, if yes, points are sent. Otherwise, no points are sent.
Assume that there are two messages in the message queue, one is the sign-in message, the other is the point message, and the point message carries the id of the sign-in record.
Therefore, when points are sent, it depends on whether there is a sign-in record. According to the above theory, the message can be saved locally and then periodically scanned.
~~~ The level is limited. I only want to think of this method for the time being. Thank you for your criticism !!!~~~