XREVRANGE key end start [COUNT count]

Available since 5.0.0.

Time 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).

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:

  1. XREVRANGE somestream + -

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

  1. XREVRANGE somestream + - COUNT 1

*Iterating with XREVRANGE

Like XRANGE this command can be used in order to iterate the whole stream content, however note that in this case, the next command calls should use the ID of the last entry, with the sequence number decremented by one. However if the sequence number is already 0, the time part of the ID should be decremented by 1, and the sequence part should be set to the maximum possible sequence number, that is, 18446744073709551615, or could be omitted at all, and the command will automatically assume it to be such a number (see XRANGE for more info about incomplete IDs).

Example:

  1. > XREVRANGE writers + - COUNT 2
  2. 1) 1) 1526985723355-0
  3. 2) 1) "name"
  4. 2) "Ngozi"
  5. 3) "surname"
  6. 4) "Adichie"
  7. 2) 1) 1526985712947-0
  8. 2) 1) "name"
  9. 2) "Agatha"
  10. 3) "surname"
  11. 4) "Christie"

The last ID returned is 1526985712947-0, since the sequence number is already zero, the next ID I'll use instead of the + special ID will be 1526985712946-18446744073709551615, or just 18446744073709551615:

  1. > XREVRANGE writers 1526985712946-18446744073709551615 - COUNT 2
  2. 1) 1) 1526985691746-0
  3. 2) 1) "name"
  4. 2) "Toni"
  5. 3) "surname"
  6. 4) "Morris"
  7. 2) 1) 1526985685298-0
  8. 2) 1) "name"
  9. 2) "Jane"
  10. 3) "surname"
  11. 4) "Austen"

And so for until the iteration is complete and no result is returned. See the XRANGE page about iterating for more information.

*Return value

Array reply, specifically:

The command returns the entries with IDs matching the specified range, from the higher ID to the lower ID matching. The returned entries are complete, that means that the ID and all the fields they are composed are returned. Moreover the entries are returned with their fields and values in the exact same order as XADD added them.

*Examples

redis> XADD writers * name Virginia surname Woolf

  1. "1576238526971-0"

redis> XADD writers * name Jane surname Austen

  1. "1576238526972-0"

redis> XADD writers * name Toni surname Morris

  1. "1576238526973-0"

redis> XADD writers * name Agatha surname Christie

  1. "1576238526974-0"

redis> XADD writers * name Ngozi surname Adichie

  1. "1576238526975-0"

redis> XLEN writers

  1. (integer) 5

redis> XREVRANGE writers + - COUNT 1

  1. 1) 1) "1576238526975-0"
  2. 2) 1) "name"
  3. 2) "Ngozi"
  4. 3) "surname"
  5. 4) "Adichie"
redis>