Redis Streams
Redis Streams can hold a series of events generated via multiple producers or sources. Usually, a Redis Stream has a key that determines each Stream uniquely within the data store. A Stream carries several entries where each entry is a key-value pair. Redis Streams have consumer groups attached to them. Each consumer group might have multiple consumers reading from the Stream. The following figure summarizes the high-level picture of a Redis Stream and related entities:
There is a lot of information about the Redis Stream, consumer groups, and consumers. The previous illustration summarizes that information up to some extent. Redis allows its users to query all these details using the XINFO command.
Information on Redis Streams: XINFO Command
The XINFO command is a high-level container command to query all the details related to the Stream, its consumer groups, and consumers. This command supports several subcommands to be called, as shown in the following:
Syntax:
XINFO Subcommands
There are four main subcommands available to use with the XINFO command. The most basic command is the HELP command which lists down all the other subcommands available. Furthermore, it describes the purpose of each subcommand with its syntax.
Syntax:
Output:
XINFO STREAM
The STREAM subcommand will display the details of the Stream stored at the specified key.
Syntax:
This command will display details related to the specified Stream, such as length, groups attached, last-generated-id, first-entry, last-entry, etc.
Example: Information on location rating information with Redis streams
Let’s add a new stream of ID locationRatingStream. Furthermore, some entries will be added, as shown in the following:
xadd locationRatingStream * stars 3 comment good
xadd locationRatingStream * stars 1 comment worst
Let’s add two consumer groups to the locationRatingStream, as shown in the following:
xgroup create locationRatingStream brazilgroup $
Finally, we will read from the consumer group via a consumer, Alex.
This would create a new consumer, Alex, as well.
Now, we can try out the XINFO STREAM subcommand, as shown in the following:
Output:
We can use the FULL parameter to see all the entries and groups stored in the Stream. Furthermore, this would show two new properties, entries and groups, as shown in the following.
The COUNT is another optional parameter we can specify with the FULL parameter. It will limit the number of rows returned per Stream and PEL entry arrays.
XINFO GROUPS
The XINFO GROUPS subcommand displays all the consumer groups attached to a stream stored at a specified key. This command displays several details such as the group’s name, number of consumers per group, length of the PEL, last-delivered-id, lag, etc.
Syntax:
Let’s use the previously created stream locationRatingStream to check the information of its attached consumer groups.
Output:
As expected, the XINFO GROUPS command lists two consumer groups related to the Stream stored at key locationRatingStream. Furthermore, it lists the number of consumers and the length of the PEL for both groups.
XINFO CONSUMERS
Each consumer group contains one or more consumers. Hence, the XINFO CONSUMERS subcommand shows information related to all the consumers in a consumer group where the group belongs to a stream identified by the specified key. This command lists the following information about consumers:
- Name: The name of the consumer.
- Pending: The number of messages that the consumer does not acknowledge.
- Idle: The number of milliseconds passed since the consumer’s last active interaction with the server.
Syntax:
Let’s use the previously created stream locationRatingStream and the consumer group canadagroup to get the details about its consumers.
Output:
As expected, the consumer group canadagroup has one consumer called Alex, with two messages yet to be acknowledged. In addition, the consumer had no interaction with the Redis server for 4550962 milliseconds.
Conclusion
Redis Streams can hold a sequence of events generated by multiple sources. In addition, each Stream can have several consumer groups and consumers reading from it. There is a lot of valuable information available about those entities. Hence, Redis provides a container command XINFO to use with different subcommands for various purposes. The XINFO HELP command lists all the subcommands available such as XINFO STREAM, XINFO GROUPS, and XINFO CONSUMERS. Each subcommand provides a large amount of information about a specific stream, its consumer groups, and related consumers.