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:LPUSHTo send a message to a queue, useRPOPConsume 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 againRPOPPoll 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:LPUSHTo send a message to a queue, useBRPOPLPUSHTo 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.

BRPOPLPUSHYou 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 2usepublishSends a message to a channel usingpsubscribeTo subscribe to a particular schema channel.

advantagesThrough:psubscribeYou 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
  • mystreamRedis 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 theThe message bodyGroup is morefield-valueFor 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 themCOUNTRepresents 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;
  • BLOCKIndicates the blocking time. 0 indicates waiting until there is a message.
  • STREAMSRepresents the stream key to read;
  • 0-0Said to startMessage IDAnd the format is the sameTimestamp _index; ifMessage IDSet 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