3

I'm developing an application for messaging using Rabbit MQ. I use explicit ACK:

model.BasicConsume(queueName,false, consumer);

and do ACK after processing message:

consumer.Received += (ch, ea) =>
                {
                    try
                    {                        
                        var message = Encoding.UTF8.GetString(ea.Body);

                        Logger.Info($"DeliveryTag={ea.DeliveryTag}, message={message}");

                        ((EventingBasicConsumer)ch).Model.BasicAck(ea.DeliveryTag, false);

                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                        throw;
                    }
                };

The problem is when there is an error of processing a message and Rabbit doesn't receive ACK, it returns the message to the queue in different order.

Fore example there are messages M1, M2, M3, M4.

If M2 was returned to queue, it will be M3, M4, M2.

Is there any way to keep the order of delivery?

P.S. I have only one consumer and RabbitMQ 3.6.6, but I still have problem of reordering.

Stas Ivanov
  • 1,173
  • 1
  • 14
  • 24
Timur Lemeshko
  • 2,747
  • 5
  • 27
  • 39

1 Answers1

3

Everything is answered here, under Message ordering guarantees. I'll just quote

Messages can be returned to the queue using AMQP methods that feature a requeue parameter (basic.recover, basic.reject and basic.nack), or due to a channel closing while holding unacknowledged messages. Any of these scenarios caused messages to be requeued at the back of the queue for RabbitMQ releases earlier than 2.7.0. From RabbitMQ release 2.7.0, messages are always held in the queue in publication order, even in the presence of requeueing or channel closure.

With release 2.7.0 and later it is still possible for individual consumers to observe messages out of order if the queue has multiple subscribers. This is due to the actions of other subscribers who may requeue messages. From the perspective of the queue the messages are always held in the publication order.

cantSleepNow
  • 9,691
  • 5
  • 31
  • 42
  • Yes, and as i wrote: "I have only one consumer and RabbitMQ 3.6.6, but i still have problem of reordering" – Timur Lemeshko Feb 15 '17 at 11:12
  • did you get this resolved? we have the same issue. We want a failed message to keep hitting the front of the queue preventing others being processed until that one is resolved – user2047485 Oct 16 '18 at 08:06