Bindings & Routing Keys
BeginnerA binding connects an exchange to a queue with an optional routing key; a queue can have multiple bindings from different exchanges to receive diverse message streams.
Overview
A binding is a rule that tells an exchange how to route messages to a queue. For direct exchanges, the binding's routing key must exactly match the message's routing key. For topic exchanges, the binding key is a pattern with * (one word) and # (zero or more words) wildcards. Fanout exchanges ignore routing keys and deliver to all bound queues. A queue can have multiple bindings — from multiple exchanges and/or multiple routing keys — giving it the ability to receive diverse message streams. Bindings are declared at startup via Spring AMQP's Binding bean or via the management API.
Direct Exchange Bindings
Each binding maps an exact routing key string from exchange to queue. Multiple queues can bind with the same key (multicast), or one queue with multiple keys.
@Configuration
public class BindingConfig {
@Bean DirectExchange ordersExchange() { return new DirectExchange("orders"); }
@Bean Queue newOrderQueue() { return QueueBuilder.durable("orders.new").build(); }
@Bean Queue cancelQueue() { return QueueBuilder.durable("orders.cancel").build(); }
// Exact-match binding: routing key "order.created" → orders.new
@Bean
Binding newOrderBinding(Queue newOrderQueue, DirectExchange ordersExchange) {
return BindingBuilder.bind(newOrderQueue).to(ordersExchange).with("order.created");
}
// Exact-match binding: routing key "order.cancelled" → orders.cancel
@Bean
Binding cancelBinding(Queue cancelQueue, DirectExchange ordersExchange) {
return BindingBuilder.bind(cancelQueue).to(ordersExchange).with("order.cancelled");
}
}Topic Exchange Bindings with Wildcards
* matches exactly one word; # matches zero or more words separated by dots. Topic routing enables fine-grained subscriptions: one consumer subscribes to all EU orders, another to all payment events globally.
@Bean TopicExchange eventsExchange() { return new TopicExchange("events"); }
// Subscribe to all order events in EU region: order.eu.created, order.eu.cancelled
@Bean
Binding euOrdersBinding(Queue euQueue, TopicExchange eventsExchange) {
return BindingBuilder.bind(euQueue).to(eventsExchange).with("order.eu.*");
}
// Subscribe to ALL payment events across ALL regions
@Bean
Binding allPaymentsBinding(Queue paymentsQueue, TopicExchange eventsExchange) {
return BindingBuilder.bind(paymentsQueue).to(eventsExchange).with("payment.#");
}
// Publishing — routing key matches patterns above
rabbitTemplate.convertAndSend("events", "order.eu.created", event); // → euQueue
rabbitTemplate.convertAndSend("events", "payment.us.completed", event); // → paymentsQueueMultiple Bindings Per Queue
A single queue can receive messages from multiple exchanges and/or routing key patterns. This simplifies consumers that need to process multiple event types from different publishers.
// One audit queue receives events from both orders and payments exchanges
@Bean Queue auditQueue() { return QueueBuilder.durable("audit.all").build(); }
@Bean
Binding auditFromOrders(Queue auditQueue, DirectExchange ordersExchange) {
return BindingBuilder.bind(auditQueue).to(ordersExchange).with("order.created");
}
@Bean
Binding auditFromPayments(Queue auditQueue, DirectExchange paymentsExchange) {
return BindingBuilder.bind(auditQueue).to(paymentsExchange).with("payment.captured");
}
// @RabbitListener on auditQueue receives both order.created and payment.captured messages
@RabbitListener(queues = "audit.all")
public void audit(Message message) { /* handle both event types */ }Key Points to Remember
- 1A binding is a rule: exchange + routing key pattern → queue
- 2Direct exchange: exact routing key match; Topic exchange: * (one word), # (zero or more)
- 3Fanout exchange ignores routing keys and delivers to all bound queues
- 4One queue can have multiple bindings — from multiple exchanges or multiple routing keys
- 5One routing key can route to multiple queues (multicast) on a direct/topic exchange
- 6Bindings are declared in Spring AMQP via Binding beans and auto-applied by RabbitAdmin
Interview Questions
Sign in to ask AriaWhat is the difference between * and # in a topic exchange routing key?
Can two different queues receive the same message on a direct exchange?
How would you route a message to multiple queues with different consumers?
What routing key pattern would match payment.us.completed and payment.eu.failed?
How does a fanout exchange treat routing keys?
Ask Aria about Bindings & Routing Keys
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.