The RabbitMQ interview questions

1. What is RabbitMQ

2. Why rabbitMQ

3. Rabbitmq scenarios

4. How do I ensure that messages are sent to RabbitMQ correctly? How do I ensure that message recipients consume messages?

5. How to avoid repeated message delivery or consumption?

6. What transport is the message based on?

7. How are messages distributed?

8. How to route messages?

9. How to ensure that messages are not lost?

10. What are the benefits of RabbitMQ?

11. Cluster for RabbitMQ

12. Disadvantages of MQ

1. What is RabbitMQ

A message queue technology using AMQP advanced message queue protocol, the biggest characteristic is that consumption does not need to ensure the existence of providers, to achieve a high degree of decoupling between services

2. Why rabbitMQ

(1) In the distributed system with asynchronous, peak clipping, load balancing and a series of advanced functions;

(2) With persistence mechanism, process messages, queue information can also be saved.

(3) Realize the decoupling between consumers and producers.

(4) For high concurrency scenarios, the use of message queue can make synchronous access into serial access to achieve a certain amount of flow limiting, which is conducive to the operation of the database.

(5) Message queue can be used to achieve the effect of asynchronous ordering, queuing, the background logical ordering.

3. Rabbitmq scenarios

(1) Asynchronous communication between services

(2) Sequential consumption

(3) Scheduled tasks

(4) Request peak cutting

4. How do I ensure that messages are sent to RabbitMQ correctly? How do I ensure that message recipients consume messages?

Sender confirmation mode

Setting the channel to Confirm mode assigns a unique ID to all messages posted on the channel.

Once a message has been posted to the destination queue or written to disk (persistable messages), the channel sends an acknowledgement to the producer (containing the unique ID of the message).

If RabbitMQ encounters an internal error that causes the message to be lost, a NACK (Notacknowledged, unacknowledged) message is sent.

The sender confirmation pattern is asynchronous, and producer applications can continue sending messages while waiting for confirmation. When the acknowledgement message arrives at the producer application, the producer application’s callback method is triggered to process the acknowledgement message.



Recipient confirmation mechanism

Each message received by the consumer must be acknowledged (message receipt and message acknowledgement are two different operations). RabbitMQ can safely remove a message from the queue only if the consumer confirms it.

The timeout mechanism is not used and RabbitMQ only confirms that the Consumer needs to resend the message if the connection is broken. That is, RabbitMQ gives the Consumer enough time to process the message as long as the connection is not broken. Ensure final consistency of data;

Some special cases are listed below

(1) If a consumer receives a message and disconnects or unsubscribs before confirming it, RabbitMQ will assume that the message has not been distributed and redistribute it to the next subscriber. (There may be hidden danger of repeated message consumption, which needs to be removed)

(1) 2 If a consumer receives a message without an acknowledgement and the connection is not broken, RabbitMQ will assume the consumer is busy and will not send any more messages to the consumer.

5. How to avoid repeated message delivery or consumption?

During message production, MQ internally generates an inner-msG-ID for each message sent by the producer as a basis for de-duplication (message delivery failure and retransmission) to avoid duplicate messages entering the queue. In message consumption, there must be a bizId (globally unique for the same business, such as payment ID, order ID, post ID, etc.) in the message body as the basis for deduplication to avoid repeated consumption of the same message.

6. What transport is the message based on?

The creation and destruction of TCP connections is expensive and the number of concurrent connections is limited by system resources, resulting in performance bottlenecks. RabbitMQ uses channels to transmit data. A channel is a virtual connection established within a real TCP connection, and there is no limit on the number of channels on each TCP connection.

7. How are messages distributed?

If at least one consumer subscribes to the queue, the message is sent to the consumer in a round-robin fashion. Each message is distributed to only one subscribing consumer (provided the consumer can process the message and confirm it properly). Multiple consumption can be realized through routing

8. How to route messages?

When the message provider -> route -> one or more queue messages are published to the exchange, the message will have a routing key, which is set when the message is created. Queues can be bound to switches through queue routing keys. When the message arrives at the exchange, RabbitMQ matches the routing key of the message with the routing key of the queue (different routing rules apply to different switches).

Commonly used switches are divided into the following three types:

Fanout: If the switch receives a message, it broadcasts it to all bound queues

Direct: If the routing keys match exactly, the message is delivered to the corresponding queue

Topic: Enables messages from different sources to reach the same queue. When using topic switches, you can use wildcards

9. How to ensure that messages are not lost?

Message persistence, of course, if the queue must be persistent

RabbitMQ ensures that persistent messages can be recovered from a server restart by writing them to a persistent log file on disk. When a persistent message is posted to the persistent exchange, Rabbit will send a response only after the message is committed to the log file. Once a consumer consumes a persistent message from the persistent queue, RabbitMQ marks it in the persistence log as waiting for garbage collection. If RabbitMQ restarts persistent messages before they are consumed, Rabbit automatically reconstructs the exchange and queues (and bindings) and republishes the messages from the persistence log file to the appropriate queues.

10. What are the benefits of RabbitMQ?

(1) Highly decoupled services

(2) High performance of asynchronous communication

(3) Flow peak cutting

11. Cluster for RabbitMQ

Mirroring cluster mode

The queue that you create, both the metadata and the messages in the queue are going to live on multiple instances, and then every time you write a message to the queue, it’s going to automatically synchronize messages to the queues of multiple instances.

The good thing is, if one of your machines goes down, you can use the other machines. The disadvantages are, first, the performance overhead is too high, the message synchronizes all machines, resulting in the network bandwidth pressure and consumption is heavy! Second, it’s not scalable, because if you add machines to a queue that’s heavily loaded, you add machines that also contain all the data in that queue, there’s no way to linearly scale your queue



12. Disadvantages of MQ

(1) Decreased system availability

The more external dependencies the system introduces, the more likely it will fail. Originally, you are system A calling BCD interface of three systems, but ABCD interface of four systems is fine, there is no problem, you prefer to add MQ to it, what if MQ fails? MQ goes down, the whole system goes down, you’re done.

(2) Improved system complexity

By adding MQ, how can you ensure that messages are not consumed twice? How to handle message loss? How to ensure sequential message delivery? Big head, lots of problems, lots of pain

(3) Consistency

A system processing directly return success, people think you this request is successful; But the problem is, what if BCD three systems, BD two system write library success, the result of C system write library failure? Your numbers don’t match up.

So message queuing is actually a very complex architecture, and you introduce it with a lot of benefits, but you have to do all kinds of additional technical solutions and architectures to get around it, and at the end of the day, you get, oh, my god, an order of magnitude more complex, maybe 10 times more complex. But when the chips are down, it’s — it’s still there.

Welcome everyone to pay attention to my public account [programmer Chase wind], 2019 many companies Java interview questions sorted out more than 1000 400 pages of PDF documents, articles will be updated in it, sorted information will also be placed in it.



The last

Welcome everyone to exchange, like the article remember to pay attention to my like yo, thank you for your support!