Distributed Message Service RabbitMQ

Explanation of Terms

2024-06-27 06:18:08

Vhost

Vhost (Virtual Host), similar to the concept of Namespace, is logically isolated. Each user can create multiple Vhosts, and each Vhost can create several Exchanges and Queues.

Queue

Message queue, each message will be put into one or more queues.

Producer

Message producer, the program that delivers messages.

Consumer

Message consumer, the program that receives messages.

Connection

TCP connection, the physical TCP connection between the producer or consumer and the message queue.

Connection connects the application with the DMS RabbitMQ. Connection performs underlying network tasks such as authentication, IP resolution, and routing. Establishing a connection between the application and the DMS RabbitMQ requires multiple TCP packet interactions, which consumes a significant amount of network resources and DMS RabbitMQ resources. A large number of connections will cause great pressure on the DMS RabbitMQ, and even trigger the flood attack protection of the DMS RabbitMQ SYN, resulting in the unresponsiveness of the DMS RabbitMQ, thus affecting the business.

In each physical TCP connection of the client, multiple channels can be established, and each channel represents a session task.

Channel is a virtual connection within a physical TCP connection. When an application establishes a connection with the DMS RabbitMQ through Connection, all AMQP protocol operations (such as creating queues, sending messages and receiving messages) are completed through the Channel in Connection. Channel can reuse Connection, that is, multiple Channels can be established under one Connection. Channel cannot exist independently of Connection, but must survive in Connection. When a Connection is disconnected, all Channels under that Connection are disconnected. When a large number of applications need to establish multiple connections with the DMS RabbitMQ, it is recommended that you use Channel to reuse Connection, thus reducing the consumption of network resources and DMS RabbitMQ resources.

 

Suggestions for using Connection and Channel

Keep the Connection long-lived and do not enable or disable the Connection frequently. If you do need to enable or disable the connection frequently, please use Channel.

A process corresponds to a Connection, and multiple threads in a process correspond to multiple Channels in a Connection.

Producer and Consumer use different Connections for message sending and consumption respectively.

Exchange

Producer sends messages to Exchange, which routes messages to one or more queues (or discards them), and Exchange routes messages to queues according to the corresponding binding logic.

Exchange Type

(1) Fanout: This type of routing rule is very simple, it will ignore the matching rules of the Routing Key and Binding Key, and route all messages sent to the Exchange to all queues bound to it, which is equivalent to the broadcast function. It is suitable for broadcasting messages.

Routing example:

Message

Routing   Key

Binding   Key

Queue

Message A

key.a

key.c.#

key.e

Queue A

Queue B

Message B

key.b

key.c.#

key.e

Queue A

Queue B

(2) Direct: This type of routing rule will route the message to the Queue whose Binding key exactly matches the Routing key; it is suitable for the scenario where the message is distinguished by a simple character identifier, and is often used in unicast routing.

Matching example:

Message

Routing   Key

Binding   Key

Queue

Message A

key.a

key.a

Queue A

Message B

key.b

key.b

Queue B

(3) Topic: This type is similar to the Direct type, except that the rules are not as strict and can be fuzzy and multi-conditional matching, i.e., this type of Exchange uses Routing key pattern matching and string comparisons to route the message to the bound Queue; supported wildcards include asterisks (*) and wellsigns (#). An asterisk (*) represents an English word (e.g. cn). The wellsign (#) represents zero, one or more English words, and English words are separated by English periods (.); it is suitable for scenarios in which messages are distinguished by wildcards, and is commonly used in multicast routing.

Routing example:

Message

Routing   Key

Binding   Key

Queue

Message A

key.a.b

key.a.b.#

Queue A

Message B

key.a.b.c

key.a.b.#

key.a.*.c

Queue A

Queue B

Message C

key.a.d.c

key.a.*.c

Queue B

Binding

A set of binding rules that tell Exchange which Queue messages should be stored to. Its function is to bind Exchange and Queue according to routing rules.

Routing Key

When the Producer sends a message to Exchange, it needs to specify a Routing key to set the routing rules for the message, and the Routing key needs to be used in conjunction with the Exchange type and Binding key to take effect.
Generally, the Exchange type and Binding key are configured. When sending messages to Exchange, the Producer can specify a Routing key to determine the Queue to which the messages are placed.


JXo5hMB53H5M