Several commonly used interview questions in RabbitMQ

The following opinions are only a summary of personal understanding. If there are any errors or omissions, please correct me!

---------------------------------------------------------------------------------------------------------------------

1. What is RabbitMQ? Why use RabbitMQ?

Answer: RabbitMQ is an open source, written in Erlang, based on AMQP protocol, messaging middleware;

You can use it to: decoupling, asynchronous, peak clipping.

 

2. What are the advantages and disadvantages of RabbitMQ?

Answer: Advantages: decoupling, asynchronous, peak clipping;

Disadvantages: Reduce the stability of the system: Originally the system was running well, but now you have to join a message queue to go in, then the message queue is down, and your system is not huh. Therefore, system availability will be reduced;

Increasing the complexity of the system: after joining the message queue, many issues must be considered, such as consistency issues, how to ensure that messages are not repeatedly consumed, and how to ensure reliable transmission of messages. Therefore, there are more things to consider and the complexity increases.

 

3. How to ensure the high availability of RabbitMQ?

Answer: No project will only use one RabbitMQ server to provide services, which is too risky;

 

4. How to ensure that RabbitMQ is not repeatedly consumed?

Answer: Let me talk about why repeated consumption: Under normal circumstances, when consumers consume messages, after consumption, they will send a confirmation message to the message queue. The message queue will know that the message has been consumed, and the message will be sent from Delete from the message queue;

However, due to network transmission and other failures, the confirmation information is not transmitted to the message queue, which causes the message queue to not know that it has consumed the message, and distributes the message to other consumers again.

In view of the above problems, a solution is: to ensure the uniqueness of the message, even if it is transmitted multiple times, do not let the multiple consumption of the message affect it; ensure that the message is idempotent;

For example: the data written into the message queue is uniquely marked, and when the message is consumed, it is judged whether it has been consumed according to the unique identifier;

 

5. How to ensure the reliable transmission of RabbitMQ messages?

Answer: The unreliable information may be due to loss of information, hijacking, etc.;

Loss is divided into: producer lost message, message list lost message, consumer lost message;

 

Producers lose messages: From the perspective of producers losing data, RabbitMQ provides transaction and confirm modes to ensure that producers do not lose messages;

The transaction mechanism means: before sending the message, start the transaction (channel.txSelect()), and then send the message. If there is any exception during the sending process, the transaction will roll back (channel.txRollback()), and if the sending is successful, the transaction will be submitted (Channel.txCommit()). However, this approach has a disadvantage: throughput decreases;

The confirm mode is mostly used: once the channel enters the confirm mode, all messages published on the channel will be assigned a unique ID (starting from 1), once the message is delivered to all matching queues;

rabbitMQ will send an ACK to the producer (including the unique ID of the message), which makes the producer know that the message has arrived at the destination queue correctly;

If rabbitMQ fails to process the message, a Nack message will be sent to you, and you can retry the operation.

 

Message queue lost data: message persistence.

To deal with the case of data loss in the message queue, it is generally to enable the persistent disk configuration.

This persistence configuration can be used in conjunction with the confirm mechanism. You can send an Ack signal to the producer after the message is persisted to the disk.

In this way, if rabbitMQ dies before the message is persisted to the disk, the producer will not receive the Ack signal and the producer will automatically resend it.

So how to persist?

By the way, it's actually very easy, just the following two steps

  1. Set the persistent flag of queue durable to true, which means it is a persistent queue
  2. Set deliveryMode=2 when sending a message

After this setting, even if rabbitMQ hangs, the data can be restored after restart

 

Consumers lose messages: Consumers usually lose data because they use the automatic message confirmation mode, so you can change to manual confirmation!

After the consumer receives the message, before processing the message, it will automatically reply that RabbitMQ has received the message;

If processing the message fails at this time, the message will be lost;

Solution: After processing the message successfully, reply the confirmation message manually.

 

6. How to ensure the order of RabbitMQ messages?

Answer: Single-threaded consumption guarantees the order of messages; the messages are numbered, and the consumer processes the messages according to the number;

Guess you like

Origin blog.csdn.net/suixinsuoyu12519/article/details/110946031