rabbitmqqueuemessagebroker

At what point to create queues in messages broker?


At what point is it customary to create queues in message brokers? For example, I have a RabbitMQ in my application, and accordingly I need to create queues before launching it.

I'm still thinking about an approach similar to database migration, i.e. creating scripts that need to be run before the application, but I haven't found any auxiliary tools for this.


Solution

  • How to Create Queues in RabbitMQ

    In RabbitMQ, it is a common practice to create queues in code, just when you need them.

    I believe most client libraries offer the possibility of creating a queue idempotently. For example, when creating a queue consumer, you might say, "consume from this queue and create it if it doesn't exist." Here is an example in Java:

    Channel channel = connection.createChannel();
    channel.queueDeclare("queue name", true, false, false, null); //here is the "create if does not exist" part
    channel.queueBind("queue name", "exchange name", "routing");
    

    From my experience, this is a very efficient way to manage queues in RabbitMQ.

    The question arises as to when and where you should create this queue. It all depends on your needs. There are essentially three approaches:

    Creating a Queue Only by a Consumer

    If you opt to create a queue by the consumer, you ensure that the queue is there when someone needs to consume from it.

    The drawback is that if you produce a message to that queue before any consumer has ever existed (assuming the queue is durable), then the message will be lost. Whether this is an issue depends on your use case.

    Creating a Queue by Both a Consumer and a Producer

    If you opt to "create if not exists" a queue both at the producer and the consumer ends, you ensure that the message never gets lost, even if no consumer ever existed for this queue. The drawback is that the queue definition needs to be consistent across both producer and consumer. For example, if you define a queue in the producer with the same name but different parameters (such as durability, arguments, etc.), you will end up with an exception because you cannot define a queue with the same name but different parameters.

    Creating a Queue Only by a Producer

    This is an option I wouldn’t recommend, but again, it depends on your use case. In this scenario, you ensure that no message gets lost when produced, but you need to make sure that the producer is created before any consumer exists. Otherwise, you will face an exception because a consumer cannot consume from a non-existent queue, nor can it create one.

    What I would not recommend

    I attempted to manually manage RabbitMQ queues, drawing from my experience with JMS queues in WLS, so it was my default thought. However, I found this method to be extremely tedious, time-consuming, and prone to errors.