0
0
Redisquery~30 mins

XRANGE and XREVRANGE in Redis - Mini Project: Build & Apply

Choose your learning style9 modes available
Using XRANGE and XREVRANGE to Read Redis Streams
📖 Scenario: You are managing a Redis stream that logs user actions on a website. You want to learn how to read entries from this stream in both forward and reverse order to analyze user behavior.
🎯 Goal: Build Redis commands to create a stream with sample data, then use XRANGE to read entries from oldest to newest, and XREVRANGE to read entries from newest to oldest.
📋 What You'll Learn
Create a Redis stream called user_actions with three entries having exact IDs and fields
Define a variable start_id with the value 0-0 and end_id with the value +
Use the XRANGE command with user_actions, start_id, and end_id to get entries in forward order
Use the XREVRANGE command with user_actions, end_id, and start_id to get entries in reverse order
💡 Why This Matters
🌍 Real World
Redis streams are used to record events like user actions, logs, or sensor data in order. Reading them forward or backward helps analyze recent or historical events.
💼 Career
Understanding XRANGE and XREVRANGE is important for developers and database administrators working with Redis streams for real-time data processing and analytics.
Progress0 / 4 steps
1
Create the Redis stream with sample entries
Create a Redis stream called user_actions and add these exact entries using XADD commands: 1609459200000-0 with fields user = alice and action = login, 1609459260000-0 with fields user = bob and action = view, and 1609459320000-0 with fields user = alice and action = logout.
Redis
Need a hint?

Use the XADD command with the stream name user_actions, the exact ID, and the field-value pairs.

2
Define start and end ID variables
Define two variables: start_id with the value 0-0 and end_id with the value + to represent the range boundaries for reading the stream.
Redis
Need a hint?

Assign the string "0-0" to start_id and "+" to end_id.

3
Use XRANGE to read entries from oldest to newest
Use the XRANGE command with the stream user_actions, start_id, and end_id to read all entries in forward order (oldest to newest).
Redis
Need a hint?

Use XRANGE user_actions 0-0 + to read all entries from the beginning to the end.

4
Use XREVRANGE to read entries from newest to oldest
Use the XREVRANGE command with the stream user_actions, end_id, and start_id to read all entries in reverse order (newest to oldest).
Redis
Need a hint?

Use XREVRANGE user_actions + 0-0 to read all entries from newest to oldest.