Types of Exchange in RabbitMQ

Overview

In this article, we will get an overview of different types of exchanges in RabbitMQ.

Exchange In RabbitMQ

Messages are not published directly to a queue; instead, the producer sends a message to an exchange. The job of an exchange is to accept messages from the producer applications and route them to the correct message queues. It does this with the help of bindings and routing keys. A binding is a link between a queue and an exchange.

Message Flow in RabbitMQ

1. The producer publishes a message to an exchange. When creating an exchange, its type must be specified.
2. The exchange receives the message and is now responsible for the routing of the message. The exchange looks at different message attributes and keys depending on the exchange type.
3. In this case, we see two bindings to two different queues from the exchange. The exchange routes the message to the correct queue, depending on these attributes.
4. The messages stay in the queue until the consumer processes them.
5. The consumer then removes the message from the queue once handled.

Types of Exchange

Direct

- A direct exchange delivers messages to queues based on a message routing key. In a direct exchange, the message is routed to the queue with the exact match of the binding key as the routing key of the message. For example, if the queue is bound to the exchange using the binding key ‘emailprocess’, a message published to the exchange with a routing key ‘emailprocess’ is routed to that queue.

Topic

- The topic exchange performs a wildcard match between the routing key and the routing pattern specified in the binding.

Fanout

- A fanout exchange routes messages to all of the queues that are bound to it.

Headers

- A header exchange uses the message header attributes for routing purposes.