0
0
RabbitMQdevops~7 mins

Reply-to queue pattern in RabbitMQ - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes, when one program sends a message to another, it needs an answer back. The reply-to queue pattern helps programs send a message and get a response easily by using a special temporary queue for replies.
When a client sends a request to a server and needs a direct answer.
When you want to avoid creating many permanent queues for each client.
When you want to keep communication simple and temporary between two programs.
When you want to handle multiple requests and responses without mixing them up.
When you want to use RabbitMQ to build a simple request-response system.
Config File - reply_to_example.py
reply_to_example.py
import pika
import uuid

class RpcClient:
    def __init__(self):
        self.connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
        self.channel = self.connection.channel()

        result = self.channel.queue_declare(queue='', exclusive=True)
        self.callback_queue = result.method.queue

        self.channel.basic_consume(
            queue=self.callback_queue,
            on_message_callback=self.on_response,
            auto_ack=True)

        self.response = None
        self.corr_id = None

    def on_response(self, ch, method, props, body):
        if self.corr_id == props.correlation_id:
            self.response = body

    def call(self, message):
        self.response = None
        self.corr_id = str(uuid.uuid4())
        self.channel.basic_publish(
            exchange='',
            routing_key='rpc_queue',
            properties=pika.BasicProperties(
                reply_to=self.callback_queue,
                correlation_id=self.corr_id),
            body=message)
        while self.response is None:
            self.connection.process_data_events()
        return self.response.decode()

rpc_client = RpcClient()
print("Sending request...")
response = rpc_client.call('Hello Server')
print(f"Received response: {response}")

This Python script uses RabbitMQ to send a message to a server and wait for a reply.

It creates a temporary queue for replies with queue_declare(queue='', exclusive=True).

The reply_to property tells the server where to send the answer.

The correlation_id helps match the reply to the request.

Commands
Run the Python script that sends a message to the server and waits for a reply using the reply-to queue pattern.
Terminal
python3 reply_to_example.py
Expected OutputExpected
Sending request... Received response: Hello Client
Check the current queues in RabbitMQ to see the temporary reply queue created by the client.
Terminal
rabbitmqctl list_queues
Expected OutputExpected
Listing queues ... rpc_queue 0 amq.gen-XYZ123 0
Key Concept

If you remember nothing else from this pattern, remember: the reply-to queue lets a client get a direct answer by telling the server where to send the reply.

Common Mistakes
Not setting the reply_to property in the message properties.
The server won't know where to send the reply, so the client never gets a response.
Always set the reply_to property to the client's temporary queue name.
Not using a unique correlation_id for each request.
Replies can get mixed up if multiple requests are sent, causing wrong responses to be matched.
Generate and set a unique correlation_id for each request and check it on reply.
Using a permanent queue for replies without cleaning it up.
This can cause message buildup and confusion between different clients.
Use exclusive, auto-delete temporary queues for replies.
Summary
Create a temporary exclusive queue for replies using queue_declare with queue=''.
Send a message with reply_to set to the temporary queue and a unique correlation_id.
Listen on the temporary queue and match replies using correlation_id to get the correct response.