preface
If you want to implement full message queuing capabilities, such as reliability assurance, persistence, broadcast mode, delay queuing, etc., using Redis to implement MQ is obviously unwise. But if you don’t want to introduce a heavyweight MQ component, you can just use Redis to implement simple MQ. There are three main schemes for Redis to implement MQ :(1) List structure; (2) Pub/Sub mode; (3) Stream structure.
1. Use the List structure to implement message queues
Plan 1Use:LPUSH
To send a message to a queue, useRPOP
Consume messages from the queue.
The problem: Simple message queue functionality is implemented, but there are huge performance problems, and consumers need to iterate over and over againRPOP
Poll for messages, even when there are no messages.
Scenario 2: Use LPUSH to send messages to the queue and BRPOP to consume messages. Advantages: BRPOP supports an incoming timeout period, expressed in seconds, in which the system waits for a result to be returned. If this parameter is set to 0, the system waits until a result is returned. This eliminates the need for circular retries when there is no message, which partly overcomes the disadvantage of Scenario 1.
Plan 3Use:LPUSH
To send a message to a queue, useBRPOPLPUSH
To consume and back up messages.
advantages: Backup messages ensure reliability to a certain extent. Consumers can consume messages again when processing fails, and delete backup messages when consuming successfully.
BRPOPLPUSH
You need to pass in two arguments, the first is the list key to fetch the data and the second is the list key to put the message into. BRPOP and LPUSH are two steps from lua script.
2. Use Pub/Sub to implement message queues
Option 1: Publish to channel and subcribe to subscribe to channel messages. Advantages: It is equivalent to sending messages in broadcast mode, and Subcribe can be continuously subscribed, without constant polling by the upper layer of the business. Disadvantages: Cannot persist messages, can only receive instant messages, cannot receive history messages, requires subscribe must be executed before publish.
Scheme 2usepublish
Sends a message to a channel usingpsubscribe
To subscribe to a particular schema channel.
advantagesThrough:psubscribe
You can subscribe to messages for channels that fit a particular schema, similar to the topic switches in RabbitMQ. The application scenarios can be more extensive.
3. Use Stream to implement message queuing
Redis Stream provides persistence capabilities compared to PUB/Sub, and adds the concept of some complete MQ components, such as consumer groups. I’ll just show you how to implement a simple message queue using Stream.
(1) Use XADD to publish messages
xadd mystream * name jiangwang age 26
Copy the code
mystream
Redis is key;- while
*
Is the message ID, which is used*
Indicates that Redis generates the message ID itself (it can also be specified, but the ID must be unique). The default generated message ID format is:Timestamp _ Index of messages in the same timestamp
. - At the back of the
The message body
Group is morefield-value
For example, name is filed, and Jiangwang is value.
(2) Use xread to read messages in the specified interval
xread COUNT 2 BLOCK 0 STREAMS mystream 0-0
Copy the code
- Among them
COUNT
Represents the number of messages to be read (if count is not specified, it will be read until the latest message is read), where count is 2; BLOCK
Indicates the blocking time. 0 indicates waiting until there is a message.STREAMS
Represents the stream key to read;0-0
Said to startMessage ID
And the format is the sameTimestamp _index
; ifMessage ID
Set to$
, then only one message will be read from the current.
summary
Three ways of using Redis to implement message queue are studied and recorded, the advantages and disadvantages of different schemes are analyzed, and practical examples are given.
Refer to the website
https://mp.weixin.qq.com/s/_q0bI62iFrG8h-gZ-bCvNQ
Copy the code