Two modes of message queue

The Java Message Service (JMS) application program interface is an API for message-oriented middleware (MOM) in the Java platform, which is used to send messages between two applications, or in a distributed system, for asynchronous communication .
Point-to-point and publish-subscribe were originally defined by JMS. The main difference or problem between these two modes is whether the messages sent to the queue can be repeatedly consumed (multiple subscriptions)

1. Definition

The JMS specification currently supports two message models: point to point (queue) and publish/subscribe (publish/subscribe, topic).

1.1. Point-to-point: Queue, non-repeatable consumption

The message producer produces the message and sends it to the queue, and then the message consumer takes it out of the queue and consumes the message.
After the message is consumed, there is no more storage in the queue, so it is impossible for the message consumer to consume the message that has been consumed. Queue supports the existence of multiple consumers, but for a message, only one consumer can consume it.

1.2. Publish/Subscribe: Topic, which can be consumed repeatedly

A message producer (publishing) publishes a message to a topic, and multiple message consumers (subscribing) consume the message at the same time. Unlike the peer-to-peer approach, messages published to a topic are consumed by all subscribers.

Publish-subscribe mode that supports subscription groups: In the
publish-subscribe mode, when the publisher has a large amount of messages, it is obvious that the processing capacity of a single subscriber is insufficient. In fact, in the real scenario, multiple subscriber nodes form a subscription group to load balance the consumption of topic messages, that is, group subscriptions, so that the subscribers can easily achieve linear expansion of consumption capacity. It can be seen that there are multiple Queues under a topic, each Queue is a point-to-point method, and the Queues are published and subscribed.

2. Difference

2.1, peer-to-peer mode

The producer sends a message to the queue. A queue can have many consumers, but a message can only be accepted by one consumer. When no consumer is available, the message will be saved until there is an available consumer, so the Queue implementation a reliable load balancer.

2.2, publish-subscribe model

The message sent by the publisher to the topic, only the subscribers who subscribe to the topic will receive the message. Topic implements publish and subscribe. When you publish a message, all services that subscribe to this topic can get the message, so from 1 to N subscribers can get a copy of the message.

3. Comparison of popular models

The traditional enterprise message queue ActiveMQ follows the JMS specification and implements the point-to-point and publish-subscribe model, but other popular message queues, RabbitMQ and Kafka, do not follow the JMS specification.

3.1、RabbitMQ

RabbitMQ implements the AQMP protocol, which defines the message routing rules and methods. The producer sends messages to different queues through routing rules, and the consumer consumes messages according to the queue name.
RabbitMQ supports both memory queues and persistent queues. The consumer is a push model. The consumption status and subscription relationship are maintained by the server. Messages are deleted immediately after consumption, and historical messages are not retained.

(1) The point-to-point
producer sends a message to the Queue through routing, and only one consumer can consume it.


(2) Multiple subscriptions
When RabbitMQ needs to support multiple subscriptions, the messages sent by the publisher are written to multiple Queues at the same time through routing, and different subscription groups consume different Queues. Therefore, when multiple subscriptions are supported, the message will be copied multiple times.

3.2、Kafka

Kafka only supports message persistence. The consumer is a pull model. The client is responsible for maintaining the consumption status and subscription relationship. After the message is consumed, it will not be deleted immediately, and historical messages will be retained. Therefore, when multiple subscriptions are supported, only one copy of the message can be stored. But there may be duplication of consumption.

(1) Point-to-point & multi-subscription
publishers produce a message into topic, and different subscription groups consume this message.


 
 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325879152&siteId=291194637
Recommended