Updated on 2024-10-18 GMT+08:00

Exchanges

Direct, fanout, topic, headers, x-delayed-message, and x-consistent-hash exchanges are available.

Direct Exchange

How It Works

  1. A queue is bound to a direct exchange with a routing key.
  2. The direct exchange routes a received message to the bound queue whose routing key is matched.

Routing

Based on a routing key matching

Scenario

Unicast routing

Example

Figure 1 Example direct exchange

As shown in this chart, Message A will be sent to Queue 1 and Queue 2. Message B will be sent to Queue 3.

Fanout Exchange

How It Works

A fanout exchange bound with multiple queues routes received messages to each queue. Fanout exchanges forward messages faster than other exchanges.

Routing

The fanout exchange delivers messages to all bound queues.

Scenario

Broadcast routing

Example

Figure 2 Example fanout exchange

As shown in this chart, Message A will be sent to Queue 1 and Queue 2.

Topic Exchange

How It Works

  1. A queue is bound to a topic exchange with a routing key that includes a wildcard.
  2. The topic exchange routes a received message to the queue if the message's routing key wildcard is matched.

Supported wildcards are stars (*) and hashes (#). Separate wildcards and words by periods (.), for example, test.#.

  • * matches one word.
  • # matches zero or more words.

Routing

Based on a routing key wildcard matching

Scenario

Multicast routing

Example

Figure 3 Example topic exchange

As shown in this chart, Message A will be sent to Queue 1 and Queue 2, Message B to Queue 1, Queue 2, and Queue 3, and Message C to Queue 3.

Headers Exchange

How It Works

  1. A queue is bound to a headers exchange with a binding expressed in a key-value pair.
  2. The headers exchange routes a message to the queue if the binding matches the message's key-value header.

The matching algorithm uses a specific binding key-value pair, which is x-match. Values:

  • all: Messages are routed only when all header pairs match.
  • any: Messages are routed when any header pair matches.

Routing

Based on matching between key-value pairs in the message headers and the binding (a key-value pair)

Example

Figure 4 Example headers exchange

As shown in this chart, Message A will be sent to Queue 1 and Queue 2, and Message B to Queue 2.

x-delayed-message Exchange

How It Works

  1. An x-delayed-message exchange is created with one of these exchange types: Direct Exchange, Fanout Exchange, Topic Exchange, and Headers Exchange. The exchange type specifies the routing rules.
  2. A queue is bound to the x-delayed-message exchange.
  3. A message with the header attribute x-delay is sent to the x-delayed-message exchange. x-delay specifies the message delivery schedule, in milliseconds.
  4. The x-delayed-message exchange does not deliver the message immediately after receiving it. The message will be routed as scheduled based on the exchange type-specified routing rules.

Routing

Messages are routed based on the rules of the exchange type set in the x-delayed-message exchange creation.

Scenario

x-delayed-message exchanges are applicable to scheduled message delivery.

Example

This example uses a direct exchange.

Figure 5 Example of an x-delayed-message exchange

As shown in the preceding figure, the x-delayed-message exchange receives message A, waits 100 ms, and sends it to queue 1 and queue 2.

x-consistent-hash Exchange

How It Works

  1. A queue is bound to an x-consistent-hash exchange with a routing key. The key must be a positive integer. A routing key indicates the weight of the queue. The larger the key, the heavier the weight, which means that the queue receives more messages.
  2. A message with a routing key is sent to the x-consistent-hash exchange. The exchange calculates a hash value based on the key, and distributes the message to the hashed queue.

Routing

x-consistent-hash exchange calculates a hash value based on a routing key, and routes a message to the hashed queue.

Example

Figure 6 Example of an x-consistent-hash exchange

As shown in the preceding figure, x-consistent-hash exchange receives messages A and B with routing keys, calculates hash values based on the keys, and routes the messages to the hashed queues. Queue 1 weighs 1 and queue 2 weighs 2. In this case, queue 2 receives twice as many messages as queue 1.