Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add description for XREAD new '+' special ID for last stream entry (#… #2687

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion commands/xread.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,26 @@ Once we get some replies, the next call will be something like:

And so forth.

## The special `+` ID

It is sometimes desirable to read the last entry in a stream. For a single
stream this can be easily achieved using the `XREVRANGE` command, like so:

```
> XREVRANGE stream + - COUNT 1
```
But if there are many streams involved, this approach is quickly becoming
too slow, having to issue a command per stream.
Instead, starting from redis 8.0, the `+` sign can be used as a special ID,
requesting the last available entry in a stream. For example:

```
> XREAD STREAM streamA streamB streamC streamD + + + +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to mention that the pattern is usually that you start iterating on a stream with + and then switch to $ on the next call?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this is the common use-case, maybe sometimes users just need to peep at the last element and don't continue from there. But I can add a mention if you think it's required.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@majklik, @guybe7 please advise.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One nice use case is the mentioned peek fro last value in multiple streams. In this case is perfect one time shoot with: XREAD STREAM streamA streamB streamC streamD + + + +
But for reliable reading of the content of streams, when I need to start from the last available value (which is my case), after the initial "XREAD STREAM streamA streamB streamC streamD + + + +" it is convenient to continue with the obtained IDs from the streams in the style: "XREAD STREAM streamA streamB streamC streamD idA idB idC idD". If the initial reading shows that one of the streams is empty, I will use 0 as the id for subsequent readings.

```

Note that when using this special ID for a stream, the **COUNT** option will
be ignored (for the specific stream) since only the last entry can be returned.

## How multiple clients blocked on a single stream are served

Blocking list operations on lists or sorted sets have a *pop* behavior.
Expand All @@ -199,4 +219,4 @@ data to the stream.

Reading the [Redis Streams introduction](/topics/streams-intro) is highly
suggested in order to understand more about the streams overall behavior
and semantics.
and semantics.
Loading