Interviewer: How does RabbitMQ implement current limiting on the consumer side?

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

RabbitMQ has many advanced features, which are not used in general projects, but there are always interviewers who will ask. When asked, we have to pretend that these are trivial to us.

insert image description here

2. Interview

Interviewer: Xiaoqi, right? Let me introduce myself first.

Me: Hello, interviewer, I graduated from XXX, I worked in XXX before, I am proficient in XXX, in a word, I am excellent, you can ask.

insert image description here

Interviewer: Okay, your self-introduction is very tough, then let me see if you have any strong skills

Me: It's a mule or a horse to come out and yo

insert image description here

3. How RabbitMQ implements current limiting on the consumer side

Interviewer: Okay, then let me ask you a scenario question first. For example, our company now has a seckill system. On the day of the event, users placed too many orders, causing the consumer side to crash. How should we deal with this situation?

Me: It’s easy to say, buy a few more servers, build a few more consumer-side services, and reduce the pressure on the server-side, so there will be no problems.

Interviewer: Uh. . . You can really solve it by saying that, it is true that our boss is very stingy. . . Reluctant to buy more servers, just want us to do it with one server

I:? ? ? Just let the horses run and not let the horses eat grass, that's fine, tell your boss, let him come out to withstand the pressure of the consumer side, isn't he reluctant to spend money, let him wait for the spike Going to the user to make trouble, so that the user can't place an order at the same time.

Interviewer: Oh no, we also thought about making trouble for the user, but the boss wouldn't let him. The boss said that the user must be able to complete the order in the shortest possible time.

Me: Yes, then use MQ, let the client place the order and put the order information into the message queue, and then let the consumer process it.

Interviewer: Oops, that's what we do, but the consumer side always crashes

Me: How is your consumer server configuration?

Interviewer: Hmm. . The consumer server configuration is half-core CPU, 1G running memory space, and 10G disk space

me: half core? Why are you kidding me, and the other half of the CPU was taken away by others?

Interviewer: Hey, the situation is like this, the thing is like this, anyway, the company is poor, I will tell you slowly when you come in, you can help me think of a solution now, I will use it to deal with it boss

Me: Then implement current limiting on the consumer side.

Interviewer: How to achieve it?

Me: Use the channel.basicQos(int prefetchSize, int prefetchCount, boolean global) method to set the current limiting configuration.

prefetchSize: Indicates the size of the message (0 means unlimited size)
prefetchCount: Indicates the number of messages
global: true means that all consumers under the channel apply this policy, while false means that only the current consumer applies this policy.

insert image description here

As shown in the figure, here we channel.basicQos(0,1,false); means that the size of the message is not limited, but the number of messages is limited, and only one message can be sent to the consumer at a time.

Interviewer: Why would the consumer end hang up if you don't limit the flow?

Me: If Xiaoming's mother has 10 dumplings for Xiaoming to eat, Xiaoming calls 9 classmates, and then there are 10 of them, each one of them eats one dumpling without any problem, but today Xiaoming is alone, always Can't put 10 dumplings into Xiaoming's mouth at once, this will directly choke Xiaoming to death.

Interviewer: So how do we solve it?

Me: Now Xiaoming eats one dumpling first, and when Xiaoming finishes eating this one dumpling, tell his mother that I have finished eating, and then his mother will give him another dumpling to eat. In this way, he can also eat 10 dumplings, and Xiaoming does not have any. thing.

Interviewer: I see what the current limiting parameters and data size are for

Me: If Xiaoming’s mother now makes Xiaoming eat ten dumplings in one bite, he wraps a very large dumpling weighing a pound. At this time, she tells Xiaoming that it is still a dumpling, and one bite is boring, and Xiaoming will still choke to death at this time. , so the data size is also limited.

Interviewer: How does the queue know that the consumer has finished consuming a message and wants to send him another message?

We need to configure the consumer to manually confirm the message. When the consumer finishes consuming the message and confirms the message manually, the queue considers that the entire consumption process is over and starts to send the next message.

Enable manual confirmation of configuration

insert image description here

Manual confirmation in the code.

insert image description here

There are two parameters for manual confirmation here. The first is the tag number, which is a number of the message. The second parameter is whether to confirm multiple messages. If it is true, it will confirm multiple messages. If it is false, it will only confirm this message. Generally, We are all false.

Interviewer: Okay, boy, there is something

Me: Please don't be obsessed with brother, brother is just a legend. . .

Interviewer: The young man is really amazing. He explained the current limit of the RabbitMQ consumer side at once. You passed the interview and 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

4. 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/124078663