Home » RabbitMQ binding vs routing key

RabbitMQ binding vs routing key

rabbitmq binding key vs routing key

1. Overview

In this article, we will learn the differences between RabbitMQ binding key vs routing key. To learn more about other RabbitMQ topics, refer to these articles.

2. RabbitMQ Exchange and Queues

The core idea in the messaging model in RabbitMQ is that the producer sends no messages directly to a queue. Actually, often the producer doesn’t even know if a message will be delivered to any queue at all.

  • producer is a user application that sends messages.
  • queue is a buffer that stores messages.
  • consumer is a user application that receives messages.

Instead, the producer can only send messages to an exchange. An exchange is a very simple thing. It receives messages from producers on one side and pushes them to queues on another side. The exchange must know exactly what to do with a message it receives.

  • Should it push into a particular queue?
  • or should it push into many queues?
  • Should need to discard it.

The Exchanges take a message and route it into zero or more queues. The routing algorithm used depends on the exchange type and rules called bindings.

Exchanges can be of any type, such as Topic, Fanout, Direct, Default, and Header.

TopicExchange exchange() {
	return new TopicExchange(topicExchangeName);

// JAva
channel.exchangeDeclare("logs", "fanout");

3. RabbitMQ binding and routing key

We need to tell the exchange to send messages to which queue. That relationship between exchange and a queue is called binding.

This can be simply read as: the queue is interested in messages from this exchange.

Bindings can take an extra routing key parameter. The routing key is a message attribute the exchange looks at when deciding how to route the message to the queue (depending on exchange type).

For example, In direct exchange, a message goes to the queues whose binding key exactly matches the routing key of the message.

// Java
channel.queueBind(queueName, "logs", "");

// Spring
Binding binding(Queue queue, TopicExchange exchange) {
        // binds the queue with an exchange using the binding key foo.bar
	return BindingBuilder.bind(queue).to(exchange).with("foo.bar");

// sending message in spring
private final RabbitTemplate rabbitTemplate;

System.out.println("Sending message...");
// send message to the exchange with Routing key foo.bar
rabbitTemplate.convertAndSend(SampleApplication.topicExchangeName, "foo.bar", "Hello from RabbitMQ!");

4. RabbitMQ binding vs Routing key

  1. Routing keys are on messages
  2. A queue binds to the exchange with a binding key K.
  3. When a new message with routing key R arrives at the exchange, the exchange routes it to the respective queue if K (binding key) = R (Routing key)

4.1. RabbitMQ binding vs routing example

In this setup, we can see the direct exchange X with two queues bound to it. The first queue is bound with the binding key orange, and the second has two bindings, one with the binding key black and the other one with green.

In such a setup, the RabbitMQ will route the message published to the exchange with a routing key orange to queue Q1. Messages with a routing key of black or green will go to Q2. It discards all other messages.

5. Conclusion

To sum up, we have learned the differences between binding vs routing key. You can find code samples in our GitHub repository.

2 thoughts on “RabbitMQ binding vs routing key”

  1. Pingback: RabbitMQ direct exchange - TedBlob

  2. Pingback: Rabbitmq Topic vs Direct exchange

Leave a Reply

Your email address will not be published.