📖 Scenario: You are building a simple message queue using Redis streams to track user notifications in a web app.
🎯 Goal: Create a Redis stream called notifications and add entries with specific IDs. Then, retrieve entries using their IDs.
📋 What You'll Learn
Create a Redis stream named notifications with initial entries and exact IDs
Define a variable last_id to hold the last entry ID
Add a new entry to the notifications stream using the last_id
Retrieve entries from the notifications stream by their IDs
💡 Why This Matters
🌍 Real World
Redis streams are used in real-time messaging, event sourcing, and logging systems to handle ordered data efficiently.
💼 Career
Understanding Redis streams and entry IDs is valuable for backend developers working with real-time data pipelines and scalable messaging systems.
Progress0 / 4 steps
1
Create the Redis stream with initial entries
Use the Redis command XADD to create a stream called notifications with two entries. Add the first entry with ID 1609459200000-0 and field message set to Welcome. Add the second entry with ID 1609459260000-0 and field message set to New message received.
Redis
Hint
Use XADD with the stream name, exact ID, and field-value pairs.
2
Define the last entry ID variable
Create a variable called last_id and set it to the string 1609459260000-0, which is the ID of the last entry added to the notifications stream.
Redis
Hint
Assign the string ID to the variable last_id.
3
Add a new entry using the last ID
Use the Redis command XADD to add a new entry to the notifications stream. Use the special ID (last_id to add the entry with an ID greater than last_id. Set the field message to System update.
Redis
Hint
Use XADD with (last_id to add an entry with a higher ID.
4
Retrieve entries by their IDs
Use the Redis command XRANGE to retrieve entries from the notifications stream between the IDs 1609459200000-0 and 1609459300000-0. This will return all entries including the new one you added.
Redis
Hint
Use XRANGE with the stream name and the start and end IDs.
Practice
(1/5)
1. What does a Redis stream entry ID consist of?
easy
A. Only a timestamp
B. A timestamp and a sequence number separated by a dash
C. Only a sequence number
D. A random string generated by Redis
Solution
Step 1: Understand Redis stream entry ID format
Redis stream entry IDs are made of two parts: a timestamp and a sequence number.
Step 2: Identify the separator
These two parts are separated by a dash (-), for example, 1526985058136-0.
Final Answer:
A timestamp and a sequence number separated by a dash -> Option B
Quick Check:
Stream entry ID = timestamp-sequence [OK]
Hint: Remember: ID = timestamp + dash + sequence number [OK]
Common Mistakes:
Thinking ID is only timestamp
Confusing sequence number as random string
Ignoring the dash separator
2. Which of the following is the correct way to add a new entry with an auto-generated ID to a Redis stream named mystream?
easy
A. XADD mystream # field1 value1
B. XADD mystream 1234567890-0 field1 value1
C. XADD mystream * field1 value1
D. XADD mystream $ field1 value1
Solution
Step 1: Recall how to auto-generate IDs in Redis streams
Using '*' as the ID in the XADD command tells Redis to generate the ID automatically.
Step 2: Check the options for correct syntax
XADD mystream * field1 value1 uses '*', which is the correct way to auto-generate the ID.
Final Answer:
XADD mystream * field1 value1 -> Option C
Quick Check:
Use '*' to auto-generate stream entry ID [OK]
Hint: Use '*' in XADD to auto-generate IDs [OK]
Common Mistakes:
Using a fixed ID instead of '*'
Using '#' or '$' which are invalid here
Forgetting to include field-value pairs
3. Given the following Redis stream entries: 1526985058136-0: field1=value1 1526985058136-1: field1=value2 What will be the ID of the next entry if you add it with XADD mystream * field1 value3 immediately after?
medium
A. 1526985058136-2
B. 1526985058137-0
C. 1526985058136-0
D. 1526985058135-0
Solution
Step 1: Understand sequence number increment for same timestamp
When multiple entries share the same timestamp, Redis increments the sequence number.
Step 2: Identify the next sequence number
Existing entries have sequence numbers 0 and 1 for timestamp 1526985058136, so the next will be 2.
Final Answer:
1526985058136-2 -> Option A
Quick Check:
Next sequence number increments by 1 for same timestamp [OK]
Hint: Next ID increments sequence if timestamp same [OK]
Common Mistakes:
Assuming timestamp always increments
Repeating an existing ID
Using a lower timestamp than existing
4. You run the command XADD mystream 1526985058136-1 field1 value1 but get an error. What is the most likely cause?
medium
A. The ID 1526985058136-1 already exists in the stream
B. The timestamp part is not a valid number
C. The field name is missing
D. The sequence number is negative
Solution
Step 1: Understand Redis stream ID uniqueness
Each stream entry ID must be unique; duplicate IDs cause errors.
Step 2: Check the given ID
If 1526985058136-1 already exists, adding it again causes an error.
Final Answer:
The ID 1526985058136-1 already exists in the stream -> Option A
Quick Check:
Duplicate stream entry IDs cause errors [OK]
Hint: Duplicate IDs cause errors in XADD [OK]
Common Mistakes:
Assuming timestamp format error
Forgetting field-value pairs
Thinking negative sequence numbers are allowed
5. You want to add multiple entries to a Redis stream with IDs that must be strictly increasing, but your system clock is not reliable and may go backward. Which approach ensures correct stream entry IDs?
hard
A. Use fixed sequence numbers with zero timestamps
B. Manually specify IDs with timestamps from your system clock
C. Generate random IDs and add them to the stream
D. Use XADD mystream * to let Redis generate IDs automatically
Solution
Step 1: Understand the problem with unreliable clocks
Manually specifying timestamps can cause IDs to go backward if the clock is wrong.
Step 2: Use Redis auto-generated IDs
Using '*' lets Redis generate strictly increasing IDs based on its own clock and sequence logic.
Final Answer:
Use XADD mystream * to let Redis generate IDs automatically -> Option D
Quick Check:
Auto-generated IDs ensure increasing order despite clock issues [OK]
Hint: Let Redis generate IDs with '*' for reliable ordering [OK]
Common Mistakes:
Trusting unreliable system clock for IDs
Using fixed or random IDs causing duplicates
Ignoring Redis auto-generation feature
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.