0
0
RabbitmqHow-ToBeginner · 4 min read

How to Use Routing Key with Exchange in RabbitMQ

In RabbitMQ, a routing key is used with an exchange to determine how messages are routed to queues. When publishing a message, you specify the routing key, and the exchange uses it to match bindings and deliver the message to the correct queue(s).
📐

Syntax

The basic syntax involves publishing a message to an exchange with a routing key. The exchange then routes the message to queues bound with matching keys.

Key parts:

  • Exchange: The message router (e.g., direct, topic, fanout).
  • Routing Key: A string used by the exchange to decide where to send the message.
  • Queue Binding: Queues bind to exchanges with a binding key that the routing key is matched against.
python
channel.basic_publish(exchange='my_exchange', routing_key='my.routing.key', body='Hello World!')
💻

Example

This example shows how to declare a direct exchange, bind a queue with a routing key, and publish a message with that routing key. The message will be delivered to the queue because the routing key matches the binding key.

python
import pika

# Connect to RabbitMQ server
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

# Declare a direct exchange
channel.exchange_declare(exchange='logs_direct', exchange_type='direct')

# Declare a queue
channel.queue_declare(queue='error_logs')

# Bind queue to exchange with routing key 'error'
channel.queue_bind(exchange='logs_direct', queue='error_logs', routing_key='error')

# Publish a message with routing key 'error'
channel.basic_publish(exchange='logs_direct', routing_key='error', body='Error log message')

print("[x] Sent 'Error log message' with routing key 'error'")

connection.close()
Output
[x] Sent 'Error log message' with routing key 'error'
⚠️

Common Pitfalls

Common mistakes when using routing keys with exchanges include:

  • Using the wrong exchange type: For example, fanout exchanges ignore routing keys, so messages go to all bound queues regardless of the key.
  • Mismatch between routing key and binding key: If the routing key does not match any binding key, the message will be dropped or returned if mandatory flag is set.
  • Not declaring or binding queues properly before publishing messages.
python
## Wrong: Using fanout exchange with routing key (routing key ignored)
channel.exchange_declare(exchange='logs_fanout', exchange_type='fanout')
channel.basic_publish(exchange='logs_fanout', routing_key='error', body='Message')

## Right: Use direct or topic exchange to respect routing keys
channel.exchange_declare(exchange='logs_direct', exchange_type='direct')
channel.basic_publish(exchange='logs_direct', routing_key='error', body='Message')
📊

Quick Reference

Tips for using routing keys with exchanges:

  • Direct Exchange: Routes messages to queues with exact matching routing keys.
  • Topic Exchange: Supports pattern matching with routing keys using * and #.
  • Fanout Exchange: Ignores routing keys and broadcasts to all bound queues.
  • Always bind queues to exchanges with appropriate binding keys to receive messages.
Exchange TypeRouting Key BehaviorUse Case
directExact match routing keySend messages to specific queues
topicPattern matching with * and #Flexible routing with patterns
fanoutIgnores routing keyBroadcast to all queues
headersBased on message headers, not routing keyAdvanced routing scenarios

Key Takeaways

Routing keys are strings used by exchanges to route messages to queues based on bindings.
Direct exchanges route messages to queues with exactly matching routing keys.
Topic exchanges allow pattern matching in routing keys for flexible routing.
Fanout exchanges ignore routing keys and send messages to all bound queues.
Always ensure queues are bound to exchanges with correct binding keys to receive messages.