Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How reliable is BasicPublish()?

Tags:

c#

queue

rabbitmq

Background

I'm using RabbitMQ with C#. I have a remote queue with one publisher and one consumer. This is how I enqueue a message into the queue (publisher):

model.BasicPublish(exchange, routingKey, basicProperties, body);

I just figured out that this call is always a success (success = no error reported, exceptions). For example, even if I not connected to the network (the queue is unreachable) - it still passes this line and I don't know about it.

The Question:

In a complex system, reliability is above all. So how I can be sure that the message is enqueued into the queue?

I think that must be a mechanism to:

  1. Retry message queue insertion in case of retry
  2. A reporting system that will give a report before the producer will drop the message (won't be tried again).

Possible solution

I saw here that I can implement something like transaction, which makes the call atomic and also reports in case of error. For me, it looks like "too big hammer". Transactions have large effect on performance.

like image 203
No1Lives4Ever Avatar asked Sep 15 '26 23:09

No1Lives4Ever


1 Answers

OK. Found the solution.

About possible solution #1:

Taken from RabbitMQ documentation (source):

Using standard AMQP 0-9-1, the only way to guarantee that a message isn't lost is by using transactions -- make the channel transactional then for each message or set of messages publish, commit. In this case, transactions are unnecessarily heavyweight and decrease throughput by a factor of 250. To remedy this, a confirmation mechanism was introduced. It mimics the consumer acknowledgements mechanism already present in the protocol.

According to this article, the right solution is to use "Publisher confirms". Based on this answer, all I have to do is:

channel.BasicPublish(QUEUE_NAME, QUEUE_NAME, messageProperties, payload);
channel.WaitForConfirmsOrDie();
like image 186
No1Lives4Ever Avatar answered Sep 18 '26 11:09

No1Lives4Ever



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!