Message Queue Middleware: A Distributed System for Decoupling and Scalability
Introduction
Message Queue Middleware is a critical component of modern distributed systems, designed to address issues of coupling, asynchronous messaging, and traffic management. This article explores the concept of message queues, their applications in real-world scenarios, and exemplary messaging middleware solutions.
Message Queue Scenarios
Message queues are commonly used in practical application scenarios, including asynchronous processing, decoupling, traffic management, and log processing.
2.1 Asynchronous Processing
Scene Description: After user registration, we need to send registered mail and SMS registration.
In traditional serial fashion, the application processes the registration information, sends the registered mail, and then sends the registration SMS. However, this approach can lead to performance bottlenecks and decreased throughput. By introducing a message queue, the service logic can be decoupled, and the application can process requests asynchronously.
Example:
Suppose we have a CPU with a throughput of 100 requests per second. In serial fashion, the application can process 7 requests per second (1000/150), while in parallel fashion, it can process 10 requests per second (1000/100). By using a message queue, the system can achieve a throughput of 20 requests per second, which is three times that of serial fashion and twice that of parallel fashion.
2.2 Application of Decoupling
Scene Description: After the user orders, the order system needs to notify the inventory system.
In traditional approaches, the order system and inventory system are tightly coupled, making it difficult to manage and scale. By introducing a message queue, the order system can write messages to the queue and return to the user, while the inventory system can subscribe to the queue and process the messages independently.
Example:
Suppose the inventory system is not accessible. In traditional approaches, the order system will fail, resulting in the order being failed. However, with a message queue, the order system can write the message to the queue and return to the user, while the inventory system can process the message independently.
2.3 Flow Cut Front
Scene Description: Spike activity, usually because of too much traffic, leading to traffic surge, application hang.
To solve this problem, a message queue can be introduced in front of the application. The user’s request is written to the queue, and if the queue size exceeds the maximum, the user request is discarded, and an error page is displayed.
2.4 Log Processing
Scene Description: Log processing means in the message queue with log processing, such as Kafka applications.
In traditional approaches, log data is collected and written to a database. However, this approach can lead to performance bottlenecks and decreased throughput. By introducing a message queue, the log data can be collected and written to the queue, and the log processing application can subscribe to the queue and process the messages independently.
Example:
Suppose we have a log collection client that collects log data and writes it to the queue. The log processing application can subscribe to the queue and process the log data independently.
2.5 Message Communications
Scene Description: Message communication refers to the efficient communication mechanism built using message queues.
In traditional approaches, point-to-point communication is used, where each message is sent to a specific recipient. However, this approach can lead to performance bottlenecks and decreased throughput. By introducing a message queue, point-to-point communication can be replaced with publish-subscribe communication, where each message is sent to a topic, and multiple recipients can subscribe to the topic.
Example:
Suppose we have a client A and client B that need to communicate with each other using a message queue. The message queue can be used to send messages from client A to client B, and client B can subscribe to the queue to receive the messages.
Third, the Exemplary Messaging Middleware
Message queues are highly available persistent messaging middleware. For example, Active MQ, Rabbit MQ, and Rocket Mq can be used as message queues.
Example:
Suppose we have an e-commerce system that needs to send messages to the inventory system. We can use a message queue, such as Active MQ, to send the messages from the e-commerce system to the inventory system.
4.1 Message Model
In the JMS specification, there are two message models: P2P (Point to Point) and Pub / Sub (Publish / Subscribe).
4.1.1 P2P Mode
P2P mode comprises three roles: a message queue, a sender, and receivers. Each message is sent to a particular queue, and the receiver acquires the message from the queue. P2P features include:
- Each message is only consumed once (ie, once consumed, the message is no longer in the message queue)
- Between sender and receiver is not dependent on time, that is to say when the sender sent the message, whether the recipient has no running, it will not affect the message is sent to the queue
- Upon successful reception of the recipient needs to reply to the message queue success
4.1.2 Pub / Sub Mode
Pub / Sub mode contains three roles: topics, publishers, and subscribers. More publishers send a message to Topic, the system will deliver these messages to multiple subscribers. Pub / Sub features include:
- Each message can have multiple consumers
- There are dependent on the time between publishers and subscribers
- After for a theme (Topic) subscribers, it must create a subscriber, the publisher’s message to the consumer
- In order to consume the message, the subscriber must keep the state running
- In order to alleviate such strict time correlation, JMS allows subscribers to create a persistent subscription
4.2 Consumer News
In JMS, produce and consume messages are asynchronous. For the consumer, JMS messages may be consuming messages in two ways:
- Synchronous: Subscriber or recipient of the message received by the receive method, method blocks receive (or timeout before) prior to receiving the message
- Asynchronous: Subscribers or the recipient can be registered as a message listener. When the message arrives, the system automatically calls onMessage method listener.
4.3 JMS Programming Model
JMS programming model includes two separate QueueConnectionFactory and TopicConnectionFactory. You can find the ConnectionFactory object via JNDI.
Destination
Destination means of sending messages in the message producer or message target consumer sources. For news producer, its Destination is a queue (Queue) or a topic (Topic); for news consumers, it is also one of the above.
Conclusion
Message Queue Middleware is a critical component of modern distributed systems, designed to address issues of coupling, asynchronous messaging, and traffic management. By introducing a message queue, applications can be decoupled, and scalability can be improved. This article has explored the concept of message queues, their applications in real-world scenarios, and exemplary messaging middleware solutions.