Bird
Raised Fist0
RabbitMQdevops~10 mins

Direct exchange in RabbitMQ - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Direct exchange
Producer sends message with routing key
↓
Direct Exchange receives message
↓
Exchange matches routing key to queue binding
↓
Message routed to matching queue(s)
↓
Consumer receives message from queue
A direct exchange routes messages to queues based on an exact match between the message's routing key and the queue's binding key.
Execution Sample
RabbitMQ
channel.exchange_declare(exchange='direct_logs', exchange_type='direct')
channel.queue_bind(queue='info_queue', exchange='direct_logs', routing_key='info')
channel.basic_publish(exchange='direct_logs', routing_key='info', body='Info message')
Declare a direct exchange, bind a queue with routing key 'info', then send a message with routing key 'info' to route it to the queue.
Process Table
StepActionRouting KeyExchange BehaviorQueue MatchedMessage Routed
1Producer sends messageinfoDirect exchange receives messageNone yetNo
2Exchange checks bindingsinfoMatches routing key 'info'info_queueNo
3Message routed to queueinfoMessage delivered to 'info_queue'info_queueYes
4Consumer receives messageinfoMessage consumed from 'info_queue'info_queueYes
💡 Message routed and consumed from queue matching routing key 'info'
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
routing_keyNoneinfoinfoinfoinfo
exchange_stateNo messageMessage receivedRouting matchedMessage routedMessage consumed
queue_matchedNoneNoneinfo_queueinfo_queueinfo_queue
Key Moments - 2 Insights
Why does the message only go to 'info_queue' and not other queues?
Because the direct exchange routes messages only to queues whose binding key exactly matches the message's routing key, as shown in execution_table step 2.
What happens if no queue is bound with the routing key sent by the producer?
The message is dropped or returned to the producer if configured, since no binding matches the routing key, which is implied by the absence of a queue matched in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the exchange match the routing key to a queue?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Check the 'Exchange Behavior' and 'Queue Matched' columns in execution_table row for step 2.
According to variable_tracker, what is the state of 'queue_matched' after step 3?
Ainfo_queue
BNone
Cdirect_logs
DMessage consumed
💡 Hint
Look at the 'queue_matched' row and the 'After Step 3' column in variable_tracker.
If the producer sends a message with routing key 'error' but no queue is bound with 'error', what happens?
AMessage is routed to 'info_queue'
BMessage is routed to all queues
CMessage is dropped or returned
DMessage is stored in exchange
💡 Hint
Refer to key_moments explanation about no matching queue binding.
Concept Snapshot
Direct Exchange in RabbitMQ:
- Routes messages by exact routing key match
- Producer sends message with routing key
- Exchange matches routing key to queue binding
- Message delivered only to matching queues
- If no match, message is dropped or returned
Full Transcript
A direct exchange in RabbitMQ routes messages to queues based on exact matching of routing keys. The producer sends a message with a routing key. The direct exchange receives the message and checks its bindings. If a queue is bound with a matching routing key, the message is routed to that queue. Otherwise, the message is dropped or returned. This ensures precise routing of messages to intended queues.

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

  1. Step 1: Understand direct exchange routing

    A direct exchange routes messages only to queues whose binding key exactly matches the message's routing key.
  2. Step 2: Compare with other exchange types

    Unlike fanout (broadcast) or topic (pattern matching) exchanges, direct exchange requires exact key match.
  3. Final Answer:

    It routes messages to queues with an exact matching routing key. -> Option C
  4. 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

  1. Step 1: Recall rabbitmqadmin syntax for exchange declaration

    The correct syntax is: rabbitmqadmin declare exchange name=EXCHANGE_NAME type=EXCHANGE_TYPE
  2. 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.
  3. Final Answer:

    rabbitmqadmin declare exchange name=logs_direct type=direct -> Option B
  4. 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

  1. Step 1: Identify bindings and routing keys

    queue1 is bound with routing key 'info', queue2 with 'error'. The message has routing key 'info'.
  2. Step 2: Apply direct exchange routing rule

    Direct exchange routes messages only to queues with exact matching routing keys. So only queue1 matches.
  3. Final Answer:

    Only queue1 -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Routing key mismatch between binding and message -> Option D
  4. 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

  1. Step 1: Identify queues bound with routing key 'red'

    Q1 and Q3 are both bound with routing key 'red'. Q2 is bound with 'blue'.
  2. 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.
  3. Final Answer:

    Q1 and Q3 both receive it because they share the same routing key -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard