All Products
Search
Document Center

ApsaraMQ for RabbitMQ:Exchange

Last Updated:Aug 23, 2024

This topic introduces the concept of exchange and the types of exchanges in ApsaraMQ for RabbitMQ. This helps you understand and select exchanges.

What is an exchange?

An exchange is a message routing agent of ApsaraMQ for RabbitMQ. When a producer sends a message to an ApsaraMQ for RabbitMQ broker, the message is sent to an exchange and then the exchange routes the message to one or more queues. An exchange routes messages based on routing keys and headers attributes.

Direct exchanges

  • Routing rule

    A direct exchange routes a message to the queue whose routing key exactly matches the routing key of the message.

  • Scenarios

    A direct exchange can be used in scenarios in which messages are distinguished by simple string. In most cases, a direct exchange is used for the unicast routing of messages.

  • Example

    The following figure shows an example.

    direct_exchange

    Message

    Routing key

    Routing key in the binding

    Queue

    Message A

    img.create

    img.create

    Queue A

    Message B

    img.log

    img.log

    Queue B

Topic exchanges

  • Routing rule

    A topic exchange routes a message to the queues whose routing keys partially match the routing key of the message. An asterisk (*) or a number sign (#) is used as a wildcard in a routing key to support partial match. An asterisk (*) indicates a word. Example: cn. A number sign (#) indicates zero, one, or more words that are separated by periods (.). Example: cn.zj.hz.

  • Scenarios

    A topic exchange can be used in scenarios in which messages are distinguished by wildcard. In most cases, a topic exchange is used for the multicast routing of messages. For example, you can use a topic exchange to distribute geographic location data.

  • Example

    The following figure shows an example.

    topic_exchange

    Message

    Routing key

    Routing key in the binding

    Queue

    Message A

    files.cn.hz

    files.cn.hz.#

    Queue A

    Message B

    files.cn.hz.store

    • files.cn.hz.#

    • files.cn.*.store

    • Queue A

    • Queue B

    Message C

    files.cn.sz.store

    files.cn.*.store

    Queue B

Fanout exchanges

  • Routing rule

    A fanout exchange routes messages to all of the queues that are bound to it. Routing keys are ignored.

  • Scenarios

    A fanout exchange is ideal for the broadcast routing of messages. For example, you can use a fanout exchange in a distribution system to broadcast status or configuration updates.

  • Example

    The following figure shows an example.

    fanout_exchange

    Message

    Routing key

    Routing key in the binding

    Queue

    Message A

    img.create

    • files.cn.hz.#

    • queue.msgText

    • Queue A

    • Queue B

    Message B

    queue.msgMap

    • files.cn.hz.#

    • queue.msgText

    • Queue A

    • Queue B

    Message C

    cn.hz.topic.alarm

    • files.cn.hz.#

    • queue.msgText

    • Queue A

    • Queue B

Headers exchanges

  • Routing rule

    A headers exchange can be considered as a special direct exchange. It works in a similar way as direct exchanges. The difference lies in that a headers exchange routes messages to queues based on headers attributes instead of routing keys. When you bind a headers exchange to a queue, you must configure binding attributes in the key-value format for the binding. When you send a message to a headers exchange, you must configure headers attributes in the key-value format for the message. After a headers exchange receives a message, the exchange routes the message based on the matching between the headers attributes of the message and the binding attributes of the bound queues.

    A special binding attribute x-match is used to determine the matching method. The value of x-match can be set to all or any.

    • all: A headers exchange routes a message to a queue only if all binding attributes of the queue except for x-match match the headers attributes of the message.

    • any: A headers exchange routes a message to a queue if one or more binding attributes of the queue except for x-match match the headers attributes of the message.

    A headers attribute is considered to match a binding attribute in the following scenarios:

    • The key and value of the headers attribute are the same as those of the binding attribute.

    • The key of the headers attribute is the same as that of the binding attribute, but the value of the binding attribute is empty.

  • Scenarios

    A headers exchange can be used in scenarios in which messages are distinguished by multiple headers attributes. In most cases, a headers exchange is used for the multicast routing of messages. For example, you can use a headers exchange to distribute news that is classified or tagged.

  • Example

    The following figure shows an example.

    headers_exchange

    Message

    Headers attribute of the message

    Headers attribute of the binding

    Queue

    Message A

    type=read
    resource=group
    • type=read
      resource=group
      x-match=all
    • type=read
      resource=topic
      x-match=any
    • Queue A

    • Queue B

    Message B

    type=read
    type=read
    resource=topic
    x-match=any

    Queue B

    Message C

    type=write
    resource=topic
    type=read
    resource=topic
    x-match=any

    Queue B

x-delayed-message exchanges

ApsaraMQ for RabbitMQ is compatible with x-delayed-message exchanges provided by open source RabbitMQ. Open source RabbitMQ allows you to use x-delayed-message exchanges by installing a plug-in. To use x-delayed-message exchanges in ApsaraMQ for RabbitMQ, you do not need to install the plug-in. You need only to declare an exchange of the x-delayed-message type and set the x-delay header attribute of the message to specify the period of time after which the message is delivered. The period of time is in milliseconds. The message is delivered to the corresponding queue after the period of time specified by the x-delay header attribute elapses.

  • Routing rule

    An x-delayed-message exchange determines the routing rule based on the exchange type specified by the x-delayed-type extended attribute. The following types of exchanges support the x-delayed-message type:

  • Scenarios

    An x-delayed-message exchange can be used in scenarios where messages need to be delivered after a specified delay. For more information, see Delayed messages.

  • Example

    An x-delayed-message exchange routes messages based on the routing rule of the exchange type specified by the x-delayed-type extended attribute. The following figure shows an example on x-delayed-message exchanges whose x-delayed-type extended attribute is set to direct.

    x-delayed-message-exchange

    Message

    x-delay

    Routing key

    Routing key in the binding

    Queue

    Message A

    3000

    img.create

    img.create

    Queue A

    Message B

    4000

    img.log

    img.log

    Queue B

    The following items describe how the x-delayed-message exchange routes Message A and Message B:

    • After the x-delayed-message exchange receives Message A, the x-delayed-message exchange delivers Message A to Queue A after 3,000 milliseconds.

    • After the x-delayed-message exchange receives Message B, the x-delayed-message exchange delivers Message B to Queue B after 4,000 milliseconds.

x-consistent-hash exchanges

ApsaraMQ for RabbitMQ is compatible with x-consistent-hash exchanges provided by open source RabbitMQ. Open source RabbitMQ allows you to use x-consistent-hash exchanges by installing a plug-in. To use x-consistent-hash exchanges in ApsaraMQ for RabbitMQ, you do not need to install the plug-in. You need to only declare an exchange of the x-consistent-hash type.

Note

x-consistent-hash exchanges do not support message routing based on the hash property. You cannot use a message ID, correlation ID, or timestamp to calculate a route.

  • Routing rules

    • x-consistent-hash exchanges allow you to route messages based on the routing key or header value. After an x-consistent-hash exchange receives a message, the exchange performs hash calculation based on the routing key or header value of the message to determine to which bound queue the message is routed.

    • The hash value that is calculated by using the same routing key or header value is the same. As a result, messages that use the same routing key or header value are routed to the same queue.

    • If you route messages based on the header value, you must use the hash-header parameter to declare the exchange and the header that is to be used. Meanwhile, messages that are published by producers must contain the header that is specified by the hash-header parameter. If the specified header is not contained, all messages are routed to the same random queue.

    • If both the routing key and the hash-header parameter are specified, the hash calculation is performed based on the value of the hash-header parameter.

    • When you bind an x-consistent-hash exchange to a queue, you must specify a routing key whose value ranges from 1 to 20 to indicate the weight of the queue. A larger value specifies a higher weight. A higher weight indicates that the exchange receives more messages.

    • If the value of a routing key is greater than 20, the weight is considered to be 20.

    • If multiple bindings exist, only the first binding whose value is a positive integer is valid. Before you change the weight of a queue, you must delete the existing bindings.

  • Scenarios

    x-consistent-hash exchanges can be used in scenarios in which messages are categorized by weight.

  • Example

    An x-consistent-hash exchange routes messages to queues based on the hash value that is calculated by using a specified method. The weights of the queues that are bound to the exchange vary, and messages are routed to queues based on the weights. The following figure shows an example on x-consistent-hash exchanges whose routing rule is calculated based on a routing key.

    x-consistent-hash

    Message

    Routing key

    Message A

    cn.hz.1

    Message B

    cn.hz.2

    Message C

    cn.hz.3

    Queue

    Routing key in the binding

    Queue A

    1

    Queue B

    2

    The following items describe how an x-consistent-hash exchange routes Message A, Message B, and Message C:

    • After Message A, Message B, and Message C arrive at the exchange, the exchange delivers the messages to queues by using the hash value that is calculated based on the routing key.

    • The exchange is bound to Queue A and Queue B. The weight of Queue A is 1, and the weight of Queue B is 2. In this case, Message A, Message B, and Message C are routed to Queue A and Queue B based on a weight ratio of 1:2.