Try Again. Fail Again. Fail Better.

Coding snippets, experiments and moments of madness, to moments of clarity.

RabbitMQ

16th Jul 2014

For the past 6 months I’ve been working on setting up RabbitMQ for several tasks. As with anything it has it’s own share of seemingly tricky to navigate problems. I’m leaving these tips here as much for my own benefit later as for everyone else.

  • When no_ack is set to true for basic_consume in AMQP 0.9.1 at least; that means the server does not expect an acknowledgement to be sent from the consumer when it receives a task i.e. if your consumer can fail for whatever reason, and you want a task to be requeued, set no_ack to false.

  • One thing that plagued me for a while was seeing a PRECONDITION_FAILED message appear when a task failed on the consumer. Check your code, and then check it again to make sure that if you nack a task, that you don’t later ack it. Any blog post, or Stack Overflow article I ever saw mentioned acking a message twice, but nobody ever talked about nacking and then acking the same task.

  • If you want a task to be requeued when you nack it, be sure to set the requeue flag to true in basic_nack.

P.S. RabbitMQ has a fantastic set of tutorials for getting up and running, and for those running PHP, I’d highly recommend installing videlalvaro/php-amqplib with Composer.