Request-reply pattern in RabbitMQ - Commands & Configuration
import pika connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() channel.queue_declare(queue='rpc_queue') print('RPC queue declared and ready') connection.close()
This Python script connects to RabbitMQ on localhost and declares a queue named rpc_queue. This queue will receive request messages. Declaring the queue ensures it exists before sending or receiving messages.
python request_reply_setup.py
rabbitmqctl list_queues
python rpc_server.py
python rpc_client.py
If you remember nothing else from this pattern, remember: the client sends a request message with a unique reply queue and waits for the server to send the answer back to that queue.
Practice
What is the main purpose of the reply_to property in RabbitMQ's request-reply pattern?
Solution
Step 1: Understand the role of
Thereply_toreply_toproperty specifies the queue where the reply should be sent.Step 2: Differentiate from other properties
Unlike sender identification or encryption,reply_todirects the response path.Final Answer:
It tells the receiver where to send the reply message. -> Option BQuick Check:
reply_to= reply destination [OK]
- Confusing reply_to with sender ID
- Thinking reply_to sets message priority
- Assuming reply_to encrypts messages
Which of the following is the correct way to set the correlation_id in a RabbitMQ message using Python pika library?
channel.basic_publish(exchange='', routing_key='rpc_queue', properties=..., body='Hello')
Solution
Step 1: Recall correct property name
The correct property name for correlation iscorrelation_idin pika.BasicProperties.Step 2: Match syntax exactly
properties=pika.BasicProperties(correlation_id='12345') uses the exact correct syntax:correlation_id='12345'.Final Answer:
properties=pika.BasicProperties(correlation_id='12345') -> Option DQuick Check:
Correct property name = correlation_id [OK]
- Using incorrect property names like correlation or corr_id
- Capitalizing property names incorrectly
- Omitting properties argument
Given this Python code snippet using pika for request-reply, what will be printed?
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='rpc_queue')
corr_id = 'abc123'
channel.basic_publish(
exchange='',
routing_key='rpc_queue',
properties=pika.BasicProperties(reply_to='reply_queue', correlation_id=corr_id),
body='Request')
print(corr_id)Solution
Step 1: Identify what is printed
The code prints the variablecorr_idwhich is set to 'abc123'.Step 2: Understand message sending vs printing
Sending a message does not affect the print statement; it prints the variable value.Final Answer:
abc123 -> Option AQuick Check:
Printed variable = corr_id = abc123 [OK]
- Confusing printed output with message body
- Expecting error due to missing reply handler
- Thinking reply_to value is printed
In a request-reply setup, a developer notices replies never arrive. Which fix addresses this common error?
channel.basic_publish(exchange='', routing_key='rpc_queue', properties=pika.BasicProperties(), body='Hello')
Solution
Step 1: Identify missing properties
The message lacksreply_toandcorrelation_id, needed for replies.Step 2: Add these properties to enable reply routing
Includingreply_totells where to send replies;correlation_idmatches responses.Final Answer:
Addreply_toandcorrelation_idin properties. -> Option CQuick Check:
Missing reply_to/correlation_id blocks replies [OK]
- Changing routing_key instead of adding properties
- Removing exchange causes unrelated errors
- Setting empty body does not fix reply issues
You want to implement a request-reply pattern where multiple clients send requests and receive replies without mixing responses. Which approach ensures correct matching?
Solution
Step 1: Understand correlation_id role
A uniquecorrelation_idlets clients match replies to requests even on a shared reply queue.Step 2: Evaluate reply queue usage
Using a shared reply queue with unique IDs is efficient and common practice.Final Answer:
Use a unique correlation_id per request and a shared reply queue. -> Option AQuick Check:
Unique correlation_id + shared queue = correct matching [OK]
- Using same correlation_id causes reply confusion
- Creating many reply queues adds complexity
- Omitting correlation_id breaks matching
Ready to prove it?
Pass the test and this pattern moves unknown
