introduce

Redis is best known for its in-memory database. However, some systems use it as a message queue management tool. Pub/Sub and RPOPLPUSH are two sets of commands used to implement such a system. In this article, I’ll share some knowledge about both command sets, their use cases, and their pros and cons.

PUBLISH/SUBSCRIBE

Suppose Pub/Sub is like a radio station, and all subscribers to the queue will receive all messages published to the queue.

How does it work

Consumers C1, C2, and C3 subscribe to queues Q Producer P publishes messages m to queues Q Queue Q sends messages to all consumers C1, C2, and C3

example

A group chat system is a typical example of this type of message queue, where a user sends a message to one group and all other group members need to receive the message. Pub/Sub is a great tool to ensure that messages are delivered to all subscribers.

When not to use it

Because of the efficiency of the memory buffer, if the consumer loses a connection to the queue, it is likely that the consumer will lose the message when the connection is lost. The Redis server decides to clear the message buffer to save more memory for the next incoming message.

RPOPLPUSH

RPOPLPUSH(Reliable queue mode) works differently. The implementation of message queue management comes from the client, not the Redis server.

How does it work

Queue Q is a list in Redis. Producers LPUSH messages M1, M2, m3 to list Q. Consumer C1 consumes messages from list Q by using the command RPOPLPUSH to pop message M1 from list Q and push the message to another work list q-C1-working. If C1 successfully uses M1, it removes message M1 from the work list q-C1-working. If C1 fails to use the message, it will either re-queue M1 to the original queue Q or reject message M1 and move it to the rejected queue Q-Rejected according to the business logic. Consumers C2 and C3, in turn, process messages in the same flow as C1, but with different messages. Only one consumer successfully used a particular message.

example

This mechanism is useful when a message is successfully consumed by one and only one consumer.

When not to use it

At least three queues are created: the primary queue, the work queue, and the reject queue. In addition, each consumer has his or her own work queue. When you look at the queue list, it’s a little tricky.

Welcome to follow my official account: Qulingfeng, for exclusive learning resources and daily dry goods push. If you are interested in my topics, you can also follow my blog: blog.qulingfeng.com