RabbitMQ Exchange Types, Bindings and Routing Keys
3 years ago Lalit Bhagtani 0
Exchanges are message routing agents, which are defined per virtual host within the rabbitMQ system. When program/application (Known as Producer) connect to RabbitMQ server to publish a message, it first sends the message to an exchange. After receiving a message, exchange routes them to different message queues with help of header attributes, bindings, and routing keys. It should be noted that messages are not published directly to a queue, instead, it is first send to an exchange and then exchange routes them to different message queues.
Binding :- It is a link between a queue and an exchange.
Routing key :- It is a message attribute that the exchange can looks at, to decide how to route the message to queues. The routing key is like an address for the message.
Exchanges, connections and queues can be configured with parameters such as durable, temporary, and auto delete during creation.
- Durable Exchange :- It will survive server restarts and will last until they are explicitly deleted.
- Temporary Exchanges :- It will exist until RabbitMQ is shutdown.
- Auto Deleted Exchanges :- It will exist till the last bound object unbound from the exchange.
User can create their own exchanges or use the predefined default exchanges, default exchanges are created when the server starts for the first time.
RabbitMQ Exchange Types :-
RabbitMQ provide four different types of exchange, each differ in the way they route messages to the queues. Brief explanation about each of them are as follows :-
- Direct Exchange :- A direct exchange routes messages to queues based on message routing key. The routing key is a message attribute in the message header added by the producer. The default exchange for direct exchange is “amq.direct“. For detailed explanation and code example, you can visit RabbitMQ – Direct Exchange.
- Topic Exchange :- A topic exchange route messages to queues based on the wildcard match between routing key and routing pattern specified during the binding of the queue. The default exchange for topic exchange is “amq.topic“. For detailed explanation and code example, you can visit RabbitMQ – Topic Exchange.
- Fanout Exchange :- A fanout exchange copies the message and routes it to all the queues bound to it. The default exchange for fanout exchange is “amq.fanout“. For detailed explanation and code example, you can visit RabbitMQ – Fanout Exchange.
- Headers Exchange :- A headers exchange route messages to queues based on message header values (key-value pair) instead of routing key. The default exchange for headers exchange is “amq.headers“. For detailed explanation and code example, you can visit RabbitMQ – Headers Exchange.
That’s all for RabbitMQ Exchange Types, Bindings and Routing Keys, If you liked it, please share your thoughts in comments section and share it with others too.