Redis Stream
Redis Stream is a data structure newly added in Redis version 5.0.
Redis Stream is mainly used for message queues (MQ, Message Queue). Redis itself has Redis publish/subscribe (pub/sub) to implement message queue functionality, but its drawback is that messages cannot be persisted; if a network disconnection or Redis downtime occurs, messages are discarded.
Simply put, publish/subscribe (pub/sub) can distribute messages but cannot record historical messages.
Redis Stream provides message persistence and master-replica replication, allowing any client to access data at any moment, remembering each client's access position, and ensuring messages are not lost.
The structure of Redis Stream is shown below; it has a message linked list that connects all added messages, and each message has a unique ID and corresponding content:

Each Stream has a unique name, which is the Redis key, automatically created when we first append a message using the xadd command.
Parsing the figure above:
- Consumer Group : consumer group, created with the XGROUP CREATE command; a consumer group has multiple consumers.
- last_delivered_id : cursor; each consumer group has a cursor last_delivered_id, and any consumer reading a message moves the cursor last_delivered_id forward.
- pending_ids : the state variable of the consumer (Consumer), used to maintain the consumer's unacknowledged ids. pending_ids records the messages that have been read by the client but have not yet been ack (Acknowledge character).
Commands related to the message queue:
- XADD - add a message to the end
- XTRIM - trim the stream, limiting its length
- XDEL - delete a message
- XLEN - get the number of elements in the stream, i.e., the message length
- XRANGE - get the message list, automatically filtering deleted messages
- XREVRANGE - get the message list in reverse, IDs from large to small
- XREAD - get the message list in blocking or non-blocking mode
Commands related to consumer groups:
- XGROUP CREATE - create a consumer group
- XREADGROUP GROUP - read messages in a consumer group
- XACK - mark a message as "processed"
- XGROUP SETID - set a new last delivered message ID for the consumer group
- XGROUP DELCONSUMER - delete a consumer
- XGROUP DESTROY - delete a consumer group
- XPENDING - show information about pending messages
- XCLAIM - transfer message ownership
- **XINFO** - view information about the stream and consumer groups;
- XINFO GROUPS - print consumer group information;
- XINFO STREAM - print stream information
XADD
Use XADD to add a message to the queue; if the specified queue does not exist, a queue is created. XADD syntax format:
XADD key ID field value [field value ...]
- key : queue name; created if it does not exist
- ID : message id; we use * to indicate it is generated by redis, but it can be customized as long as you ensure it is incrementing.
- field value : record.
Example
redis
XADD mystream
name Sara surname OConnor
"1601372323627-0"
redis
XADD mystream
field1 value1 field2 value2 field3 value3
"1601372323627-1"
redis
XLEN mystream
(
integer
)
2
redis
XRANGE mystream - +
1
)
1
)
"1601372323627-0"
2
)
1
)
"name"
2
)
"Sara"
3
)
"surname"
4
)
"OConnor"
2
)
1
)
"1601372323627-1"
2
)
1
)
"field1"
2
)
"value1"
3
)
"field2"
4
)
"value2"
5
)
"field3"
6
)
"value3"
redis
XTRIM
Use XTRIM to trim the stream and limit its length. Syntax format:
XTRIM key MAXLEN [~] count
- key : queue name
- MAXLEN : length
- count : count
Example
127.0.0.1:
6379
XADD mystream
field1 A field2 B field3 C field4 D
"1601372434568-0"
127.0.0.1:
6379
XTRIM mystream MAXLEN
2
(
integer
)
0
127.0.0.1:
6379
XRANGE mystream - +
1
)
1
)
"1601372434568-0"
2
)
1
)
"field1"
2
)
"A"
3
)
"field2"
4
)
"B"
5
)
"field3"
6
)
"C"
7
)
"field4"
8
)
"D"
127.0.0.1:
6379
redis
XDEL
Use XDEL to delete messages. Syntax format:
XDEL key ID [ID ...]
- key: queue name
- ID : message ID
Example
XADD mystream
a
1
1538561698944
0
XADD mystream
b
2
1538561700640
0
XADD mystream
c
3
1538561701744
0
XDEL mystream
1538561700640
0
(
integer
)
1
127.0.0.1:
6379
XRANGE mystream - +
1
)
1
)
1538561698944
0
2
)
1
)
"a"
2
)
"1"
2
)
1
)
1538561701744
0
2
)
1
)
"c"
2
)
"3"
XLEN
Use XLEN to get the number of elements in the stream, i.e., the message length. Syntax format:
XLEN key
- key: queue name
Example
redis
XADD mystream
item
1
"1601372563177-0"
redis
XADD mystream
item
2
"1601372563178-0"
redis
XADD mystream
item
3
"1601372563178-1"
redis
XLEN mystream
(
integer
)
3
redis
XRANGE
Use XRANGE to get the message list, automatically filtering deleted messages. Syntax format:
XRANGE key start end [COUNT count]
- key : queue name
- start : start value, - means the minimum value
- end : end value, + means the maximum value
- count : count
Example
redis
XADD writers
name Virginia surname Woolf
"1601372577811-0"
redis
XADD writers
name Jane surname Austen
"1601372577811-1"
redis
XADD writers
name Toni surname Morrison
"1601372577811-2"
redis
XADD writers
name Agatha surname Christie
"1601372577812-0"
redis
XADD writers
name Ngozi surname Adichie
"1601372577812-1"
redis
XLEN writers
(
integer
)
5
redis
XRANGE writers - + COUNT
2
1
)
1
)
"1601372577811-0"
2
)
1
)
"name"
2
)
"Virginia"
3
)
"surname"
4
)
"Woolf"
2
)
1
)
"1601372577811-1"
2
)
1
)
"name"
2
)
"Jane"
3
)
"surname"
4
)
"Austen"
redis
XREVRANGE
Use XREVRANGE to get the message list, automatically filtering deleted messages. Syntax format:
XREVRANGE key end start [COUNT count]
- key : queue name
- end : end value, + means the maximum value
- start : start value, - means the minimum value
- count : count
Example
redis
XADD writers
name Virginia surname Woolf
"1601372731458-0"
redis
XADD writers
name Jane surname Austen
"1601372731459-0"
redis
XADD writers
name Toni surname Morrison
"1601372731459-1"
redis
XADD writers
name Agatha surname Christie
"1601372731459-2"
redis
XADD writers
name Ngozi surname Adichie
"1601372731459-3"
redis
XLEN writers
(
integer
)
5
redis
XREVRANGE writers + - COUNT
1
1
)
1
)
"1601372731459-3"
2
)
1
)
"name"
2
)
"Ngozi"
3
)
"surname"
4
)
"Adichie"
redis
XREAD
Use XREAD to get the message list in blocking or non-blocking mode. Syntax format:
XREAD [COUNT count] [BLOCK milliseconds] STREAMS key [key ...] id [id ...]
- count : count
- milliseconds : optional, blocking milliseconds; non-blocking mode if not set
- key : queue name
- id : message ID
Example
Read two messages from the head of the Stream
XREAD COUNT
2
STREAMS mystream writers
0
0
0
0
1
)
1
)
"mystream"
2
)
1
)
1
)
1526984818136
0
2
)
1
)
"duration"
2
)
"1532"
3
)
"event-id"
4
)
"5"
5
)
"user-id"
6
)
"7782813"
2
)
1
)
1526999352406
0
2
)
1
)
"duration"
2
)
"812"
3
)
"event-id"
4
)
"9"
5
)
"user-id"
6
)
"388234"
2
)
1
)
"writers"
2
)
1
)
1
)
1526985676425
0
2
)
1
)
"name"
2
)
"Virginia"
3
)
"surname"
4
)
"Woolf"
2
)
1
)
1526985685298
0
2
)
1
)
"name"
2
)
"Jane"
3
)
"surname"
4
)
"Austen"
XGROUP CREATE
Use XGROUP CREATE to create a consumer group. Syntax format:
XGROUP [CREATE key groupname id-or-$] [SETID key groupname id-or-$] [DESTROY key groupname] [DELCONSUMER key groupname consumername]
- key : queue name; created if it does not exist
- groupname : group name.
- $ : means start consuming from the tail, accepting only new messages; the current Stream messages are all ignored.
Consume from the head:
XGROUP CREATE mystream consumer-group-name 0-0
Consume from the tail:
XGROUP CREATE mystream consumer-group-name $
XREADGROUP GROUP
Use XREADGROUP GROUP to read messages in a consumer group. Syntax format:
XREADGROUP GROUP group consumer [COUNT count] [BLOCK milliseconds] [NOACK] STREAMS key [key ...] ID [ID ...]
- group : consumer group name
- consumer : consumer name.
- count : read count.
- milliseconds : blocking milliseconds.
- key : queue name.
- ID : message ID.
XREADGROUP GROUP consumer-group-name consumer-name COUNT 1 STREAMS mystream >