0
0
Redisquery~10 mins

XRANGE and XREVRANGE in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - XRANGE and XREVRANGE
Start with Stream Key
Specify Start and End IDs
Choose XRANGE or XREVRANGE
Fetch Entries in Range
Return Entries in Order
End
XRANGE and XREVRANGE fetch entries from a Redis stream between two IDs, returning them in forward or reverse order.
Execution Sample
Redis
XRANGE mystream 1609459200000-0 1609459205000-0
XREVRANGE mystream 1609459205000-0 1609459200000-0
Fetch entries from 'mystream' between two timestamps, forward with XRANGE and reverse with XREVRANGE.
Execution Table
StepCommandStart IDEnd IDOrderEntries Returned
1XRANGE mystream 1609459200000-0 1609459205000-01609459200000-01609459205000-0Forward[{id:"1609459200000-0", fields:{a:1}}, {id:"1609459201000-0", fields:{b:2}}, {id:"1609459203000-0", fields:{c:3}}]
2XREVRANGE mystream 1609459205000-0 1609459200000-01609459205000-01609459200000-0Reverse[{id:"1609459203000-0", fields:{c:3}}, {id:"1609459201000-0", fields:{b:2}}, {id:"1609459200000-0", fields:{a:1}}]
3XRANGE mystream 1609459204000-0 1609459205000-01609459204000-01609459205000-0Forward[]
4XREVRANGE mystream 1609459200000-0 1609459200000-01609459200000-01609459200000-0Reverse[{id:"1609459200000-0", fields:{a:1}}]
5XRANGE mystream - +-+Forward[All entries from oldest to newest]
6XREVRANGE mystream + -+-Reverse[All entries from newest to oldest]
7Exit---No more entries or invalid range
💡 Execution stops when no entries match the range or the range is invalid.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
Start ID1609459200000-01609459200000-01609459205000-01609459204000-01609459200000-0-
End ID1609459205000-01609459205000-01609459200000-01609459205000-01609459200000-0-
OrderForwardForwardReverseForwardReverse-
Entries Returned[][3 entries][3 entries][][1 entry]-
Key Moments - 3 Insights
Why does XRANGE return entries in ascending order but XREVRANGE returns them in descending order?
Because XRANGE reads from the start ID to the end ID in forward time order (see execution_table rows 1 and 2), while XREVRANGE reads backwards from the end ID to the start ID.
What happens if the start ID is greater than the end ID in XRANGE?
XRANGE will return an empty list because the range is invalid or no entries fall in that range (see execution_table row 3).
How do the special IDs '+' and '-' affect the range?
'+' means the maximum possible ID (newest), '-' means the minimum possible ID (oldest). Using them fetches all entries from oldest to newest or newest to oldest (see execution_table rows 5 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table row 1, what is the order of entries returned by XRANGE?
ADescending (newest to oldest)
BAscending (oldest to newest)
CRandom order
DNo entries returned
💡 Hint
Check the 'Order' column in row 1 of execution_table.
At which step does XRANGE return an empty list due to no entries in range?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Entries Returned' column for empty lists in execution_table.
If you swap the start and end IDs in XREVRANGE, how does the entries order change?
AIt stays the same
BIt reverses to ascending order
CIt returns no entries
DIt causes an error
💡 Hint
Refer to execution_table row 3 and the explanation about invalid ranges.
Concept Snapshot
XRANGE key start end [COUNT n]
XREVRANGE key end start [COUNT n]

- XRANGE returns stream entries from start to end ID in ascending order.
- XREVRANGE returns entries from end to start ID in descending order.
- Use '-' for earliest ID and '+' for latest ID.
- If no entries match, returns empty list.
Full Transcript
XRANGE and XREVRANGE are Redis commands to read entries from a stream between two IDs. XRANGE reads forward from the start ID to the end ID, returning entries in ascending order. XREVRANGE reads backward from the end ID to the start ID, returning entries in descending order. Special IDs '-' and '+' represent the earliest and latest entries respectively. If the range is invalid or no entries exist in the range, the commands return an empty list. This visual execution shows step-by-step how these commands fetch entries and how the order and range affect the results.