Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does RabbitMQ decide when it is time to delete a message?

I am trying to understand the logic for message deletion in RabbitMQ.

My goal is to make messages persist even if there is not a client connected to read them, so that when clients reconnect the messages are waiting for them. I can use durable, lazy queues so that messages are persisted to disk, and I can use HA replication to ensure that multiple nodes get a copy of all queued messages.

I want to have messages go to two or more queues, using topic or header routing, and have one or more clients reading each queue.

I have two queues, A and B, fed by a header exchange. Queue A gets all messages. Queue B gets only messages with the "archive" header. Queue A has 3 consumers reading. Queue B has 1 consumer. If the consumer of B dies, but the consumers of A continue acknowledging messages, will RabbitMQ delete the messages or continue to store them? Queue B will not have anyone consuming it until B is restarted, and I want the messages to remain available for later consumption.

I have read a bunch of documentation so far, but still have not found a clear answer to this.

like image 875
Mnebuerquo Avatar asked Jan 03 '23 19:01

Mnebuerquo


1 Answers

RabbitMQ will decide when to delete the messages upon acknowledgement.

Let's say you have a message sender:

var factory = new ConnectionFactory() { HostName = "localhost", Port = 5672, UserName = "guest", Password = "guest" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
    channel.QueueDeclare(queue: "hello",
                         durable: true,
                         exclusive: false,
                         autoDelete: false,
                         arguments: null);

    string message = "Hello World!";
    var body = Encoding.UTF8.GetBytes(message);

    channel.BasicPublish(exchange: "",
                         routingKey: "hello",
                         basicProperties: null,
                         body: body);
    Console.WriteLine(" [x] Sent {0}", message);
}

This will create a durable queue "hello" and send the message "Hello World!" to it. This is what the queue would look like after sending one message to it.

enter image description here

Now let's set up two consumers, one that acknowledges the message was received and one that doesn't.

channel.BasicConsume(queue: "hello",
                    autoAck: false,
                    consumer: consumer);

and

channel.BasicConsume(queue: "hello",
                    autoAck: true,
                    consumer: consumer);

If you only run the first consumer, the message will never be deleted from the queue, because the consumer states that the messages will only disappear from the queue if the client manually acknowledges them: https://www.rabbitmq.com/confirms.html

The second consumer however will tell the queue that it can safely delete all the messages it received, automatically/immediately.

If you don't want to automatically delete these messages, you must disable autoAck and do some manual acknowledgement using the documentation:

http://codingvision.net/tips-and-tricks/c-send-data-between-processes-w-memory-mapped-file (Scroll down to "Manual Acknowledgement").

channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
like image 81
Rafael Avatar answered Jan 30 '23 01:01

Rafael