Interviewer: How to design RabbitMQ expiration time setting, dead letter queue, and delay queue?

hello! Hello everyone, I'm Xiaoqi, an unreliable programmer.
Xiaoqi intends to share some techniques in a relaxed and humorous way. If you think you have learned something through Xiaoqi's article, then give Xiaoqi a like.
The article is continuously updated, you can search for [Xiaoqi JAVA Interview] on WeChat to read it as soon as possible, and reply to [Information] and there are more benefits I have prepared for you!

I. Introduction

We often use RabbitMQ , but it has many advanced features that we also need to master to deal with complex business logic in real scenarios.

2. Interview

Interviewer: Xiaoqi, right, let's start the interview

Me: Come on, I'm already hungry and thirsty

insert image description here

Interviewer: Have you used RabbitMQ?

me: used

3. What if RabbitMQ sends a message for a long time and no one handles it and expires?

Interviewer: What if RabbitMQ sends a message for a long time and no one handles it and expires?

Me: The message has not been processed for a long time and expired. In order not to affect the normal use of the exchange, we can put the expired message in a dead letter exchange , and then bind a queue to this exchange, which is the dead letter queue , and then pass a special Consumers to process these expired messages

insert image description here

Interviewer: Will the message enter the dead letter exchange only if the message reaches the expiration time and is not consumed? Is there any other situation?

Me: There are three situations in which a message becomes a dead letter.
1: The queue message length reaches the limit.
2: The consumer rejects the message and does not put the message in the original queue for him to resend.
3: There is a message expiration setting in the original queue, and the message has not been consumed after the timeout period.

Interviewer: How do we define a dead letter exchange bound to a dead letter queue?

Me: We can configure the name of the dead letter switch and the name of the routing key between the dead letter switch and the dead letter queue through the two parameters x-dead-letter-exchange and x-dead-letter-routing-key .

insert image description here

4. What is TTL?

Interviewer: What is TTL?

Me: The full name of TTL is Time To Live (time to live/expiration time), which is used to set the expiration time in mq.

Interviewer: How do we generally set the expiration time, and what methods do we use?

Me: We have two ways to set the expiration time, one is to set the expiration time for a single message, and the other is to set the expiration time for a queue, so that the messages sent to the queue follow that expiration time.

1: The queue sets the expiration time: x-message-ttl, unit: ms (milliseconds), so that all messages entering the queue follow this expiration time.

insert image description here

2: Set the expiration time for a single message: expiration, unit: ms (milliseconds), so that only this message has an expiration time.

insert image description here

Interviewer: What if I set a time for a single message and a time for the queue?

Me: In that case, according to the shortest time.

5. How to design the RabbitMQ delay queue?

Interviewer: Then do you know the delay queue, what kind of scenarios are it generally used for?

Me: I know, the delay queue is generally after the message is sent to the MQ, and the consumer does not need to process it immediately. For example, when we order takeout, we successfully place the order. At this time, the inventory has been reduced, but we have not paid yet. If we never pay, the order is automatically cancelled after 30 minutes and the stock is restored.

Interviewer: Does RabbitMQ have a delay queue? How would you design a delay queue yourself?

Me: RabbitMQ does not provide the function of delay queue, but we can use TTL + dead letter queue to realize the function of delay queue.

insert image description here

Interviewer: Yes, boy, you have a good grasp of this part.

Me: It's ok, it's all daily accumulation

Interviewer: The guy is really good, RabbitMQ is very good, you passed the interview, you can start your job tomorrow

Me: Ah, are you in such a hurry, I still have a lot of things to talk about later.

Interviewer: Don't worry, I will listen to you slowly after you come in, and let you talk to me after working overtime

me. . . It's too hard

6. Summary

Here about RabbitMQ has not been sorted out, the article will continue to be updated later, it is recommended to collect.

The commands involved in the article must be knocked several times like me. Only in the process of knocking can you find out whether you really master the commands.

If you think my article is not bad, please like it. In addition, you can search [Xiaoqi JAVA Interview] on WeChat to read more good articles and get the information I have prepared for you.

Guess you like

Origin blog.csdn.net/weixin_44096133/article/details/124136055