One, foreword

MQ is now mostly used to do the heterogeneity of the system, to do the decoupling of the system, the module of the system is the sender and receiver, MQ plays the role of the post office. As a relay role, you need to ensure 100% delivery of messages.

Today we’ll look at how to ensure 100% delivery of messages. Compiled a Java interview treasure book complete PDF

Two, the RabbitMQ features

What RabbitMQ ensures is that as soon as you send a message to the Broker, I guarantee that the message will reach the consumer.

Of course, there are preconditions, such as:

  1. You need to do a manual answer,

  2. At least the Broker does not hang, the message is persisted, and so on.

To ensure reliability, use RabbitMQ to send messages to the Broker.

  1. The message was sent successfully

  2. Ensure that the Broker successfully received the message

  3. The producer received an acknowledgement from the Broker

  4. Message compensation mechanism, the current three steps all kneel, make a compensation retransmission mechanism

  5. As a final barrier, if the number of retries in step 4 is too high, then there is a problem with the system and we need human intervention

By doing these five steps, we can basically guarantee that 100 percent of the messages are delivered. In addition, follow the public number of Java technology stack, in the background reply: interview, you can get my RabbitMQ series of interview questions and answers, very complete.

Iii. Reliability guarantee of delivery by producers

3.1 First, a schematic diagram

Add a description

3.2 With regard to the above diagrams, we will analyze them step by step

  • Step1: data warehousing, this step is necessary

  • Step2: Drop the message into the database and initialize its state to 0 (sending)

  • Step3: post messages to the Broker

  • Step4: Broker sends a successful reply

  • Step5: The producer receives a successful response and changes the message status to 1: (The message is sent successfully) The above is a normal process, and the following is the solution mechanism if there is an anomaly:

  • Step6: periodically check whether the status of the message is 1

  • Step7: if the status of the message of step6 is still 0, it enters resending and repeats step1-step5 above

  • Step8: If the message redevelops to a certain number of times, then manual access processing, because at this time, the description may be a message

The above scheme seems to be perfect, but consider that in step4, if a network problem occurs when the Broker sends a reply and the message does not reach the producer, the whole process will enter the compensation process. Then there will be two messages in the Broker, that is, the problem of repeated delivery. So we’re going to deal with this problem on the consumer side.

Iv. Idempotency of the consumption end:

4.1 Causes of the need to solve idempotent

  • The Broker sent a reply message that did not reach the producer

  • When the consumer sends a reply, the consumer hangs up

4.2 For the above resolution of our mechanism

Since all of the above messages have unique identifiers, we only need to look up the identifiers of the corresponding messages to determine their status.

4.3 Construct a uniquely identified solution

First of all, explain the different scenarios for different scenarios. Don’t start by saying that tens of thousands of concurrent applications are ok. Take your time and work your way up here.

4.3.1 Adding an ID to a Database
Advantages:

1) : can not be simpler, in the case of small concurrency can be acceptable

2) : Helps with paging and sorting

Disadvantages:

1) : it is not applicable in the case of sub-database sub-table and read/write separation with multiple live slave

2) : When the performance is not up to the requirements, it is not conducive to expansion

3) : Different database syntax implementation is different

4.3.2 Use Redis to generate ID
Advantages:

1) : Redis single thread can generate globally unique ids

2) : Redis clustering can be used for higher throughput

Disadvantages:

1) : Introduce new components to increase system complexity

2) : Need to pay attention to dealing with concurrency issues

4.3.3 Open Source Snowflake with Twitter

Snowflake is an open-source distributed ID generation algorithm for Twitter. The result is a long ID that uses 41bit as the number of milliseconds and 10bit as the machine ID (5 bits for the data center, The 5 bits are machine ids.) the 12 bits are used as serial numbers in milliseconds (each node can produce 2^12 = 4096 ids per millisecond) and the last one is a sign bit.

Advantages:

Does not rely on database and other middleware, and the performance is fair

Disadvantages:

It is time dependent. If the weightlessness of each machine is not synchronized, it will not increase globally. Compiled a Java interview treasure book complete PDF