0
0
Redisquery~10 mins

Stream entry IDs in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Stream entry IDs
Add entry to stream
Generate entry ID
Entry ID format: <milliseconds>-<sequence>
Store entry with ID
Return entry ID to user
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.
Execution Sample
Redis
XADD mystream * field1 value1
XADD mystream * field2 value2
Adds two entries to the stream 'mystream' with automatically generated IDs.
Execution Table
StepCommandGenerated Entry IDActionResult
1XADD mystream * field1 value11686900000000-0Generate ID with current ms and seq 0Entry added with ID 1686900000000-0
2XADD mystream * field2 value21686900000000-1Same ms, increment seq to 1Entry added with ID 1686900000000-1
3XADD mystream * field3 value31686900000001-0Next ms, reset seq to 0Entry added with ID 1686900000001-0
4XADD mystream * field4 value41686900000001-1Same ms, seq incremented to 1Entry added with ID 1686900000001-1
5XADD mystream * field5 value51686900000001-2Same ms, seq incremented to 2Entry added with ID 1686900000001-2
6Stop-No more commandsEnd of execution
💡 No more entries added, execution ends.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
Current Milliseconds1686900000000168690000000016869000000001686900000001168690000000116869000000011686900000001
Sequence Number0010122
Generated Entry ID-1686900000000-01686900000000-11686900000001-01686900000001-11686900000001-21686900000001-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.