📖 Scenario: You are managing a Redis database that stores user activity logs in a stream. You want to find out how many entries are currently in the stream to monitor activity volume.
🎯 Goal: Build a Redis command sequence that creates a stream with specific entries and then uses the XLEN command to find out how many entries are in the stream.
📋 What You'll Learn
Create a Redis stream called user_activity with exactly three entries.
Add a configuration variable stream_name set to user_activity.
Use the XLEN command with the stream_name variable to get the length of the stream.
Complete the Redis commands to show the final stream length retrieval.
💡 Why This Matters
🌍 Real World
Redis streams are used in real-time data processing, such as logging user actions or sensor data. Knowing the stream length helps monitor data volume and system health.
💼 Career
Understanding Redis streams and commands like XLEN is valuable for backend developers and system administrators working with real-time data and caching systems.
Progress0 / 4 steps
1
Create a Redis stream with three entries
Create a Redis stream called user_activity and add exactly three entries with the following fields and values: user as alice, action as login; user as bob, action as logout; user as carol, action as purchase. Use the XADD command for each entry.
Redis
Hint
Use XADD command three times with user_activity as the stream name and the specified fields and values.
2
Set a variable for the stream name
Create a variable called stream_name and set it to the string user_activity.
Redis
Hint
Use the SET command to create a key stream_name with value user_activity.
3
Use XLEN to get the stream length
Use the XLEN command with the variable stream_name to get the length of the stream.
Redis
Hint
Use XLEN followed by the stream name user_activity to get the number of entries.
4
Complete the command sequence to retrieve stream length
Complete the Redis commands by using the variable stream_name in the XLEN command to retrieve the stream length.
Redis
Hint
Redis does not support variables directly in commands, so use the stream name user_activity directly in XLEN.
Practice
(1/5)
1. What does the Redis command XLEN do when used on a stream key?
easy
A. Deletes the stream key
B. Returns the number of entries in the stream
C. Returns the last entry in the stream
D. Adds a new entry to the stream
Solution
Step 1: Understand the purpose of XLEN
The XLEN command is designed to return the count of entries in a Redis stream.
Step 2: Compare with other commands
Unlike commands that add or delete entries, XLEN only returns the length (number of entries).
Final Answer:
Returns the number of entries in the stream -> Option B
Quick Check:
XLEN = stream length count [OK]
Hint: XLEN always gives stream size count, not data content [OK]
Common Mistakes:
Confusing XLEN with XADD which adds entries
Thinking XLEN deletes or modifies the stream
Assuming XLEN returns stream entries instead of count
2. Which of the following is the correct syntax to get the length of a Redis stream named mystream?
easy
A. XLEN = mystream
B. XLEN(mystream)
C. XLEN mystream
D. XLEN->mystream
Solution
Step 1: Recall Redis command syntax
Redis commands use the format COMMAND key without parentheses or operators.
Step 2: Identify correct syntax
XLEN mystream is the correct way to call the command to get stream length.
Final Answer:
XLEN mystream -> Option C
Quick Check:
Redis commands use space-separated syntax [OK]
Hint: Redis commands never use parentheses or operators [OK]
Common Mistakes:
Using parentheses like a function call
Trying to assign XLEN to a variable with =
Using arrow or other operators in command
3. Given a Redis stream orders with 5 entries, what will the command XLEN orders return?
medium
A. 5
B. 0
C. An error
D. The last entry ID
Solution
Step 1: Understand what XLEN returns
XLEN returns the number of entries in the stream, which is 5 here.
Step 2: Confirm no errors expected
Since the stream exists and has entries, no error or zero is returned.
Final Answer:
5 -> Option A
Quick Check:
XLEN counts entries = 5 [OK]
Hint: XLEN returns count, not content or errors if stream exists [OK]
Common Mistakes:
Assuming XLEN returns zero if stream has entries
Expecting XLEN to return entry data or IDs
Thinking XLEN throws error for existing streams
4. You run XLEN mystream but get (integer) 0. What is the likely cause?
medium
A. Redis server is down
B. XLEN command is misspelled
C. XLEN cannot be used on streams
D. The stream mystream does not exist in Redis
Solution
Step 1: Understand XLEN behavior on missing keys
XLEN returns (integer) 0 if the stream key does not exist or is empty.
Step 2: Check command correctness
If command is correct and server is running, 0 indicates the stream mystream does not exist.
Final Answer:
The stream mystream does not exist in Redis -> Option D
Quick Check:
Missing key returns 0 from XLEN [OK]
Hint: XLEN returns 0 for missing or empty stream keys [OK]
Common Mistakes:
Assuming XLEN command is wrong
Ignoring server status or connection issues
Thinking XLEN can't be used on streams
5. You want to monitor the size of a Redis stream events and alert if it exceeds 1000 entries. Which Redis command helps you check the stream length efficiently?
hard
A. XLEN events
B. XREAD events
C. XTRIM events MAXLEN 1000
D. XDEL events
Solution
Step 1: Identify command to get stream length
XLEN returns the number of entries in a stream efficiently.