There is a key new feature in redis 5: stream. Stream is a storage structure in the log form, and you can append data into it. It will generate a timestamp ID for each data. And stream also has a convenient model or reading data. So stream is suitable for message queues and time series storage. What we can know from the above are: You can also get the number of elements in the Stream: When we want to use a range query, we need to specify the start and end IDs, which is equivalent to giving a time range: You can use - for the smallest ID and + for the biggest ID: When there are too many elements returned, you can limit the number of returned results, which is just like the paging when querying the database and specify it by the COUNT parameter: You can also use the XREVRANGE command to reverse the query, and the usage is the same as XRANGE. The mystream after STREAMS specifies the key of the target stream; 0 is the smallest ID, and we need to obtain the elements that is greater than the specified ID in the specified stream; COUNT refers to the number of the elements we want to obtain. Multiple streams can be specified together, such as STREAMS mystream otherstream 0 0. If you execute the following in client 1: it will enter the waiting state. the elements just added will be displayed in client 1: 0 is the specified timeout, so 0 means never timeout here; $ means the maximum ID in the stream. When the amount of stream is very large, or when the consumer processing is very time consuming, it'll under greater pressure if there is only one consumer. Thus redis stream provides the concept of the consumer group, allowing multiple consumers to process the same stream to implement load balancing. For example, if there are 3 consumers C1, C2, and C3, and there are 7 message elements in the stream, then the allocation for the consumers is: https://web.archive.org/web/20190217140100/https://www.tutorialdocs.com/article/redis-stream-tutorial.html1. Introduction
2. Usage
2.1 Adding elements to stream
redis:6379> XADD mystream * sensor-id 1234 temperature 19.8
1531989605376-0
redis:6379> XLEN mystream
(integer) 1
2.2 Range query
redis:6379> XRANGE mystream 1531989605376 1531989605377
1) 1) 1531989605376-0
2) 1) "sensor-id"
2) "1234"
3) "temperature"
4) "19.8"
redis:6379> XRANGE mystream - +
1) 1) 1531989605376-0
2) 1) "sensor-id"
2) "1234"
3) "temperature"
4) "19.8"
redis:6379> XRANGE mystream - + COUNT 2
1) 1) 1531989605376-0
2) 1) "sensor-id"
2) "1234"
3) "temperature"
4) "19.8"
2.3 Listening for new elements of stream
redis:6379> XREAD COUNT 2 STREAMS mystream 0
1) 1) "mystream"
2) 1) 1) 1531989605376-0
2) 1) "sensor-id"
2) "1234"
3) "temperature"
4) "19.8"
2.3.1 Blocking listener
redis:6379> XREAD BLOCK 0 STREAMS mystream $
And if you add elements to client 2:redis:6379> XADD mystream * test 1
1) 1) "mystream"
2) 1) 1) 1531994510562-0
2) 1) "test"
2) "1"
2.4 Consumer Group
1 -> C1
2 -> C2
3 -> C3
4 -> C1
5 -> C2
6 -> C3
7 -> C1
原文连接
https://dev.to/bajena/removing-n-oldest-entries-from-a-redis-stream-5aob