📖 Scenario: You are setting up a messaging system using RabbitMQ. You want to send messages to specific queues based on exact routing keys using a direct exchange.
🎯 Goal: Build a RabbitMQ setup with a direct exchange, bind queues with specific routing keys, and send messages that are routed correctly.
📋 What You'll Learn
Create a direct exchange named direct_logs
Create two queues named info_queue and error_queue
Bind info_queue to direct_logs with routing key info
Bind error_queue to direct_logs with routing key error
Publish messages with routing keys info and error to the exchange
Consume messages from both queues and print them
💡 Why This Matters
🌍 Real World
Direct exchanges are used in messaging systems where messages must be routed exactly to queues based on routing keys, such as logging systems or task dispatchers.
💼 Career
Understanding direct exchanges is essential for roles involving message brokers, microservices communication, and event-driven architectures.
Progress0 / 4 steps
1
Create the direct exchange and queues
Create a direct exchange called direct_logs. Create two queues called info_queue and error_queue.
RabbitMQ
Hint
Use exchange_declare with exchange_type='direct' and queue_declare for queues.
2
Bind queues to the direct exchange with routing keys
Bind the queue info_queue to the exchange direct_logs with routing key info. Bind the queue error_queue to the exchange direct_logs with routing key error.
RabbitMQ
Hint
Use queue_bind with the correct exchange, queue, and routing_key.
3
Publish messages with routing keys
Publish a message 'Info message' with routing key info to the exchange direct_logs. Publish a message 'Error message' with routing key error to the same exchange.
RabbitMQ
Hint
Use basic_publish with the correct exchange, routing_key, and body.
4
Consume and print messages from both queues
Consume one message from info_queue and print it. Then consume one message from error_queue and print it.
RabbitMQ
Hint
Use basic_get to get messages from queues and print the decoded body.
Practice
(1/5)
1. What is the main behavior of a direct exchange in RabbitMQ?
easy
A. It broadcasts messages to all queues regardless of routing keys.
B. It routes messages based on pattern matching of routing keys.
C. It routes messages to queues with an exact matching routing key.
D. It stores messages without routing them to any queue.
Solution
Step 1: Understand direct exchange routing
A direct exchange routes messages only to queues whose binding key exactly matches the message's routing key.
Step 2: Compare with other exchange types
Unlike fanout (broadcast) or topic (pattern matching) exchanges, direct exchange requires exact key match.
Final Answer:
It routes messages to queues with an exact matching routing key. -> Option C
Quick Check:
Direct exchange = exact routing key match [OK]
Hint: Direct exchange matches routing keys exactly [OK]
Common Mistakes:
Confusing direct with fanout exchange behavior
Thinking direct exchange uses pattern matching
Assuming messages go to all queues regardless of keys
2. Which of the following is the correct way to declare a direct exchange named logs_direct using RabbitMQ's command line tool rabbitmqadmin?
easy
A. rabbitmqadmin add exchange logs_direct type=fanout
B. rabbitmqadmin declare exchange name=logs_direct type=direct
C. rabbitmqadmin create exchange logs_direct direct
D. rabbitmqadmin declare exchange logs_direct type=topic
Solution
Step 1: Recall rabbitmqadmin syntax for exchange declaration
The correct syntax is: rabbitmqadmin declare exchange name=EXCHANGE_NAME type=EXCHANGE_TYPE
Step 2: Match the command to declare a direct exchange named logs_direct
rabbitmqadmin declare exchange name=logs_direct type=direct matches the syntax and specifies type=direct correctly.
Final Answer:
rabbitmqadmin declare exchange name=logs_direct type=direct -> Option B
Quick Check:
rabbitmqadmin declare exchange + name + type=direct [OK]
Hint: Use 'declare exchange name=... type=direct' syntax [OK]
Common Mistakes:
Using 'create' instead of 'declare'
Wrong exchange type like fanout or topic
Missing 'name=' before exchange name
3. Given a direct exchange ex_direct with two queues bound: queue1 with routing key info and queue2 with routing key error. If a message is published with routing key info, which queues will receive the message?
medium
A. Only queue1
B. Only queue2
C. Both queue1 and queue2
D. No queue receives the message
Solution
Step 1: Identify bindings and routing keys
queue1 is bound with routing key 'info', queue2 with 'error'. The message has routing key 'info'.
Step 2: Apply direct exchange routing rule
Direct exchange routes messages only to queues with exact matching routing keys. So only queue1 matches.
Final Answer:
Only queue1 -> Option A
Quick Check:
Message routing key = queue binding key [OK]
Hint: Message goes only to queues with exact routing key match [OK]
Common Mistakes:
Assuming message goes to all queues
Confusing routing keys with patterns
Thinking queue2 also receives message
4. You declared a direct exchange and bound a queue with routing key task. However, messages published with routing key tasks are not received by the queue. What is the most likely cause?
medium
A. Message payload is empty
B. Exchange type is incorrectly set to fanout
C. Queue is not declared properly
D. Routing key mismatch between binding and message
Solution
Step 1: Check routing key binding and message key
The queue is bound with routing key 'task' but messages are sent with 'tasks'. These keys differ.
Step 2: Understand direct exchange routing behavior
Direct exchange requires exact routing key match. A mismatch means the message won't be routed to the queue.
Final Answer:
Routing key mismatch between binding and message -> Option D
Quick Check:
Exact routing key match needed [OK]
Hint: Check routing keys for exact spelling match [OK]
Common Mistakes:
Assuming fanout exchange routes by keys
Blaming queue declaration without checking keys
Thinking empty payload blocks routing
5. You have a direct exchange ex_direct with three queues bound: Q1 with routing key red, Q2 with routing key blue, and Q3 with routing key red. If you publish a message with routing key red, which queues receive the message and why?
hard
A. Q1 and Q3 both receive it because they share the same routing key
B. No queue receives it because multiple bindings cause conflict
C. Only Q3 receives it because it was bound last
D. Only Q1 receives it because it was bound first
Solution
Step 1: Identify queues bound with routing key 'red'
Q1 and Q3 are both bound with routing key 'red'. Q2 is bound with 'blue'.
Step 2: Apply direct exchange routing logic
Direct exchange routes messages to all queues with matching routing keys. So both Q1 and Q3 receive the message.
Final Answer:
Q1 and Q3 both receive it because they share the same routing key -> Option A
Quick Check:
Multiple queues with same key all get message [OK]
Hint: Multiple queues with same key all get messages [OK]
Common Mistakes:
Thinking only one queue receives the message
Assuming binding order affects routing
Believing multiple bindings cause conflicts
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.