When adding an entry to a Redis stream, Redis generates a unique entry ID based on the current time in milliseconds and a sequence number to ensure uniqueness.
Adds two entries to the stream 'mystream' with automatically generated IDs.
Execution Table
Step
Command
Generated Entry ID
Action
Result
1
XADD mystream * field1 value1
1686900000000-0
Generate ID with current ms and seq 0
Entry added with ID 1686900000000-0
2
XADD mystream * field2 value2
1686900000000-1
Same ms, increment seq to 1
Entry added with ID 1686900000000-1
3
XADD mystream * field3 value3
1686900000001-0
Next ms, reset seq to 0
Entry added with ID 1686900000001-0
4
XADD mystream * field4 value4
1686900000001-1
Same ms, seq incremented to 1
Entry added with ID 1686900000001-1
5
XADD mystream * field5 value5
1686900000001-2
Same ms, seq incremented to 2
Entry added with ID 1686900000001-2
6
Stop
-
No more commands
End of execution
💡 No more entries added, execution ends.
Variable Tracker
Variable
Start
After 1
After 2
After 3
After 4
After 5
Final
Current Milliseconds
1686900000000
1686900000000
1686900000000
1686900000001
1686900000001
1686900000001
1686900000001
Sequence Number
0
0
1
0
1
2
2
Generated Entry ID
-
1686900000000-0
1686900000000-1
1686900000001-0
1686900000001-1
1686900000001-2
1686900000001-2
Key Moments - 3 Insights
Why does the sequence number reset to 0 when the milliseconds part changes?
Because the sequence number counts entries added within the same millisecond. When the millisecond changes, the sequence resets to 0 as IDs must be unique and ordered.
Can two entries have the same entry ID?
No, because the entry ID combines the current time in milliseconds and a sequence number that increments for entries added in the same millisecond, ensuring uniqueness.
What happens if many entries are added very quickly within the same millisecond?
The sequence number increments for each entry within that millisecond, allowing multiple unique IDs like 1686900000000-0, 1686900000000-1, 1686900000000-2, and so on.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the entry ID generated at step 3?
A1686900000000-1
B1686900000001-0
C1686900000001-1
D1686900000000-0
💡 Hint
Check the 'Generated Entry ID' column in row 3 of the execution table.
At which step does the sequence number reset to 0 after incrementing?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Sequence Number' variable in variable_tracker after each step.
If the current milliseconds did not change between step 3 and 4, what would be the sequence number at step 4?
A0
B2
C1
D3
💡 Hint
Sequence increments by 1 for entries in the same millisecond, see variable_tracker sequence number progression.
Concept Snapshot
Stream entry IDs in Redis are unique identifiers for stream entries.
They have the format <milliseconds>-<sequence>.
Milliseconds is the current Unix time in ms.
Sequence increments for multiple entries in the same ms.
Sequence resets to 0 when ms changes.
IDs ensure order and uniqueness in streams.
Full Transcript
This visual execution trace shows how Redis generates stream entry IDs when adding entries with XADD and the * ID option. Each entry ID consists of the current time in milliseconds and a sequence number. The sequence number starts at 0 for a new millisecond and increments for multiple entries added within the same millisecond. The execution table traces five entries added, showing how the milliseconds and sequence number change. The variable tracker highlights the state of the milliseconds and sequence number after each step. Key moments clarify why the sequence resets and how uniqueness is guaranteed. The quiz questions test understanding of the ID generation process referencing the execution visuals.
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.