Bird
Raised Fist0
RabbitMQdevops~20 mins

Correlation ID for matching replies in RabbitMQ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Correlation ID Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Purpose of Correlation ID in RabbitMQ

What is the main purpose of using a Correlation ID in RabbitMQ messaging?

ATo uniquely identify and match a reply message to its original request message
BTo specify the queue where the message should be delivered
CTo encrypt the message content for security
DTo set the message priority for delivery order
Attempts:
2 left
💡 Hint

Think about how a client knows which reply belongs to which request.

💻 Command Output
intermediate
1:30remaining
Output of a RabbitMQ message with Correlation ID

Given a message published with the following properties:

properties = {"correlation_id": "abc123", "reply_to": "response_queue"}

What will be the value of correlation_id in the received message properties?

A"abc123"
B"response_queue"
Cnull
D"correlation_id"
Attempts:
2 left
💡 Hint

The correlation_id is set explicitly in the message properties.

🔀 Workflow
advanced
2:30remaining
Correct workflow to handle RPC with Correlation ID

Which of the following workflows correctly uses the Correlation ID to handle RPC (Remote Procedure Call) replies in RabbitMQ?

AClient sends request with Correlation ID; server sends reply without Correlation ID; client matches replies by timestamp.
BClient sends request without Correlation ID; server generates Correlation ID and sends reply; client listens on default queue without matching Correlation ID.
CClient sends request with a unique Correlation ID and reply_to queue; server processes and sends reply with same Correlation ID to reply_to queue; client listens on reply_to queue and matches replies by Correlation ID.
DClient sends request with Correlation ID; server ignores Correlation ID and sends reply to a fixed queue; client matches replies by message content only.
Attempts:
2 left
💡 Hint

Think about how the client knows which reply belongs to which request.

❓ Troubleshoot
advanced
2:00remaining
Troubleshooting missing replies in RPC with Correlation ID

A client sends RPC requests with unique Correlation IDs and a reply_to queue. However, the client never receives replies. Which of the following is the most likely cause?

AThe server is sending replies to the default exchange instead of the reply_to queue.
BThe client is listening on the wrong queue, not the reply_to queue specified.
CThe server is not setting the Correlation ID in the reply message properties.
DThe client is not generating unique Correlation IDs for each request.
Attempts:
2 left
💡 Hint

Check where the client is listening for replies.

✅ Best Practice
expert
2:30remaining
Best practice for generating Correlation IDs in RabbitMQ RPC

Which of the following is the best practice for generating Correlation IDs for RPC requests in RabbitMQ to ensure reliable reply matching?

AUse the client's IP address as the Correlation ID.
BUse a simple incrementing integer starting from 1 for each request.
CUse the current timestamp in seconds as the Correlation ID.
DUse a UUID (Universally Unique Identifier) for each request's Correlation ID.
Attempts:
2 left
💡 Hint

Think about uniqueness and collision risk.

Practice

(1/5)
1. What is the main purpose of the correlation_id property in RabbitMQ messaging?
easy
A. To specify the message priority in the queue
B. To link a reply message to its original request message
C. To encrypt the message content for security
D. To define the message expiration time

Solution

  1. Step 1: Understand message flow in request-reply pattern

    In RabbitMQ, when a client sends a request, it expects a reply. The correlation_id helps identify which reply matches which request.
  2. Step 2: Identify the role of correlation_id

    The correlation_id is set in the request message and copied to the reply, so the client can match them correctly.
  3. Final Answer:

    To link a reply message to its original request message -> Option B
  4. Quick Check:

    Correlation ID = Link request and reply [OK]
Hint: Correlation ID connects request and reply messages [OK]
Common Mistakes:
  • Thinking correlation_id encrypts messages
  • Confusing correlation_id with message priority
  • Assuming correlation_id sets message expiration
2. Which of the following is the correct way to set the correlation_id property when publishing a message in RabbitMQ using Python pika library?
easy
A. channel.send(exchange='', routing_key='queue', body='msg', properties={'correlation': '123'})
B. channel.publish(exchange='', routing_key='queue', body='msg', correlation_id='123')
C. channel.basic_publish(exchange='', routing_key='queue', body='msg', properties=pika.BasicProperties(correlation_id='123'))
D. channel.basic_send(exchange='', routing_key='queue', body='msg', correlation='123')

Solution

  1. Step 1: Recall pika syntax for setting message properties

    In pika, message properties are set using pika.BasicProperties with named arguments like correlation_id.
  2. Step 2: Identify correct method and property usage

    The correct method is basic_publish and the property is correlation_id inside BasicProperties.
  3. Final Answer:

    channel.basic_publish(exchange='', routing_key='queue', body='msg', properties=pika.BasicProperties(correlation_id='123')) -> Option C
  4. Quick Check:

    Use basic_publish + BasicProperties(correlation_id) [OK]
Hint: Use BasicProperties with correlation_id in basic_publish [OK]
Common Mistakes:
  • Using wrong method like publish or send
  • Passing correlation_id outside BasicProperties
  • Using incorrect property name like 'correlation'
3. Given the following Python code snippet using pika, what will be printed?
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

corr_id = 'abc123'

channel.basic_publish(
    exchange='',
    routing_key='rpc_queue',
    body='Request',
    properties=pika.BasicProperties(correlation_id=corr_id, reply_to='reply_queue')
)

print(corr_id)
medium
A. abc123
B. Request
C. None
D. Error: correlation_id not defined

Solution

  1. Step 1: Analyze variable assignment and usage

    The variable corr_id is assigned the string 'abc123' before publishing the message.
  2. Step 2: Understand the print statement

    The print statement outputs the value of corr_id, which is 'abc123'. The message publishing does not affect this variable.
  3. Final Answer:

    abc123 -> Option A
  4. Quick Check:

    Print variable corr_id = abc123 [OK]
Hint: Print variable directly, not message body [OK]
Common Mistakes:
  • Confusing printed output with message body
  • Expecting None because of message publish
  • Assuming error due to correlation_id usage
4. You wrote this code to match replies by correlation ID but it never matches. What is the likely error?
def on_response(ch, method, props, body):
    if props.correlation_id == corr_id:
        print('Reply matched:', body)

channel.basic_consume(queue='reply_queue', on_message_callback=on_response, auto_ack=True)
medium
A. The variable corr_id is not defined in the callback scope
B. The auto_ack parameter should be False
C. The correlation_id property is not set in the reply message
D. The queue name 'reply_queue' is incorrect

Solution

  1. Step 1: Check variable scope in callback function

    The callback uses corr_id but it is not passed or declared inside the function, causing a NameError or mismatch.
  2. Step 2: Understand variable scope and fix

    To fix, corr_id should be accessible inside on_response, either by passing it or declaring as global or closure variable.
  3. Final Answer:

    The variable corr_id is not defined in the callback scope -> Option A
  4. Quick Check:

    corr_id must be in callback scope [OK]
Hint: Ensure corr_id is accessible inside callback function [OK]
Common Mistakes:
  • Ignoring variable scope issues
  • Changing auto_ack unnecessarily
  • Assuming queue name is wrong without checking
5. You want to implement a RabbitMQ RPC client that sends multiple requests and matches replies correctly. Which approach best uses correlation_id to handle multiple outstanding requests?
hard
A. Use the same correlation_id for all requests to simplify matching
B. Set correlation_id only on the first request and ignore for others
C. Do not set correlation_id and rely on message order in the queue
D. Generate a unique correlation_id for each request and store a mapping of IDs to callbacks or futures

Solution

  1. Step 1: Understand multiple outstanding requests scenario

    When sending many requests, replies can arrive in any order, so each must be uniquely identified.
  2. Step 2: Use unique correlation IDs and mapping

    Assign a unique correlation_id per request and keep a map from ID to the request handler (callback or future) to match replies correctly.
  3. Final Answer:

    Generate a unique correlation_id for each request and store a mapping of IDs to callbacks or futures -> Option D
  4. Quick Check:

    Unique correlation_id + mapping = correct reply matching [OK]
Hint: Use unique correlation_id per request with a map [OK]
Common Mistakes:
  • Using same correlation_id for all requests
  • Ignoring correlation_id and relying on order
  • Setting correlation_id only once
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard