📖 Scenario: You are managing a chat application where messages are stored in a Redis stream. You want to read new messages as they arrive.
🎯 Goal: Build a Redis command sequence to read entries from a stream using XREAD.
📋 What You'll Learn
Create a Redis stream key called chat_stream with initial messages
Set a variable last_id to track the last read message ID
Use XREAD to read new messages from chat_stream starting from last_id
Update last_id to the ID of the last message read
💡 Why This Matters
🌍 Real World
Reading messages from a Redis stream is common in chat apps, event processing, and real-time data pipelines.
💼 Career
Understanding XREAD helps in building scalable, real-time applications that process streaming data efficiently.
Progress0 / 4 steps
1
Create the Redis stream with initial messages
Use the Redis command XADD to create a stream called chat_stream and add two messages with fields user and message. Add the first message with user as Alice and message as Hello. Add the second message with user as Bob and message as Hi.
Redis
Hint
Use XADD with * to auto-generate IDs.
2
Set the last read message ID variable
Create a variable called last_id and set it to 0-0 to indicate reading from the start of the stream.
Redis
Hint
Use a string variable last_id with value '0-0'.
3
Read new messages from the stream using XREAD
Use the Redis command XREAD with COUNT 10 to read up to 10 new entries from the stream chat_stream starting from the ID stored in last_id. Use the syntax: XREAD COUNT 10 STREAMS chat_stream last_id.
Redis
Hint
Use XREAD COUNT 10 STREAMS chat_stream last_id to read messages.
4
Update last_id to the ID of the last message read
After reading messages, update the variable last_id to the ID of the last message read from the stream. For this exercise, set last_id to "1609459200000-0" as an example of a message ID.
Redis
Hint
Assign last_id the string value "1609459200000-0".
Practice
(1/5)
1. What does the Redis command XREAD primarily do?
easy
A. Deletes entries from a stream
B. Lists all keys in the Redis database
C. Creates a new stream with initial entries
D. Reads new entries from one or more streams starting at a given ID
Solution
Step 1: Understand the purpose of XREAD
XREAD is designed to read entries from Redis streams, starting at a specified ID.
Step 2: Compare with other commands
Deleting entries is done by XDEL, creating streams is implicit when adding entries, and listing keys is done by KEYS or SCAN.
Final Answer:
Reads new entries from one or more streams starting at a given ID -> Option D
Quick Check:
XREAD reads stream entries = B [OK]
Hint: XREAD always reads entries from streams starting at an ID [OK]
Common Mistakes:
Confusing XREAD with commands that delete or create streams
Thinking XREAD lists keys instead of reading stream data
2. Which of the following is the correct syntax to read from a stream named mystream starting from the ID 0-0 using XREAD?
easy
A. XREAD STREAMS mystream 0-0
B. XREAD mystream STREAMS 0-0
C. XREAD STREAMS 0-0 mystream
D. XREAD 0-0 STREAMS mystream
Solution
Step 1: Recall XREAD syntax
The correct syntax is: XREAD STREAMS key [key ...] ID [ID ...]. The keyword STREAMS comes before the stream names and IDs.
Step 2: Match the order for one stream
For one stream, it is: XREAD STREAMS mystream 0-0.
What will be the output of the command XREAD COUNT 2 STREAMS mystream 1609459200000-0?
medium
A. [["mystream", [["1609459201000-0", {"name": "Bob"}], ["1609459202000-0", {"name": "Carol"}]]]]
B. [["mystream", [["1609459200000-0", {"name": "Alice"}], ["1609459201000-0", {"name": "Bob"}]]]]
C. [["mystream", [["1609459202000-0", {"name": "Carol"}]]]]
D. Error: Invalid COUNT usage
Solution
Step 1: Understand COUNT and starting ID
Starting at 1609459200000-0 means reading from the first entry. COUNT 2 limits to 2 entries.
Step 2: Select the first two entries
The first two entries are Alice and Bob with IDs 1609459200000-0 and 1609459201000-0.
Final Answer:
[["mystream", [["1609459200000-0", {"name": "Alice"}], ["1609459201000-0", {"name": "Bob"}]]]] -> Option B
Quick Check:
COUNT limits entries read = A [OK]
Hint: COUNT limits number of entries returned [OK]
Common Mistakes:
Starting from next ID instead of given ID
Ignoring COUNT and returning all entries
Misunderstanding the output format
4. You run the command XREAD STREAMS mystream 0-0 COUNT 2 but get an error. Which of the following is the most likely cause?
medium
A. The COUNT argument is placed after the STREAMS keyword
B. The stream name is missing
C. The ID format is incorrect
D. XREAD does not support COUNT option
Solution
Step 1: Recall XREAD syntax
Modifiers like COUNT must precede the STREAMS keyword: XREAD [COUNT count] STREAMS key id ...
Step 2: Analyze the given command
COUNT 2 is placed after STREAMS mystream 0-0, which is invalid and causes a syntax error.
Step 3: Rule out other options
Stream name present, ID 0-0 valid, COUNT supported.
Final Answer:
The COUNT argument is placed after the STREAMS keyword -> Option A
Quick Check:
COUNT after STREAMS = error A [OK]
Hint: COUNT and BLOCK must precede STREAMS [OK]
Common Mistakes:
Placing COUNT after STREAMS keyword
Omitting stream name
Assuming COUNT is unsupported
5. You want to read new entries from two streams, streamA and streamB, starting from IDs 0-0 and 1609459200000-0 respectively, and wait up to 5000 milliseconds if no new entries arrive. Which command correctly achieves this?