xrevrange.3valkey - Man Page

Returns the messages from a stream within a range of IDs in reverse order.

Synopsis

XREVRANGE key end start [COUNT count]

Description

This command is exactly like XRANGE, but with the notable difference of returning the entries in reverse order, and also taking the start-end range in reverse order: in XREVRANGE you need to state the end ID and later the start ID, and the command will produce all the element between (or exactly like) the two IDs, starting from the end side.

So for instance, to get all the elements from the higher ID to the lower ID one could use:

XREVRANGE somestream + -

Similarly to get just the last element added into the stream it is enough to send:

XREVRANGE somestream + - COUNT 1

Reply

valkey-protocol(7) Array reply: The command returns the entries with IDs matching the specified range. The returned entries are complete, which means that the ID and all the fields they are composed of are returned. Moreover, the entries are returned with their fields and values in the same order as XADD added them.

Complexity

O(N) with N being the number of elements returned. If N is constant (e.g. always asking for the first 10 elements with COUNT), you can consider it O(1).

Acl Categories

@read @slow @stream

History

Examples

127.0.0.1:6379> XADD writers * name Virginia surname Woolf
"1714701492147-0"
127.0.0.1:6379> XADD writers * name Jane surname Austen
"1714701492157-0"
127.0.0.1:6379> XADD writers * name Toni surname Morrison
"1714701492167-0"
127.0.0.1:6379> XADD writers * name Agatha surname Christie
"1714701492177-0"
127.0.0.1:6379> XADD writers * name Ngozi surname Adichie
"1714701492187-0"
127.0.0.1:6379> XLEN writers
(integer) 5
127.0.0.1:6379> XREVRANGE writers + - COUNT 1
1) 1) "1714701492187-0"
   2) 1) "name"
      2) "Ngozi"
      3) "surname"
      4) "Adichie"

See Also

xack(3valkey), xadd(3valkey), xautoclaim(3valkey), xclaim(3valkey), xdel(3valkey), xgroup(3valkey), xgroup-create(3valkey), xgroup-createconsumer(3valkey), xgroup-delconsumer(3valkey), xgroup-destroy(3valkey), xgroup-help(3valkey), xgroup-setid(3valkey), xinfo(3valkey), xinfo-consumers(3valkey), xinfo-groups(3valkey), xinfo-help(3valkey), xinfo-stream(3valkey), xlen(3valkey), xpending(3valkey), xrange(3valkey), xread(3valkey), xreadgroup(3valkey), xsetid(3valkey), xtrim(3valkey)

Info

2024-09-23 8.0.0 Valkey Command Manual