Bird
Raised Fist0
Redisquery~15 mins

XRANGE and XREVRANGE in Redis - Deep Dive

Choose your learning style10 modes available
Overview - XRANGE and XREVRANGE
What is it?
XRANGE and XREVRANGE are Redis commands used to read entries from a Redis stream. XRANGE returns stream entries in chronological order between two IDs, while XREVRANGE returns them in reverse chronological order. These commands help you fetch data from streams efficiently by specifying start and end points.
Why it matters
Streams are like logs or event records that grow over time. Without XRANGE and XREVRANGE, you would struggle to read parts of these logs efficiently or in the order you want. These commands let you access exactly the data you need, making real-time data processing and event handling possible in many applications.
Where it fits
Before learning XRANGE and XREVRANGE, you should understand basic Redis data types and how Redis streams work. After mastering these commands, you can explore advanced stream processing commands like XREAD, XREADGROUP, and consumer groups for building scalable real-time systems.
Mental Model
Core Idea
XRANGE and XREVRANGE let you read a slice of a Redis stream in forward or backward order by specifying start and end IDs.
Think of it like...
Imagine a photo album with pages numbered by date. XRANGE lets you flip through pages from an earlier date to a later date, while XREVRANGE lets you flip backward from a later date to an earlier date.
┌─────────────┐
│ Redis Stream│
│ Entries:    │
│ [ID1]       │
│ [ID2]       │
│ [ID3]       │
│ ...         │
│ [IDn]       │
└─────────────┘

XRANGE: ID1 → IDn (forward)
XREVRANGE: IDn → ID1 (backward)
Build-Up - 7 Steps
1
FoundationUnderstanding Redis Streams Basics
🤔
Concept: Introduce what Redis streams are and how they store data as ordered entries with unique IDs.
Redis streams are like logs that store messages or events. Each entry has a unique ID made of a timestamp and a sequence number. Entries are stored in the order they arrive, making streams useful for time-series data or event tracking.
Result
You understand that streams hold ordered data entries identified by IDs.
Knowing that streams are ordered collections with unique IDs is key to understanding how XRANGE and XREVRANGE work.
2
FoundationBasic Syntax of XRANGE Command
🤔
Concept: Learn how to use XRANGE to read entries between two IDs in forward order.
XRANGE stream_key start_id end_id [COUNT count] - stream_key: the name of the stream - start_id: the ID to start reading from - end_id: the ID to stop reading at - COUNT (optional): limits number of entries returned Example: XRANGE mystream 0-0 + This reads all entries from the beginning (0-0) to the latest (+).
Result
You can fetch stream entries in chronological order between two IDs.
Understanding XRANGE syntax lets you retrieve specific parts of a stream efficiently.
3
IntermediateUsing XREVRANGE for Reverse Reading
🤔Before reading on: do you think XREVRANGE returns entries from oldest to newest or newest to oldest? Commit to your answer.
Concept: XREVRANGE reads stream entries in reverse order, from a higher ID to a lower ID.
XREVRANGE stream_key end_id start_id [COUNT count] - Note the order of IDs is reversed compared to XRANGE Example: XREVRANGE mystream + 0-0 This reads all entries from the latest (+) back to the earliest (0-0).
Result
You can fetch stream entries in reverse chronological order.
Knowing XREVRANGE reads backward helps when you want recent events first without scanning the entire stream.
4
IntermediateFiltering Stream Entries with COUNT
🤔Before reading on: do you think COUNT limits the number of entries returned or the number of bytes? Commit to your answer.
Concept: COUNT limits how many entries XRANGE or XREVRANGE return, helping control data volume.
Adding COUNT to XRANGE or XREVRANGE restricts the number of entries returned. Example: XRANGE mystream 0-0 + COUNT 5 Returns only the first 5 entries. This is useful for pagination or limiting memory use.
Result
You can control how many entries you get back from a stream query.
Understanding COUNT prevents fetching too much data at once, improving performance and resource use.
5
IntermediateUsing Special IDs 0-0 and +
🤔
Concept: Learn the special IDs that represent the start and end of the stream.
In XRANGE and XREVRANGE, 0-0 means the very first entry possible, and + means the latest entry. Example: XRANGE mystream 0-0 + reads all entries. Example: XREVRANGE mystream + 0-0 reads all entries backward. These special IDs simplify reading entire streams.
Result
You can easily specify full stream ranges without knowing exact IDs.
Knowing these special IDs makes querying streams flexible and straightforward.
6
AdvancedHandling Stream Gaps and Missing IDs
🤔Before reading on: do you think XRANGE returns entries for IDs that don't exist or skips them? Commit to your answer.
Concept: XRANGE and XREVRANGE skip IDs that don't exist and return only actual entries within the range.
If you specify a range with IDs that don't exactly match entries, Redis returns entries closest to and within the range. Example: XRANGE mystream 1609459200000-0 1609459300000-0 If no entry exists at 1609459250000-0, it skips it and returns existing entries. This behavior ensures smooth reading even if some IDs are missing.
Result
You get only real entries, no errors for missing IDs.
Understanding this prevents confusion when expected IDs are not found and helps design robust queries.
7
ExpertPerformance Implications of XRANGE and XREVRANGE
🤔Before reading on: do you think XRANGE scans the entire stream or uses an index to find entries? Commit to your answer.
Concept: XRANGE and XREVRANGE use internal indexing to efficiently locate entries by ID, but large ranges can still impact performance.
Redis streams store entries in a radix tree indexed by IDs, allowing fast range queries. However, requesting very large ranges or omitting COUNT can cause high memory and CPU use. Using COUNT and precise ranges helps keep queries fast and resource-friendly. Also, reverse reading (XREVRANGE) is as efficient as forward reading.
Result
You understand how to write queries that perform well on large streams.
Knowing the internal indexing and performance tradeoffs helps prevent slow queries and system overload.
Under the Hood
Redis streams store entries in a radix tree keyed by entry IDs, which are composed of a millisecond timestamp and a sequence number. XRANGE and XREVRANGE traverse this tree to find entries between the specified start and end IDs. The radix tree allows efficient range scans in both forward and reverse directions. COUNT limits how many entries are returned to avoid large memory usage.
Why designed this way?
The radix tree structure was chosen to support fast insertion and range queries by ID, which is essential for time-ordered data. Using composite IDs with timestamps ensures uniqueness and ordering. The design balances fast writes with efficient reads, enabling real-time stream processing. Alternatives like simple lists would be slower for range queries and reverse scans.
┌─────────────────────────────┐
│ Redis Stream Radix Tree     │
│                             │
│  [1609459200000-0] ──┐      │
│                      │      │
│  [1609459200001-0] ──┼─> Entries
│                      │      │
│  [1609459200002-0] ──┘      │
│                             │
└─────────────────────────────┘

XRANGE: Traverse from start_id to end_id forward
XREVRANGE: Traverse from end_id to start_id backward
Myth Busters - 4 Common Misconceptions
Quick: Does XRANGE return entries including the start and end IDs or only between them? Commit to yes or no.
Common Belief:XRANGE returns entries only strictly between the start and end IDs, excluding them.
Tap to reveal reality
Reality:XRANGE includes entries with IDs equal to the start and end IDs if they exist.
Why it matters:Misunderstanding this can cause missing data or off-by-one errors when reading streams.
Quick: Does XREVRANGE return entries in ascending order? Commit to yes or no.
Common Belief:XREVRANGE returns entries in ascending (oldest to newest) order, just like XRANGE.
Tap to reveal reality
Reality:XREVRANGE returns entries in descending (newest to oldest) order.
Why it matters:Expecting ascending order can break logic that depends on processing recent events first.
Quick: Does COUNT limit the number of bytes returned or the number of entries? Commit to your answer.
Common Belief:COUNT limits the total size in bytes of the returned data.
Tap to reveal reality
Reality:COUNT limits the number of entries returned, not the byte size.
Why it matters:Confusing this can lead to unexpected large data transfers or inefficient queries.
Quick: Can XRANGE return entries that do not exist in the stream? Commit to yes or no.
Common Belief:XRANGE can return entries for IDs that don't exist by interpolating data.
Tap to reveal reality
Reality:XRANGE only returns actual entries stored in the stream; it skips missing IDs.
Why it matters:Expecting entries for non-existent IDs can cause logic errors or confusion.
Expert Zone
1
XRANGE and XREVRANGE can be combined with COUNT to implement efficient pagination over streams without loading all data at once.
2
The composite ID format (timestamp-sequence) allows multiple entries per millisecond, but careful ID management is needed to avoid collisions in custom ID generation.
3
Reverse range queries (XREVRANGE) are as efficient as forward queries due to the radix tree structure, which is not common in many database range scans.
When NOT to use
Avoid using XRANGE or XREVRANGE for real-time continuous reading of new entries; instead, use XREAD or XREADGROUP which support blocking and consumer groups. For very large streams where you need to process entries once, consumer groups provide better scalability and fault tolerance.
Production Patterns
In production, XRANGE and XREVRANGE are often used for backfilling data, debugging, or fetching historical events. They are combined with COUNT for pagination and with consumer groups for distributed processing. Monitoring stream length and trimming old entries is common to maintain performance.
Connections
Event Sourcing
XRANGE and XREVRANGE provide the mechanism to read event logs in order, which is fundamental to event sourcing patterns.
Understanding how to read streams by range helps grasp how event sourcing systems reconstruct state from ordered events.
Time Series Databases
Both Redis streams and time series databases organize data by time and support range queries over time intervals.
Knowing XRANGE's time-based ID ranges clarifies how time series queries work in other systems.
Version Control Systems
Reading commits in forward or reverse order in version control is conceptually similar to XRANGE and XREVRANGE reading entries by ID order.
This connection shows how ordered data retrieval is a common pattern across different fields managing sequences.
Common Pitfalls
#1Using XRANGE with start ID greater than end ID.
Wrong approach:XRANGE mystream + 0-0
Correct approach:XRANGE mystream 0-0 +
Root cause:XRANGE expects start ID to be less than or equal to end ID; reversing them returns no results.
#2Confusing the order of IDs in XREVRANGE.
Wrong approach:XREVRANGE mystream 0-0 +
Correct approach:XREVRANGE mystream + 0-0
Root cause:XREVRANGE requires the end ID first and start ID second to read backward; reversing causes empty results.
#3Omitting COUNT on large streams causing performance issues.
Wrong approach:XRANGE mystream 0-0 +
Correct approach:XRANGE mystream 0-0 + COUNT 100
Root cause:Fetching all entries without limits can overload memory and CPU, slowing down Redis.
Key Takeaways
XRANGE and XREVRANGE let you read Redis stream entries in forward and reverse order by specifying ID ranges.
Stream entry IDs are unique and ordered, combining timestamps and sequence numbers to support precise range queries.
Using COUNT limits the number of entries returned, helping control resource use and enabling pagination.
Special IDs 0-0 and + represent the start and end of the stream, simplifying full-range queries.
Understanding the radix tree indexing and correct ID ordering is essential to avoid common mistakes and write efficient queries.

Practice

(1/5)
1. What does the Redis command XRANGE do when used on a stream?
easy
A. Adds new entries to the stream.
B. Reads stream entries from newest to oldest within a specified range.
C. Deletes entries from the stream within a specified range.
D. Reads stream entries from oldest to newest within a specified range.

Solution

  1. Step 1: Understand XRANGE purpose

    XRANGE is used to read entries from a Redis stream in order from the oldest to the newest.
  2. Step 2: Compare with other commands

    XREVRANGE reads in reverse order, and other commands like XADD add entries, so XRANGE is for reading oldest to newest.
  3. Final Answer:

    Reads stream entries from oldest to newest within a specified range. -> Option D
  4. Quick Check:

    XRANGE = oldest to newest read [OK]
Hint: XRANGE always reads forward (oldest to newest) [OK]
Common Mistakes:
  • Confusing XRANGE with XREVRANGE order
  • Thinking XRANGE adds or deletes entries
  • Assuming XRANGE reads newest first
2. Which of the following is the correct syntax to read the last 5 entries from a Redis stream using XREVRANGE?
easy
A. XRANGE mystream + - COUNT 5
B. XREVRANGE mystream + - COUNT 5
C. XREVRANGE mystream - + COUNT 5
D. XREVRANGE mystream 0 - COUNT 5

Solution

  1. Step 1: Understand XREVRANGE syntax

    XREVRANGE reads from the newest entry (+) to the oldest (-). To get last 5 entries, use range '+' to '-' with COUNT 5.
  2. Step 2: Check options

    XREVRANGE mystream + - COUNT 5 uses correct order '+' to '-' and COUNT 5. XREVRANGE mystream - + COUNT 5 reverses range, XRANGE mystream + - COUNT 5 uses XRANGE, and XREVRANGE mystream 0 - COUNT 5 uses invalid start ID.
  3. Final Answer:

    XREVRANGE mystream + - COUNT 5 -> Option B
  4. Quick Check:

    XREVRANGE newest to oldest with COUNT [OK]
Hint: XREVRANGE uses '+' then '-' for newest to oldest [OK]
Common Mistakes:
  • Swapping '+' and '-' in range
  • Using XRANGE instead of XREVRANGE
  • Incorrect start or end IDs
3. Given a Redis stream mystream with entries:
1609459200000-0: {"name":"Alice"}
1609459260000-0: {"name":"Bob"}
1609459320000-0: {"name":"Carol"}
What will be the result of XRANGE mystream 1609459200000-0 1609459260000-0?
medium
A. [["1609459320000-0", {"name":"Carol"}]]
B. [["1609459260000-0", {"name":"Bob"}], ["1609459200000-0", {"name":"Alice"}]]
C. [["1609459200000-0", {"name":"Alice"}], ["1609459260000-0", {"name":"Bob"}]]
D. Empty list

Solution

  1. Step 1: Understand XRANGE with ID range

    XRANGE returns entries from start ID to end ID in order from oldest to newest.
  2. Step 2: Identify matching entries

    Entries with IDs 1609459200000-0 and 1609459260000-0 fall within the range, so both are returned in order.
  3. Final Answer:

    [["1609459200000-0", {"name":"Alice"}], ["1609459260000-0", {"name":"Bob"}]] -> Option C
  4. Quick Check:

    XRANGE returns oldest to newest in range [OK]
Hint: XRANGE returns entries in ascending ID order [OK]
Common Mistakes:
  • Expecting reverse order output
  • Including entries outside the range
  • Confusing XRANGE with XREVRANGE
4. You run the command XREVRANGE mystream - + COUNT 3 on a stream with more than 3 entries but get an empty list. What is the likely cause?
medium
A. The range start and end IDs are reversed; XREVRANGE expects '+' then '-'.
B. COUNT cannot be used with XREVRANGE.
C. The stream key 'mystream' does not exist.
D. XREVRANGE requires numeric IDs only.

Solution

  1. Step 1: Check XREVRANGE range order

    XREVRANGE reads from newest to oldest, so the start ID must be '+' and end ID '-'. Using '-' then '+' reverses the order and results in an empty list.
  2. Step 2: Verify other options

    COUNT is valid with XREVRANGE, a missing key returns empty list (but stream exists with >3 entries), and special IDs like -/+ are valid.
  3. Final Answer:

    The range start and end IDs are reversed; XREVRANGE expects '+' then '-'. -> Option A
  4. Quick Check:

    XREVRANGE range must be '+' to '-' [OK]
Hint: XREVRANGE range must start with '+' and end with '-' [OK]
Common Mistakes:
  • Swapping start and end IDs in XREVRANGE
  • Assuming COUNT is invalid
  • Confusing the empty result with missing key
5. You want to retrieve the 3 newest entries from a stream mystream but only those with IDs greater than 1609459200000-0. Which command achieves this correctly?
hard
A. XREVRANGE mystream + 1609459200000-0 COUNT 3
B. XRANGE mystream 1609459200000-0 + COUNT 3
C. XREVRANGE mystream 1609459200000-0 + COUNT 3
D. XRANGE mystream + 1609459200000-0 COUNT 3

Solution

  1. Step 1: Understand the requirement

    We want the 3 newest entries with IDs greater than 1609459200000-0, so we read backwards from newest (+) down to that ID.
  2. Step 2: Choose correct command and range

    XREVRANGE reads newest to oldest, so range '+' to '1609459200000-0' with COUNT 3 gets newest entries above that ID.
  3. Step 3: Eliminate wrong options

    XRANGE reads oldest to newest, so XRANGE mystream 1609459200000-0 + COUNT 3 and D read forward. XREVRANGE mystream 1609459200000-0 + COUNT 3 reverses range order for XREVRANGE, causing error.
  4. Final Answer:

    XREVRANGE mystream + 1609459200000-0 COUNT 3 -> Option A
  5. Quick Check:

    Newest entries above ID = XREVRANGE + to ID [OK]
Hint: Use XREVRANGE + to ID for newest entries above ID [OK]
Common Mistakes:
  • Using XRANGE instead of XREVRANGE for newest entries
  • Reversing range IDs in XREVRANGE
  • Omitting COUNT to limit results
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard