Redis

How to Use Redis Streams

Streams are a simple but versatile and powerful data structure that allows you to implement streams in Redis. Streams are built-in types from Redis version 5.0 and can help remove the limitations of log data structure.

In this article, we will focus on using and working with Redis streams rather than the actual implementation. You can, however, check the docs for more.

Redis Create Stream

To create a stream in Redis, we use the XADD command followed by the stream name, ID, key, and data as the parameters.

The syntax is as shown:

XADD [stream name] [id] [key] [data]

Example is as shown:

127.0.0.1:6379> XADD datastream * ip 231.17.140.219
"1646904960928-0"

In the above example, we run the XADD command to add a new stream-entry. In our example, give the entry datastream with a unique id.

Although you can set an ID manually, we use the asterisk to tell Redis to auto-generate a unique identifier. This is the output of the command above.

NOTE: Every generated IP is monotonically incremented from the previous ones. In most cases, you will rarely need to set the ID for an entry manually. We can do this as shown in the command below:

127.0.0.1:6379> XADD datastream 74376383723373 ip 171.17.140.219
"74376383723373-0"

In the command above, we manually specify the entry ID.

The entry holds a field and value IP and corresponding IP.

Redis Add Stream with Limit

In some cases, you may not want the stream entries to go above a specified value. You can do this by specifying the MAXLEN parameter as:

XADD datastream MAXLEN 100 * ip 231.17.140.219

Redis Stream Entries

To get the number of entries in a Redis stream, we can use the XLEN command as:

127.0.0.1:6379> XLEN datastream
(integer) 3

This should return an integer denoting the number of entries in the stream.

Getting Dat From Stream

We can iterate over the entries in a stream by specifying the starting and end IDs as shown in the command:

127.0.0.1:6379> XRANGE datastream - +
1) 1) "1646904960928-0"
   2) 1) "ip"
      2) "231.17.140.219"
2) 1) "74376383723373-0"
   2) 1) "ip"
      2) "231.17.140.219"
3) 1) "74376383723373-1"
   2) 1) "ip"
      2) "231.17.140.219"

Using the – + (lower and upper bound) parameters returns all the entries in the stream. You can also specify a range as:

127.0.0.1:6379> XRANGE datastream 1646904960928-0 74376383723373-0
1) 1) "1646904960928-0"
   2) 1) "ip"
      2) "231.17.140.219"
2) 1) "74376383723373-0"
   2) 1) "ip"
      2) "231.17.140.219"

The command should return the entries within the ID range specified.

Redis Read Everything

To read every entry in the stream starting from the top, use the XREAD command as shown:

127.0.0.1:6379> XREAD COUNT 100 STREAMS datastream 0
1) 1) "datastream"
   2) 1) 1) "1646904960928-0"
         2) 1) "ip"
            2) "231.17.140.219"
      2) 1) "74376383723373-0"
         2) 1) "ip"
            2) "231.17.140.219"
      3) 1) "74376383723373-1"
         2) 1) "ip"
            2) "231.17.140.219"

To read only new data as it arrives, use the command as:

127.0.0.1:6379> XREAD BLOCK 10000 STREAMS datastream $

The command will wait from any stream entries for the specified milliseconds and then close. In our case, we set the wait for 10000 milliseconds.

Conclusion

This tutorial provides the basics of working with data streams in Redis. Check the docs for more.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list