There is no best, only the right

preface

RabbitMQ is a great example of MQ and has a place in many MQ implementations with its reliability, low latency and other features, and is often asked about MQ in interviews.

How to set up RabbitMQ high availability mode?

RabbitMQ has three modes: Single-machine mode, Common cluster mode, and Mirrored cluster mode. The single-machine mode is unnecessary. There is also no high availability in normal clustering mode, where multiple RabbitMQ instances are started but each queue will only be placed in one RabbitMQ instance. If A consumer consumes A queue from B, then B will need to pull messages from A, and if A dies, it will not be able to consume.

RabbitMQ Cluster mode

Mirror mode consists of a master and slaves distributed on different nodes. Except for publish, all actions are only sent to the master. The master broadcasts the command execution results to the slave. The slave executes the commands exactly in the same order as the master executes the commands. If the master dies, the messages sent by the producer are stored on other slaves, and the slave can be promoted to master to consume messages again!

How can RabbitMQ ensure that data is not lost?

The previous article covered three ways to prevent data loss: on the production side, on the queue itself, and on the consumer side 👍

  • Production: Confirm mode is recommended. This will assign a unique ID to each message written to RabbitMQ, and RabbitMQ will send you an ACK message indicating that the message was successfully sent. If RabbitMQ fails to process the message, it will return a nACK message telling you that the message failed to be received.
  • The queue itself: Enable RabbitMQ persistence. Once written messages are persisted to disk and can be recovered even if the machine is down.
  • Consumer: Confirm that the message has been consumed correctly through the manual comfirm mechanism.

How to set RabbitMQ persistence?

  1. This ensures RabbitMQ will persist the metadata of the queue, but it will not persist the data in the queue.
  2. Set the deliveryMode of the message to 2 when sending the message and set the message to persist.

What components and functions does RabbitMQ have?

  • ConnectionFactory: The manager for establishing connections between applications and RabbitMQ.
  • A Channel is a virtual connection created over “real” TCP. AMQP commands are sent through a Channel. Each Channel has a unique ID.
  • Exchange: Used to forward messages. The producer’s message goes to the Exchange first. The Exchange queries the BindingKey table based on the message’s RoutingKey and sends the message to the appropriate Queue.

There are three types of Exchange in common use:

  1. Fanout: If the switch receives a message, it broadcasts it to all bound queues.
  2. Direct: If the RoutingKey matches exactly, the message is delivered to the appropriate queue.
  3. Topic: Enables messages from different sources to reach the same queue. A RoutingKey can be forwarded as long as it matches the Routing table (BindingKey table), and wildcards can be matched when using topic switches.
  • Queue: Used to store messages from producers.
  • RoutingKey: Producer messages are allocated to different queues based on routingKeys.
  • BindingKey: Used to establish a connection between exchanges and queues. Exchange creates a query table for binding information, which is used to route messages to different queues.
  • VHost: Virtual host, which is not strictly a component but a concept. Multiple vhosts can be set up within a broker (RabbitMQ instance) to separate permissions between different users.

Why do RabbitMQ use Channel rather than TCP to send commands directly?

Given that TCP creation and destruction are very expensive and that the operating system can create only a limited number of TCP per second, peak business times create a large number of connections, waste resources, and can lead to system bottlenecks. If we use one TCP connection per request, we can meet the performance requirements and ensure the privacy of each connection, perfect 👍.

How does RabbitMQ implement delayed message queuing?

  1. After the message expires, it enters the dead letter exchange and is forwarded to the delay consumption queue by the exchange to realize the delay function.
  2. Use the Rabbitmq-delayed -message-exchange plug-in for delay.

Is RabbitMQ in push or pull mode

RabbitMQ supports both modes, but mainly uses push mode, channel.basicconsume (QUEUE_NAME, false, consumer). Push mode actively pushes messages to consumers. Proactively avoids a backlog of messages. Pull mode can use channel.basicget (QUEUE_NAME, false) to fetch messages, mainly used when pulling messages in batches.

Toodles!