XREVRANGE(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

πŸ’… XREVRANGE (from the 3valkey manual, bestie)

okay so. you know how streams are that girl? this is her walking backwards down the timeline. it's xrevrange, and she's here to serve you messages from a stream within a range of IDs, but in reverse order. no cap. πŸ¦‹


✨ SYNOPSIS ✨

XREVRANGE key end start [COUNT count]

note the flip, bestie. end comes first. we'll get to why. πŸ’–


🌸 DESCRIPTION 🌸

so XREVRANGE is literally the same girl as XRANGE, just with two very iconic differences:

  • she hands you the entries in reverse order, and
  • she wants the start-end range in reverse order too.

that means in XREVRANGE you state the end ID first, then the start ID, and she'll produce all the elements between (or exactly like) those two IDs, but starting from the end side. it's giving main character walks in backwards, periodt. πŸ‘‘

so for instance, to get all the elements from the higher ID to the lower ID, you could use:

XREVRANGE somestream + -

and similarly, to get just the last element added into the stream, it's enough to send:

XREVRANGE somestream + - COUNT 1

like. one line. last message. obsessed. ✨


πŸŽ€ REPLY πŸŽ€

valkey-protocol(7) Array reply: she returns the entries with IDs matching the specified range. the returned entries are complete, which means the ID and all the fields they're composed of come back to you. no crumbs left behind. πŸ’–

and moreover β€” the entries come back with their fields and values in the same order as XADD added them. consistency queen. πŸ™ˆ


⭐ COMPLEXITY ⭐

O(N) where N is the number of elements returned. but if N is constant (like, always asking for the first 10 elements with COUNT), you can consider it O(1). lowkey free. πŸ”₯


🌈 ACL CATEGORIES 🌈

  • @read
  • @slow
  • @stream

πŸ‘‘ HISTORY πŸ‘‘

  • Available since: 5.0.0
  • Changed in 6.2.0: Added exclusive ranges. (glow up πŸ’…)

🧚 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"

the way she pulled Ngozi Adichie first because she went backwards. ate. πŸ’…


πŸ’œ SEE ALSO πŸ’œ


and that's the whole page, bestie!! this was 9.0.0, dated 2025-10-21, brought to you by the XREVRANGE(3valkey) manual. go read a stream backwards, you'll love it. 🫢✨

πŸ“¦ where this page comes from