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:
Example is as shown:
"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:
"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:
Redis Stream Entries
To get the number of entries in a Redis stream, we can use the XLEN command as:
(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:
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:
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:
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:
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.