RabbitMQ. Part 2. Understanding Exchanges

Exchange - exchanger or exchange point. Messages are sent to it. Exchange distributes the message in one or more queues. He routes messages to a queue based on created links (bindings) between it and the queue.

Exchange is not Erlang process. For reasons of scalability exchange is a string (link to the module with the code where the routing logic is located) in the built-in database mnesia. 1 thousand exchangers will consume only 1MB of memory.

Table of contents

Direct Exchange

Direct exchange - used when needed deliver a message to specific queues. The message is published to the exchanger with a specific routing key and gets into all queues that are associated with this exchanger with a similar routing key. The routing key is a string. Matching is done using checking strings for equality.

Graphical representation of the message flow:

RabbitMQ. Part 2. Understanding Exchanges

В rabbitmq there is a concept default exchanger. It direct exchange no name. If the default exchanger is used, then the message will be routed to a queue with a name equal to message routing key.

Topic Exchange

Topic exchange – similar direct exchange enables selective routing by comparing the routing key. But, in this case, the key is given by pattern. When creating a template, use 0 or more words (letters AZ и az and numbers 0-9), separated by a dot, as well as symbols * и #.

  • * - can be replaced with exactly 1 word
  • # - can be replaced by 0 or more words

Graphical representation of the message flow:

RabbitMQ. Part 2. Understanding Exchanges

From version RabbitMQ 2.4.0 routing algorithm for topic exchange started working until 145 times faster. They achieved this by implementing the approach trie implementation, which implies the representation of templates as a tree structure. For example templates a.b.c, a.*.b.c, a.#.c и b.b.c will be represented by the following structure:

RabbitMQ. Part 2. Understanding Exchanges

Pattern matching is searched starting from the root and going from top to bottom.

Features:

  • the use of this exchanger can become a good choice for possible future app development, because templates can always be customized so that the message is published similarly direct exchange or fanout exchange
  • templates that use * much fasterthan templates that use #.
  • topic exchange slower direct exchange

Fanout Exchange

Fanout exchange - all messages are delivered to all queues even if a routing key is specified in the message.

Features:

  • RabbitMQ does not work with routing keys and templates which has a positive effect on performance. This is the fastest exchange;
  • all consumers must be able to process all messages;

Graphical representation of the message flow:

RabbitMQ. Part 2. Understanding Exchanges

Headers Exchange

Headers exchange - routes messages to related queues based on a comparison of pairs of (key, value) properties headers binding and similar message property. headers represents Dictionary<ключ, значение>.

If you add a special key to the dictionary x-match with the value of any, then the message is routed if the pairs (key, value) partially match. This behavior is similar to the operator or.

var bindingArguments = new Dictinary<String, Object>();
bindingArguments.add("x-match", "any");

Default key x-match contains a value all. This means that the message is routed when the pairs (key, value) completely match. This behavior is similar to the operator and.

Graphical representation of the message flow:

RabbitMQ. Part 2. Understanding Exchanges

Features:

  • additional flexibility
  • additional computational overhead. All pairs (key, value) of the attribute headers must be sorted by key name before calculating message routing values. Slower than other types of exchange.

Consistent Hashing Exchange

This exchanger is plugin и not built in в RabbitMQ.

Consistent-hashing exchange (hash-consistent exchange) - used when there are multiple queues that are potential recipients of a message and when you need to load balance between them. The message is associated with the queue by weight (a conditional string value from 0 - n).

Equivalent weight of queues - indicates that each queue will receive about the same amount messages (each message will be put into only one queue). There is no complete guarantee of uniform distribution of messages.

Graphical representation of the message flow:

RabbitMQ. Part 2. Understanding Exchanges

Hash computed based on routing key or property headers messages. If all published messages had different routing keys, or headers, then the distribution will occur by weight. Otherwise, the routing key will be used, or headers.

Should help when consumer throughput needs to grow higher than a solution with multiple consumers using the same queue.

Combination of exchangers (E2E)

The behavior of all exchangers can be combined using communication Exchange-to-Exchange (combination of exchangers is not included in the specification AMQP. This is a protocol extension from the side RabbitMQ).

Graphical representation of the message flow:

RabbitMQ. Part 2. Understanding Exchanges

By E2E we can find the right scalable configuration that meets both current and growing requirements.

Create an Exchange

The exchanger is created using a synchronous RPC request to the server. The request is made using the method Exchange.Declarecalled with parameters:

  • exchanger name
  • exchanger type
  • other options

Example of creation exchange by means of RabbitMQ.Client:

//...
channel.ExchangeDeclare(
    exchange: "my_exchange",
    type: "direct",
    durable: "false",
    autoDelete: "false",
    arguments: null
);
//...

  • exchange - the name of the exchanger that we want to create. Name must be unique
  • type - type of exchanger
  • durable - if installed truethen exchange will be permanent. It will be stored on disk and will be able to survive a server/broker restart. If the value falsethen exchange is temporary and will be removed when the server/broker is restarted
  • autoDelete - automatic deletion. Exchange will be deleted when all associated queues are deleted
  • arguments are optional arguments. Most often, through the arguments set alternative exchange (alternative exchanger). If a message cannot go through the original route, it can be sent to an alternate exchange to be routed along a different path.

RabbitMQ. Part 2. Understanding Exchanges

If creation exchange perhaps, then the server will send the client a synchronous RPC answer Exchange.DeclareOk. If creation impossible (there was a refusal on the request Exchange.Declare) then the channel will close server using an asynchronous command Channel.Close and the client will get an exception OperationInterruptedException, which will contain the error code and its description.

An exchanger must be created before posting. If you publish a message to some non-existent exchanger - RabbitMQ silently remove it.

Create an Exchange GUI

Go to the admin panel RabbitMQ under user guest (username: guest and password: guest). Please note that the user guest can only connect from localhost. Now let's go to the tab Exchanges and click on Add a new exchange. Fill in the properties:

RabbitMQ. Part 2. Understanding Exchanges

Most of the properties have been described above. Here we note that if we set Internal, then the exchange can only be used for E2E. Producer will not be able to send messages to such an exchange.

Conclusion

When developing a system, it is convenient to describe the topology routing using a graph. But before you start building a graph, it is worth highlighting the paths with high traffic, because. they require higher throughput (performance). Next, you can classify the traffic. And then start building.

If in the constructed graph there exists finite set routing keys, then, it is worth looking towards several fanout exchange, which are 1:1 related to the routing key. Remember that fanout exchange the fastest.

If the number of routes tends to infinity, it is worth paying attention to topic exchange or, if the template is not needed, then you can choose direct exchnge, because he is faster topic exchange.

Combinations of various exchange should help you find the right one. scalable configuration, which meets both current and growing system requirements.

Quantity exchange and queues should be minimal compared to the number of routes.

In the next article, we will begin to understand more about Queues and Bindings.

references

Source: habr.com

Add a comment