The exchange types
Let's have a look at the different types of exchanges that exist in RabbitMQ. There are four different types of exchanges. Direct, Fanout, Topic, and Headers. Each of these types will handle messages and routing keys differently. Direct Exchange will route messages to all queues that have been connected to this exchange with the same routing key as the routing key that the publisher used. In this example, the publisher used the routing key customer.purchase. Let's assume there are two queues that have been bound to this exchange. One with the routing key customer.purchase and the other with customer.registration. Direct Exchange will now route the message to the queue with customer.purchase as a routing key. The other queue will not receive the message. In the case of a Fanout Exchange, messages are routed to all bound queues and the routing key is just ignored. Every queue will receive a copy of the message. Topic Exchanges are used to route messages based on patterns in the routing key. In this example, we'll send a message and add the routing key customer.purchase.cancelled. Now if a binding was declared with a hash, this symbol matches zero or more words in the routing key. You can also use an asterisk, which will match a single word. A queue that uses premiumcustomer.# in this example will not match and the message will not be routed to this queue. The last type of exchange is the Headers Exchange. This exchange will ignore the routing key and instead look at the headers that were sent with the message. Let's assume we have a publisher that sent a message with the headers, entity: order and action: cancelled. When we bind a queue to this exchange, we need to decide if we want to match all or any of these headers. A queue that has a binding with the arguments, entity = order, action = cancelled, and x-match = all will receive this message. The x-match = all arguments means all headers must match. With a queue that has a binding with the arguments, entity = order, action = confirmed, and x-match is all will not receive this message. The action argument differs from the header sent with the message. Finally, picture a queue that has a binding with the arguments, entity = order, action = confirmed, and x-match = any. This queue will receive the message. Even though not all headers match the arguments of the binding, at least one of them does. The entity header matches and that's enough. These four types of exchanges are powerful enough to solve the many use cases that you will encounter.
No comments:
Post a Comment