Site icon Tutexchange

Understanding RabbitMQ Queue Basics

Advertisements

A queue is a storage mechanism in RabbitMQ where messages from exchanges are held until a receiver consumes them.

How Queues Work

  1. Binding to an Exchange: Queues are connected to exchanges through bindings, which define the rules for how messages from an exchange are routed to the queue. These rules can involve routing keys or more complex patterns.
  2. Message Storage: Once messages are routed from an exchange, they are stored in the queue. If no queue is bound to an exchange or the message doesn’t match any binding rules, the message may be dropped.
  3. Consumption: Messages in the queue are delivered to consumers (applications or services) that are subscribed to it. A consumer can:
    • Acknowledge (ACK) a message, signalling successful processing.
    • Reject (NACK) or not acknowledge, leading the message to be requeued or dropped, depending on the settings.

Benefits of Using Queues in RabbitMQ

Configure Queue

Queue Name

The queue name is a unique identifier for a queue that can be referenced in the application. It must adhere to the following rules:

Durability

Durability is a property of a queue that determines whether messages in the queue can survive server (broker) restarts. There are two durability options:

  1. Durable
    • If a queue is marked as durable, it will persist across server restarts.
    • This ensures that the queue remains intact, along with its metadata, even after the broker restarts.
  2. Transient (Non-Durable)
    • If a queue is marked as transient, it will not survive server restarts.
    • Such queues are temporary and are typically used for short-lived or less critical messaging scenarios.

Auto-Delete

Auto-delete is a property of a queue that determines whether it is automatically deleted under specific conditions.

Options

  1. Yes: The queue will be auto-deleted.
  2. No: The queue will persist and will not be automatically deleted.

If the queue is exclusive, the durability attribute becomes irrelevant since the queue will be deleted when the client disconnects or its connection is lost. Auto-deleted queues are removed when:

If the queue never had a consumer, it will not be deleted.

Arguments

Optional parameters that can modify the behaviour of the queue or enable additional features. These arguments are typically passed during queue declaration.

Common Example

Message TTL (Time-To-Live)

{ "x-message-ttl": 60000 } // Messages live for 60 seconds

Queue Length Limit

{ "x-max-length": 1000 } // Maximum 1000 messages

Queue Overflow Policy

Determines what happens when the queue reaches its length limit.Options:

{ "x-overflow": "reject-publish" }

Dead-Letter Exchange (DLX)

Specifies an exchange to which messages are redirected when they cannot be delivered (e.g., expired, rejected, or undeliverable messages).

{ "x-dead-letter-exchange": "my-dlx" }

Queue TTL

{ "x-expires": 300000 } // Queue expires after 5 minutes

Creating Queue

To create a queue, click on the “Queues” tab in the web management plugin.

First, we will start by naming the queue “test-queue.” Then, set the durability to “Durable.” Finally, set the “Auto Delete” option to “No” and click the “Add queue” button to create the queue.

Go to the Queues Tab
After logging in, click on the “Queues” tab in the top navigation bar.

After adding a new queue in the RabbitMQ Web Management UI, you can easily find and view the queue you just created. It will be listed just above the “Add queue” panel.

Referenced from:- https://www.rabbitmq.com/

👉Read the Full Article on exchange here: https://tutexchange.com/types-of-exchanges-in-rabbitmq-explained/

👉Read the Full Article on bindings here: https://tutexchange.com/mastering-rabbitmq-bindings-a-simple-guide-to-linking-exchanges-and-queues/

Exit mobile version