Member-only story
The Perfect Message Queue Solution Based on the Redis Stream Type
Build your own system easily
Redis 5.0 brings the Stream type. Literally, it is a stream type, but in fact, from a functional point of view, it should be a perfect implementation of Redis for message queues (MQ, Message Queue).
Anyone who has used Redis as a message queue knows that there are many implementations of message queues based on Reids, such as:
- PUB/SUB, subscribe/publish model.
- Implementation of List-based LPUSH+BRPOP.
- Implementation based on Sorted-Set.
Each implementation has typical features and problems.
The Stream type released in Redis 5.0 is also used to implement a typical message queue. The appearance of the Stream type almost satisfies all the contents of the message queue, including but not limited to:
- Serialization generation of the message ID.
- Message traversal.
- Blocking and non-blocking reads of messages.
- Packet consumption of messages.
- Processing of outstanding messages.
- Message queue monitoring.
The message queue has producers and consumers. Let’s experience the wonderfulness of the Stream type.
Production News
The XADD
command is used to append a message to a stream (stream data), as shown below:
127.0.0.1:6379> XADD memberMessage * user reggie msg Hello
"1553439850328-0"
127.0.0.1:6379> XADD memberMessage * user dwen msg World
"1553439858868-0"
The syntax format is:
XADD key ID field string [field string ...]
You need to provide the key
, message ID
scheme, and message content, where the message content is key-value
data.
ID
, the most commonly used*
, indicates that the message ID is generated by Redis, which is also a strongly recommended scheme.field string
[field string], is the content of the current message, consisting of one or more key values.