0
0
RabbitMQdevops~5 mins

Binding keys and routing keys in RabbitMQ - Commands & Configuration

Choose your learning style9 modes available
Introduction
When sending messages in RabbitMQ, you need a way to decide which messages go to which queues. Binding keys and routing keys help match messages to queues so the right consumers get the right messages.
When you want to send messages to specific queues based on message content
When you need to filter messages so only certain queues receive them
When you want to use a topic exchange to route messages by patterns
When you want to connect multiple queues to one exchange with different filters
When you want to organize message flow in a publish-subscribe system
Config File - rabbitmq_binding_example.sh
rabbitmq_binding_example.sh
#!/bin/bash

# Declare a topic exchange named 'my_topic_exchange'
rabbitmqadmin declare exchange name=my_topic_exchange type=topic

# Declare two queues
rabbitmqadmin declare queue name=queue_errors durable=true
rabbitmqadmin declare queue name=queue_logs durable=true

# Bind queue_errors to the exchange with binding key 'error.*'
rabbitmqadmin declare binding source=my_topic_exchange destination=queue_errors routing_key=error.*

# Bind queue_logs to the exchange with binding key '*.log'
rabbitmqadmin declare binding source=my_topic_exchange destination=queue_logs routing_key=*.log

This script creates a topic exchange called my_topic_exchange. It then creates two queues: queue_errors and queue_logs. Each queue is bound to the exchange with a binding key pattern. queue_errors receives messages with routing keys starting with error., and queue_logs receives messages with routing keys ending with .log. This setup routes messages based on their routing keys matching the binding keys.

Commands
Create a topic exchange named 'my_topic_exchange' where messages will be sent and routed based on routing keys.
Terminal
rabbitmqadmin declare exchange name=my_topic_exchange type=topic
Expected OutputExpected
Successfully declared exchange 'my_topic_exchange'
type=topic - Defines the exchange type to route messages by pattern matching routing keys
Create a durable queue named 'queue_errors' to receive error messages.
Terminal
rabbitmqadmin declare queue name=queue_errors durable=true
Expected OutputExpected
Successfully declared queue 'queue_errors'
durable=true - Ensures the queue survives RabbitMQ restarts
Create a durable queue named 'queue_logs' to receive log messages.
Terminal
rabbitmqadmin declare queue name=queue_logs durable=true
Expected OutputExpected
Successfully declared queue 'queue_logs'
durable=true - Ensures the queue survives RabbitMQ restarts
Bind 'queue_errors' to the exchange with binding key 'error.*' so it receives messages with routing keys starting with 'error.'.
Terminal
rabbitmqadmin declare binding source=my_topic_exchange destination=queue_errors routing_key=error.*
Expected OutputExpected
Successfully declared binding from 'my_topic_exchange' to 'queue_errors' with routing key 'error.*'
routing_key=error.* - Filters messages to those with routing keys matching 'error.*'
Bind 'queue_logs' to the exchange with binding key '*.log' so it receives messages with routing keys ending with '.log'.
Terminal
rabbitmqadmin declare binding source=my_topic_exchange destination=queue_logs routing_key=*.log
Expected OutputExpected
Successfully declared binding from 'my_topic_exchange' to 'queue_logs' with routing key '*.log'
routing_key=*.log - Filters messages to those with routing keys matching '*.log'
Key Concept

Binding keys on queues and routing keys on messages work together to route messages to the right queues based on matching patterns.

Common Mistakes
Using the same string for binding key and routing key without understanding pattern matching
Messages may not be routed as expected because topic exchanges use patterns like '*' and '#' for matching
Use wildcards '*' and '#' in binding keys to match routing keys patterns properly
Binding queues to the wrong exchange type (e.g., direct instead of topic)
Routing keys are interpreted differently depending on exchange type, causing routing failures
Ensure the exchange type matches the routing logic you want, like 'topic' for pattern matching
Not declaring queues before binding them to exchanges
Bindings fail because the queue does not exist yet
Always declare queues before creating bindings
Summary
Create a topic exchange to route messages based on routing keys.
Declare queues that will receive messages filtered by binding keys.
Bind queues to the exchange with binding keys that use patterns to match routing keys.
Messages sent with routing keys matching binding keys are delivered to the correct queues.