XADD mystream * name Alice age 30
XADD mystream * sensor-id 1 temperature 23.5
XADD mystream 1640995200000-0 event login user 42
Use `*` to auto-generate the entry ID (timestamp-sequence format).
XADD mystream MAXLEN 1000 * message hello
XADD mystream MAXLEN ~ 1000 * message hello
XREAD COUNT 10 STREAMS mystream $
XREAD COUNT 5 BLOCK 5000 STREAMS mystream 1640995200000-0
XREAD STREAMS stream1 stream2 0-0 0-0
`$` means only new entries. `BLOCK` makes it wait up to ms for new data.
XREAD COUNT 10 BLOCK 0 STREAMS mystream $
XRANGE mystream - +
XRANGE mystream 1640995200000-0 1640995260000-0
XRANGE mystream - + COUNT 5
XRANGE mystream 1640995200000-0 (1640995200000-0 COUNT 1
`-` is the oldest ID, `+` is the newest. `(` denotes an exclusive bound.
XREVRANGE mystream + - COUNT 3
XREVRANGE mystream + 1640995200000-0 COUNT 10
XLEN mystream
Returns the number of entries in the stream.
XLEN mystream
XLEN orders
XLEN events
XDEL mystream 1640995200000-0
XDEL mystream id1 id2 id3
XTRIM mystream MAXLEN 1000
XTRIM mystream MAXLEN ~ 5000
XTRIM mystream MINID 1640995200000-0
`~` enables approximate trimming for performance. `MINID` removes entries older than the given ID.
XREADGROUP GROUP mygroup consumer1 COUNT 1 STREAMS mystream >
XREADGROUP GROUP mygroup consumer1 BLOCK 5000 COUNT 10 STREAMS mystream >
XREADGROUP GROUP mygroup consumer1 COUNT 1 STREAMS mystream 0
`>` receives only new messages. `0` receives pending (unacknowledged) messages for this consumer.
XREADGROUP GROUP mygroup consumer1 COUNT 10 STREAMS s1 s2 > >
XACK mystream mygroup 1640995200000-0
XACK mystream mygroup id1 id2 id3
Acknowledges successful processing of a message, removing it from the pending entries list.
XACK mystream mygroup $(XREADGROUP GROUP mygroup consumer1 COUNT 1 STREAMS mystream > | grep -oP '\d+-\d')
XGROUP CREATE mystream mygroup $
XGROUP CREATE mystream mygroup 0 MKSTREAM
XGROUP CREATE mystream mygroup 1640995200000-0
`$` starts from new messages only. `0` starts from the beginning. `MKSTREAM` creates the stream if missing.
XGROUP DESTROY mystream mygroup
XGROUP DELCONSUMER mystream mygroup consumer1
XGROUP SETID mystream mygroup $
XPENDING mystream mygroup
XPENDING mystream mygroup - + 10
XPENDING mystream mygroup - + 10 consumer1
Shows pending (delivered but unacknowledged) messages. The summary form returns total count, min/max ID, and consumers.
XPENDING mystream mygroup 1640995200000-0 + 20 consumer1
XCLAIM mystream mygroup consumer1 3600000 id1 id2
XCLAIM mystream mygroup consumer1 0 id1
XCLAIM mystream mygroup consumer1 60000 id1 JUSTID
Transfers pending messages from one consumer to another. Time is min idle time in ms.
XAUTOCLAIM mystream mygroup consumer1 3600000 0-0 COUNT 10
`XAUTOCLAIM` automatically claims messages idle longer than the threshold.
XINFO STREAM mystream
XINFO GROUPS mystream
XINFO CONSUMERS mystream mygroup
Returns detailed metadata about the stream, its consumer groups, and consumers within a group.
XINFO STREAM mystream FULL
XINFO STREAM mystream FULL COUNT 5
XADD mystream MAXLEN 10000 * field value
XADD mystream MAXLEN ~ 10000 * field value
XTRIM mystream MAXLEN = 10000
XTRIM mystream MAXLEN ~ 10000
Use `~` (approximate) for performance — Redis removes whole macros nodes instead of exact trimming. Best paired with `XADD`.
XADD logstream MAXLEN ~ 50000 * level error msg "disk full"
XGROUP CREATE orders shipping 0 MKSTREAM
XGROUP CREATE orders billing 0 MKSTREAM
XGROUP CREATE orders notifications 0 MKSTREAM
Multiple groups on the same stream: each group receives all messages independently (broadcast).
XADD orders * type new user 42 total 99.99
Reliable queue with retry:
XGROUP CREATE tasks workers 0 MKSTREAM
XREADGROUP GROUP workers worker1 BLOCK 5000 COUNT 1 STREAMS tasks >
XACK tasks workers 1640995200000-0
Fan-out / broadcast:
XGROUP CREATE events service-a $ MKSTREAM
XGROUP CREATE events service-b $ MKSTREAM
XADD events * type user-created data '{"id":1}'
Pending recovery:
XPENDING tasks workers - + 100 worker1
XAUTOCLAIM tasks workers worker2 300000 0-0 COUNT 50
XADD mystream * name Alice age 30
XADD mystream * sensor-id 1 temperature 23.5
XADD mystream 1640995200000-0 event login user 42
使用 `*` 自动生成条目 ID(时间戳-序列号格式)。
XADD mystream MAXLEN 1000 * message hello
XADD mystream MAXLEN ~ 1000 * message hello
XREAD COUNT 10 STREAMS mystream $
XREAD COUNT 5 BLOCK 5000 STREAMS mystream 1640995200000-0
XREAD STREAMS stream1 stream2 0-0 0-0
`$` 表示仅读取新条目。`BLOCK` 使命令阻塞等待新数据(毫秒)。
XREAD COUNT 10 BLOCK 0 STREAMS mystream $
XRANGE mystream - +
XRANGE mystream 1640995200000-0 1640995260000-0
XRANGE mystream - + COUNT 5
XRANGE mystream 1640995200000-0 (1640995200000-0 COUNT 1
`-` 是最旧 ID,`+` 是最新 ID。`(` 表示不包含边界值。
XREVRANGE mystream + - COUNT 3
XREVRANGE mystream + 1640995200000-0 COUNT 10
XLEN mystream
返回流中的条目数量。
XLEN mystream
XLEN orders
XLEN events
XDEL mystream 1640995200000-0
XDEL mystream id1 id2 id3
XTRIM mystream MAXLEN 1000
XTRIM mystream MAXLEN ~ 5000
XTRIM mystream MINID 1640995200000-0
`~` 启用近似裁剪以提升性能。`MINID` 移除早于指定 ID 的条目。
XREADGROUP GROUP mygroup consumer1 COUNT 1 STREAMS mystream >
XREADGROUP GROUP mygroup consumer1 BLOCK 5000 COUNT 10 STREAMS mystream >
XREADGROUP GROUP mygroup consumer1 COUNT 1 STREAMS mystream 0
`>` 仅接收新消息。`0` 接收该消费者的待处理(未确认)消息。
XREADGROUP GROUP mygroup consumer1 COUNT 10 STREAMS s1 s2 > >
XACK mystream mygroup 1640995200000-0
XACK mystream mygroup id1 id2 id3
确认消息处理成功,将其从待处理列表中移除。
XACK mystream mygroup $(XREADGROUP GROUP mygroup consumer1 COUNT 1 STREAMS mystream > | grep -oP '\d+-\d')
XGROUP CREATE mystream mygroup $
XGROUP CREATE mystream mygroup 0 MKSTREAM
XGROUP CREATE mystream mygroup 1640995200000-0
`$` 仅从新消息开始消费。`0` 从头开始。`MKSTREAM` 在流不存在时自动创建。
XGROUP DESTROY mystream mygroup
XGROUP DELCONSUMER mystream mygroup consumer1
XGROUP SETID mystream mygroup $
XPENDING mystream mygroup
XPENDING mystream mygroup - + 10
XPENDING mystream mygroup - + 10 consumer1
查看已投递但未确认的待处理消息。摘要模式返回总数、最小/最大 ID 和消费者列表。
XPENDING mystream mygroup 1640995200000-0 + 20 consumer1
XCLAIM mystream mygroup consumer1 3600000 id1 id2
XCLAIM mystream mygroup consumer1 0 id1
XCLAIM mystream mygroup consumer1 60000 id1 JUSTID
将待处理消息从一个消费者转移到另一个消费者。时间为最小空闲时间(毫秒)。
XAUTOCLAIM mystream mygroup consumer1 3600000 0-0 COUNT 10
`XAUTOCLAIM` 自动认领空闲时间超过阈值的消息。
XINFO STREAM mystream
XINFO GROUPS mystream
XINFO CONSUMERS mystream mygroup
返回流、消费者组及组内消费者的详细元数据。
XINFO STREAM mystream FULL
XINFO STREAM mystream FULL COUNT 5
XADD mystream MAXLEN 10000 * field value
XADD mystream MAXLEN ~ 10000 * field value
XTRIM mystream MAXLEN = 10000
XTRIM mystream MAXLEN ~ 10000
使用 `~`(近似裁剪)可提升性能——Redis 按宏节点整块删除而非精确裁剪。建议配合 `XADD` 使用。
XADD logstream MAXLEN ~ 50000 * level error msg "disk full"
XGROUP CREATE orders shipping 0 MKSTREAM
XGROUP CREATE orders billing 0 MKSTREAM
XGROUP CREATE orders notifications 0 MKSTREAM
同一流上创建多个消费者组:每个组独立接收所有消息(广播模式)。
XADD orders * type new user 42 total 99.99
可靠队列与重试:
XGROUP CREATE tasks workers 0 MKSTREAM
XREADGROUP GROUP workers worker1 BLOCK 5000 COUNT 1 STREAMS tasks >
XACK tasks workers 1640995200000-0
扇出 / 广播:
XGROUP CREATE events service-a $ MKSTREAM
XGROUP CREATE events service-b $ MKSTREAM
XADD events * type user-created data '{"id":1}'
待处理消息恢复:
XPENDING tasks workers - + 100 worker1
XAUTOCLAIM tasks workers worker2 300000 0-0 COUNT 50