首页 归档 关于 learn love 工具

Redis Stream Tutorial In 20 Minutes

1. Introduction

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.

2. Usage

2.1 Adding elements to stream

redis:6379> XADD mystream * sensor-id 1234 temperature 19.8
1531989605376-0

What we can know from the above are:

  • mystream is the key of stream;
  • the parameter at the location of * is the element ID, and * indicates that an element ID is generated by the system automatically;
  • the added element contains 2 key-value pairs, sensor-id 1234 and temperature 19.8;
  • the returned value is the ID of the newly added element, consisting of a timestamp and an incrementing number.

You can also get the number of elements in the Stream:

redis:6379> XLEN mystream
(integer) 1

2.2 Range query

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:

redis:6379> XRANGE mystream 1531989605376 1531989605377
1) 1) 1531989605376-0
  2) 1) "sensor-id"
     2) "1234"
     3) "temperature"
     4) "19.8"

You can use - for the smallest ID and + for the biggest ID:

redis:6379> XRANGE mystream - +
1) 1) 1531989605376-0
  2) 1) "sensor-id"
     2) "1234"
     3) "temperature"
     4) "19.8"

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:

redis:6379> XRANGE mystream - + COUNT 2
1) 1) 1531989605376-0
  2) 1) "sensor-id"
     2) "1234"
     3) "temperature"
     4) "19.8"

You can also use the XREVRANGE command to reverse the query, and the usage is the same as XRANGE.

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"

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.

2.3.1 Blocking listener

If you execute the following in client 1:

redis:6379> XREAD BLOCK 0 STREAMS mystream $

it will enter the waiting state.
And if you add elements to client 2:

redis:6379> XADD mystream * test 1

the elements just added will be displayed in client 1:

1) 1) "mystream"
  2) 1) 1) 1531994510562-0
        2) 1) "test"
           2) "1"

0 is the specified timeout, so 0 means never timeout here; $ means the maximum ID in the stream.

2.4 Consumer Group

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:

1 -> C1
2 -> C2
3 -> C3
4 -> C1
5 -> C2
6 -> C3
7 -> C1

原文连接

https://web.archive.org/web/20190217140100/https://www.tutorialdocs.com/article/redis-stream-tutorial.html
https://dev.to/bajena/removing-n-oldest-entries-from-a-redis-stream-5aob