Redis Stream Commands 命令学习
- 作者: 五速梦信息网
- 时间: 2026年04月04日 13:29
概况
A Redis stream is a data structure that acts like an append-only log. You can use streams to record and simultaneously syndicate events in real time. Examples of Redis stream use cases include:
- Event sourcing (e.g., tracking user actions, clicks, etc.)
- Sensor monitoring (e.g., readings from devices in the field)
- Notifications (e.g., storing a record of each user‘s notifications in a separate stream)
Redis generates a unique ID for each stream entry. You can use these IDs to retrieve their associated entries later or to read and process all subsequent entries in the stream.
XREADXREADGROUPXRANGE
redis stream 是一个追加式的数据结构,可以用在以下三个场景:
1、事件溯源,比如跟踪用户的操作,点击,等
2、传感监视,如某些专业领域的设置监控
3、消息 可以把不同用户的消息存在一个单据的stream中
127.0.0.1:6386> XADD temperatures:us-ny:10007 * temp_f 87.2 pressure 29.69 humidity 46xadd:唯一可以把数据存入stream中的命令 XADD
“1678622044821-0”
127.0.0.1:6386> XADD temperatures:us-ny:10007 * temp_f 83.1 pressure 29.21 humidity 46.5
“1678622061283-0”
127.0.0.1:6386> XADD temperatures:us-ny:10007 * temp_f 81.9 pressure 28.37 humidity 43.7
“1678622071669-0”
127.0.0.1:6386> XRANGE temperatures:us-ny:10007 1658354934941-0 + COUNT 2 [COUNT count]
[root@machine138 redis-stack]# bin/redis-cli -p 6386
127.0.0.1:6386> XRANGE temperatures:us-ny:10007 1678622061283-0 + COUNT 2
- 1) “1678622061283-0”
- 1) “temp_f”
- “83.1”
- “pressure”
- “29.21”
- “humidity”
- “46.5”
- 1) “1678622071669-0”
- 1) “temp_f”
- “81.9”
- “pressure”
- “28.37”
- “humidity”
- “43.7”
XADD key [NOMKSTREAM] [<MAXLEN | MINID> [= | ~] threshold
[LIMIT count]] <* | id> field value [field value …]
NOMKSTREAM
XRANGEXREADXADD
XADDXDELXTRIM
Streams are an append-only data structure. The fundamental write command, called XADD, appends a new entry to the specified stream.
each stream entry consists of one or more field-value pairs, somewhat like a record or a Redis hash
XADD
xadd命令可以创建一个stream 并把 键值对存入其中,顺序与用户输入的顺序相同,可以通过xread,xrange等命令读取,
每个stream entry 都由一个或多个key-value 组成,有点像hashes (HSET key value,HGET key value)
例如:XADD mystream * sensor-id 1234 temperature 19.8
mystream:stream key
*:自动生成 stream ID :<millisecondsTime>-<sequenceNumber> millisecondsTime:就是本地时间sequenceNumber:就是同一个millisecondsTime之内产生的64位整数sensor-id:key1
1234:vlaue1
temperature:key2
19.8 value2
另外,ID可以自定义,但是要遵循以下几个原则 : 1、格式必须是 :number-seq ,其中 number可以是任何数字,但是要确保后面的ID 的number 不能比之前的ID 小,等于之前的ID 中number时,必须使seq大于前一个seq,否则会提示 ERR The ID specified in XADD is equal or smaller than the target stream top item,在redis 7.0之后 ID 中seq 可以用代替 如: XADD somestream 0- baz qux
0-3
2、-seq 可以省略,redis 自动补全 -0,如
127.0.0.1:6386> xadd ms1 20 key1 value1
“20-0” 正式上面的对ID的规则 ,才有后面的命令 XRANGE
XRANGE key start end [COUNT count]
Getting data from Streams
XADDtail -fBLPOPtail -f
However, this is just one potential access mode. We could also see a stream in quite a different way: not as a messaging system, but as a time series store. In this case, maybe it’s also useful to get the new messages appended, but another natural query mode is to get messages by ranges of time, or alternatively to iterate the messages using a cursor to incrementally check all the history. This is definitely another useful access mode.
Finally, if we see a stream from the point of view of consumers, we may want to access the stream in yet another way, that is, as a stream of messages that can be partitioned to multiple consumers that are processing such messages, so that groups of consumers can only see a subset of the messages arriving in a single stream. In this way, it is possible to scale the message processing across different consumers, without single consumers having to process all the messages: each consumer will just get different messages to process. This is basically what Kafka ™ does with consumer groups. Reading messages via consumer groups is yet another interesting mode of reading from a Redis Stream.
Redis Streams support all three of the query modes described above via different commands. The next sections will show them all, starting from the simplest and most direct to use: range queries.
这一段主要是stream 读取方式的由来,最后采取了类型Kafka类似的方式,做为stream 的读取方式。
-+
127.0.0.1:6386> xrange ms1 - +
- 1) “key1”
- “value1”
- “value1”
- 1) “key1”
- “value1”
- “value1”
- 1) “key1”
- “value1”
- “value1”
- 1) “key1”
- “value1”
- “value1”
- 1) “key1”
- “value1”
- :表示当前stream (ms1)下最小ID 的entry
+: 表当前stream (ms1)下最大ID 的entry
后面的 count n可以限定获取多少条entry (范围内前n个entry)
127.0.0.1:6386> xrange ms1 - + count 2
- “value1”
- 1) “key1”
- “value1”
- “value1”
- 1) “key1”
- “value1”
127.0.0.1:6386>127.0.0.1:6386> xrange ms1 1 13-1
- “value1”
- 1) “key1”
- “value1”
- “value1”
- 1) “key1”
- “value1”
- “value1”
- 1) “key1”
- “value1”
- “value1”
- 1) “key1”
- “value1”
上面的 1:表示从1-0 开始的ID
13-1:表示要获取的最大的ID
127.0.0.1:6386> xrange ms1 1 13-1 count 2
- “value1”
- 1) “key1”
- “value1”
- “value1”
- 1) “key1”
- “value1”
在上面的命令中,如果 要完成后面的ENTRY的读取,需要借助 小括号 ( 后面跟已经读取到的最大的iD)
127.0.0.1:6386> xrange ms1 (12-0 + count 2
- “value1”
- 1) “key1”
- “value1”
- “value1”
- 1) “key1”
- “value1”
也可以选择确定的结束 ID
127.0.0.1:6386> xrange ms1 (12-0 13-1 count 2
- “value1”
- 1) “key1”
- “value1”
- “value1”
- 1) “key1”
- “value1”
上面是按照存入的顺序 获取的,如果 想按照存入的顺序相反的次序获取entry可以使用 xrevrange
XREVRANGE end start [count n] 注意 开始 和结束 id 与range 命令相比是 相反的。 reverse order!!!
127.0.0.1:6386> xrevrange ms1 15 1 count 1
- “value1”
- 1) “key1”
- “value1”
+ - 同样适用
127.0.0.1:6386> xrevrange ms1 + - count 1
- “value1”
- 1) “key1”
- “value1”
后面会学习 xread 命令。。。。
- “value1”
- 上一篇: Redis 宝典 丨 基础、高级特性与性能调优
- 下一篇: Redis Java客户端的选择
相关文章
-
Redis 宝典 丨 基础、高级特性与性能调优
Redis 宝典 丨 基础、高级特性与性能调优
- 互联网
- 2026年04月04日
-
redis 操作类型
redis 操作类型
- 互联网
- 2026年04月04日
-
redis 根据key 导出
redis 根据key 导出
- 互联网
- 2026年04月04日
-
Redis Java客户端的选择
Redis Java客户端的选择
- 互联网
- 2026年04月04日
-
RedHat6Centos6.5安装mongodb php driver
RedHat6Centos6.5安装mongodb php driver
- 互联网
- 2026年04月04日
-
react中怎么复制一个对象
react中怎么复制一个对象
- 互联网
- 2026年04月04日






