Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does RabbitMq ensure that messages are not lost

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to ensure that messages are not lost in RabbitMq". Friends who are interested may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to ensure that the message is not lost by RabbitMq"!

During the ① production phase, the producer creates a message and sends it to the rabbit server over the network.

In the ② message storage phase, it is first sent to the switch, and then through the routing algorithm, it arrives at the queue, waiting to be pulled and consumed.

In the ③ consumption phase, consumers pull messages from rabbit servers through the network to consume.

Messages may be lost in all three stages, which are analyzed below.

Message storage phase

Normally, we use the BasicPublish method to send a message to the switch and then route it to the queue, and before the consumer consumes it, what happens when the server restarts (the queue, the switch uses the default creation method)? The answer is: the message is lost. The reason is simple: messages are in memory and are not flushed, and they are non-persistent by default. After the service is restarted, they need to be recreated, and the message is naturally lost! Fortunately, Rabbit provides a persistence mechanism. When queues and switches are created, the durable property is set to true, and the message delivery mode (delivery mode) is set to 2, then the message is marked as persistent. This avoids the loss of server restart messages.

Sending phase

Since the publish operation does not return any information to the producer, how do you know if the server has persisted the persistent message to the hard disk? The server may go down before writing the message to disk, and the message is lost as a result!

Yes. )

Rabbit provides two solutions, transactions, but performance is greatly compromised and producer applications are synchronized. The production environment is generally not used; another option is the confirmation mode. It is also simple that the message is routed to all matching subscription queues and then told to the producer asynchronously. Use the channel.ConfirmSelect () method to turn on the channel acknowledgement mode. Then inject two callback functions, the ack and nack events. Channel.BasicAcks + = (sender, ev) = > {Console.WriteLine ("message has been received" + ev.DeliveryTag)

}

Channel.BasicNacks + = (sender, ev) = > {Console.WriteLine ("message not confirmed" + ev.DeliveryTag);}

Consumption stage

You might ask, how can consumer messages be lost? Rabbitmq provides automatic and manual acknowledgement of the message, which is then removed from the queue. If autoAck is true, automatic confirmation mode, the server will automatically dequeue the message after it is sent to the consumer. If the connection is broken for some reason, or if your consumer application fails, then the message will be lost!

By setting AutoAck to false, manually confirm and inform the server that the message has been processed and can be deleted out of the queue.

Channel.BasicConsume (queue: queueName, autoAck: false, consumer: consumer); consumer.Received + = (model, ea) = > {/ / dosometing channel.BasicAck (ea.DeliveryTag, false); / / confirm}

Summary: if you have done the above processing, then the news will not hide from you. There is a performance problem here. Message persistence will affect the delivery speed if it is brushed to disk, and message confirmation will also affect the message delivery speed. No, it can basically meet the demand. If the performance requirements are not met, other methods can be used, such as including the name of the reply queue every time a message is sent, so that the consumer can send back the reply to confirm acceptance. If the message reply does not arrive within a reasonable time range, the producer resends the message.

At this point, I believe you have a deeper understanding of "how to ensure that messages are not lost in RabbitMq". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report