📖 Scenario: You are setting up a messaging system using RabbitMQ. You want to route messages to different queues based on specific topics. This helps different parts of your application receive only the messages they need.
🎯 Goal: Build a simple RabbitMQ setup where you create an exchange, bind queues with binding keys, and send messages with routing keys to see how messages are routed.
📋 What You'll Learn
Create a topic exchange named logs_topic
Create two queues named queue_error and queue_info
Bind queue_error to logs_topic with binding key error.*
Bind queue_info to logs_topic with binding key info.#
Publish messages with routing keys error.system and info.user.login
Observe which queues receive which messages
💡 Why This Matters
🌍 Real World
Routing messages with binding keys and routing keys is common in microservices and event-driven systems to direct messages to the right service.
💼 Career
Understanding RabbitMQ routing helps in roles like DevOps engineer, backend developer, and system architect where message brokers are used for scalable communication.
Progress0 / 4 steps
1
Create the exchange and queues
Create a topic exchange called logs_topic. Then create two queues named queue_error and queue_info.
RabbitMQ
Hint
Use exchange_declare with exchange_type='topic' to create the exchange. Use queue_declare to create each queue.
2
Bind queues with binding keys
Bind the queue queue_error to the exchange logs_topic using the binding key error.*. Bind the queue queue_info to the same exchange using the binding key info.#.
RabbitMQ
Hint
Use queue_bind method to connect each queue to the exchange with the correct binding key.
3
Publish messages with routing keys
Publish a message with routing key error.system and another message with routing key info.user.login to the exchange logs_topic.
RabbitMQ
Hint
Use basic_publish to send messages to the exchange with the specified routing keys.
4
Print which queues receive messages
Consume one message from queue_error and one message from queue_info. Print the messages received from each queue.
RabbitMQ
Hint
Use basic_get to get one message from each queue and print the message body.
Practice
(1/5)
1. What is the main purpose of a binding key in RabbitMQ?
easy
A. To label messages for routing
B. To create a new exchange
C. To connect a queue to an exchange and filter messages
D. To delete a queue
Solution
Step 1: Understand the role of binding keys
Binding keys link queues to exchanges and decide which messages the queue receives.
Step 2: Differentiate from routing keys
Routing keys label messages, but binding keys filter messages for queues.
Final Answer:
To connect a queue to an exchange and filter messages -> Option C
Quick Check:
Binding key = filter for queues [OK]
Hint: Binding keys link queues to exchanges filtering messages [OK]
Common Mistakes:
Confusing binding keys with routing keys
Thinking binding keys create exchanges
Assuming binding keys delete queues
2. Which of the following is the correct syntax to bind a queue named myQueue to an exchange myExchange with a binding key key.* using RabbitMQ CLI?
easy
A. rabbitmqadmin declare binding source=myExchange destination=myQueue routing_key=key.*
B. rabbitmqctl bind_queue myExchange myQueue key.*
C. rabbitmqctl declare binding myQueue myExchange key.*
D. rabbitmqadmin bind myQueue myExchange key.*
Solution
Step 1: Identify the correct RabbitMQ CLI command
The rabbitmqadmin declare binding command is used to create bindings with source, destination, and routing_key parameters.
Step 2: Match parameters to syntax
Source is the exchange, destination is the queue, and routing_key is the binding key pattern.
Final Answer:
rabbitmqadmin declare binding source=myExchange destination=myQueue routing_key=key.* -> Option A
Quick Check:
Use rabbitmqadmin declare binding with source, destination, routing_key [OK]
Hint: Use rabbitmqadmin declare binding with source, destination, routing_key [OK]
Common Mistakes:
Using rabbitmqctl bind_queue which is not a valid command
Swapping queue and exchange names
Omitting routing_key parameter
3. Given a topic exchange with a queue bound using binding key logs.*.error, which routing key will deliver a message to this queue?
medium
A. logs.system.error
B. logs.error.system
C. logs.system.warning
D. error.logs.system
Solution
Step 1: Understand the binding key pattern
The binding key logs.*.error matches routing keys with three words where the first is 'logs', the second can be any word, and the third is 'error'.
Step 2: Match each option to the pattern
logs.system.error: 'logs.system.error' matches exactly. logs.error.system: 'logs.error.system' ends with 'system', not 'error'. logs.system.warning: 'logs.system.warning' ends with 'warning', not 'error'. error.logs.system: 'error.logs.system' does not start with 'logs'.
Hint: Match binding key wildcards to routing key words [OK]
Common Mistakes:
Ignoring position of words in routing key
Confusing '*' wildcard with '#' wildcard
Assuming partial matches are enough
4. You have a queue bound to a topic exchange with binding key data.#. You send a message with routing key data but it is not received by the queue. What is the likely cause?
medium
A. Binding key data.# does not match routing key data
B. Routing key data should be data.#
C. Queue is not bound to the exchange
D. Binding key data.# matches routing key data, so the problem is elsewhere
Solution
Step 1: Check if binding key matches routing key
The binding key data.# matches routing keys starting with 'data' and zero or more words after. The routing key 'data' matches because '#' can match zero words.
Step 2: Consider other causes
Since the binding key matches, the message not arriving is likely due to other issues like queue not bound or exchange type mismatch.
Final Answer:
Binding key data.# matches routing key data, so the problem is elsewhere -> Option D
Quick Check:
Binding key '#' matches zero or more words [OK]
Hint: Remember '#' matches zero or more words including none [OK]
Common Mistakes:
Thinking '#' requires at least one word
Assuming routing key must equal binding key exactly
Ignoring other configuration issues
5. You want to route messages with routing keys order.created and order.updated to a queue named orderQueue using a topic exchange. Which most specific binding key should you use to receive both types of messages?
hard
A. order.created, order.updated
B. order.*
C. *.created, *.updated
D. order.#
Solution
Step 1: Understand routing keys and binding keys
Routing keys are 'order.created' and 'order.updated'. The binding key must match both.
Step 2: Analyze each binding key option
order.*: 'order.*' matches 'order.created' and 'order.updated' because '*' matches exactly one word. order.#: 'order.#' matches these and more, but is broader than needed. *.created, *.updated: '*.created, *.updated' do not match because the first word must be 'order'. order.created, order.updated: 'order.created, order.updated' is not valid syntax for a single binding key; multiple bindings needed.
Final Answer:
order.* -> Option B
Quick Check:
'order.*' matches 'order.created' and 'order.updated' [OK]
Hint: Use 'order.*' to match one word after 'order' [OK]
Common Mistakes:
Using '#' when '*' is enough
Trying to use multiple keys in one binding key string
Misplacing wildcards in binding keys
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.