Stream entry IDs in Redis - Time & Space Complexity
We want to understand how the time to process stream entry IDs in Redis grows as the number of entries increases.
Specifically, how does Redis handle entry IDs when adding or reading from streams?
Analyze the time complexity of adding entries to a Redis stream and reading entries by ID.
XADD mystream * field1 value1
XADD mystream * field2 value2
XRANGE mystream 0-0 +
XREAD COUNT 10 STREAMS mystream 0-0
This code adds entries with auto-generated IDs to a stream and reads entries by ID range or count.
Look for repeated actions that affect time.
- Primary operation: Adding entries and scanning entries by ID range.
- How many times: Each XADD adds one entry; XRANGE or XREAD may scan multiple entries depending on count.
As the number of entries grows, adding one entry stays quick, but reading more entries takes longer.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Adding: 1 operation, Reading: up to 10 operations |
| 100 | Adding: 1 operation, Reading: up to 100 operations |
| 1000 | Adding: 1 operation, Reading: up to 1000 operations |
Pattern observation: Adding an entry is constant time; reading scales with how many entries you request.
Time Complexity: O(1) for adding entries, O(k) for reading k entries.
This means adding one entry takes the same time no matter how big the stream is, but reading depends on how many entries you want.
[X] Wrong: "Adding entries to a Redis stream gets slower as the stream grows because it has to check all previous entries."
[OK] Correct: Redis uses efficient internal structures so adding an entry is quick and does not scan the whole stream.
Understanding how Redis handles stream entry IDs helps you explain performance in real-time data systems, showing you know how data size affects speed.
What if we changed reading entries by ID range to reading entries by a filtered field value? How would the time complexity change?
Practice
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 BQuick Check:
Stream entry ID = timestamp-sequence [OK]
- Thinking ID is only timestamp
- Confusing sequence number as random string
- Ignoring the dash separator
mystream?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 CQuick Check:
Use '*' to auto-generate stream entry ID [OK]
- Using a fixed ID instead of '*'
- Using '#' or '$' which are invalid here
- Forgetting to include field-value pairs
1526985058136-0: field1=value11526985058136-1: field1=value2What will be the ID of the next entry if you add it with
XADD mystream * field1 value3 immediately after?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 AQuick Check:
Next sequence number increments by 1 for same timestamp [OK]
- Assuming timestamp always increments
- Repeating an existing ID
- Using a lower timestamp than existing
XADD mystream 1526985058136-1 field1 value1 but get an error. What is the most likely cause?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 AQuick Check:
Duplicate stream entry IDs cause errors [OK]
- Assuming timestamp format error
- Forgetting field-value pairs
- Thinking negative sequence numbers are allowed
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 DQuick Check:
Auto-generated IDs ensure increasing order despite clock issues [OK]
- Trusting unreliable system clock for IDs
- Using fixed or random IDs causing duplicates
- Ignoring Redis auto-generation feature
Ready to prove it?
Pass the test and this pattern moves unknown
