Redis stream can store field-value pairs within its macro nodes. It is memory efficient and fast in accessing stream entries since it’s based on a Radix tree structure. The entries look like the Redis hash key-value pairs.
The previous illustration shows a stream stored at key “surveryresponse:user01”. It contains N number of entries.
Count Stream Entries With XLEN
In real-world applications, we might need to know how many entries are available in a given stream. It is not practical to make an XRANGE call with minimum and maximum possible IDs to query all the entries per stream and count them programmatically. It consumes time for two operations to read and count programmatically.
Hence, Redis provides an XLEN command to count entries for a stream stored at a specified key. This command has constant time complexity, which is fast for your applications.
Syntax
The XLEN command returns an integer, the number of entries stored in a stream.
The streams are a bit different from other Redis data types because a stream can exist with zero entries. Redis streams can have multiple consumer groups attached. The XDEL command will not delete the stream itself even if all the entries have been deleted. Hence, the XLEN command might return 0 in two cases:
- The stream is empty, or all the entries have been deleted already.
- The stream key doesn’t exist.
The stream stored at key surveyresponse:user02 is an empty one. But it has two consumer groups attached. Hence, the stream exists. In real-world applications, you need to do an explicit check with the EXISTS command to verify whether the Redis key exists or not.
Example: Count the Number of Users Participating in an Online Survey
Let’s assume that a restaurant has conducted an online survey to get a star rating from its customers on their service quality. They have used an in-memory Redis data store to keep track of each customer rating. Each stream entry consists of a couple field-value pairs to store the rating as an integer and user ID as a string.
Let’s create a stream using the XADD command.
xadd restaurentsurvery * userid 2 rating 5
xadd restaurentsurvery * userid 3 rating 1
xadd restaurentsurvery * userid 4 rating 5
Four entries have been added to the stream stored at the key restaurentsurvey.
Next, we will use the XLEN command to count the number of users who took part in the survey.
Output:
As expected, the return value is 4. Assume that more than 10,000 customers have participated in the survey. Then, the XLEN command would be a lifesaver. Even the Redis XLEN command would take constant time to count the 4 customers or 10,000 customers.
Conclusion
Redis streams can contain thousands of entries until the maximum memory is reached, where each entry looks like a field-value pair. Usually, it is cumbersome to count the number of entries stored in a stream program-wise. It consumes time for two operations like reading and counting. Hence, Redis provides count operation out of the box with the XLEN command. It has constant time complexity which is fast to use in high-performance applications. The XLEN command only takes the key of the stream as a parameter.