XADD for adding entries in Redis - Time & Space Complexity
When we add entries to a Redis stream using XADD, it's important to know how the time it takes grows as the stream gets bigger.
We want to understand how the cost of adding one entry changes when the stream has more data.
Analyze the time complexity of the following Redis command.
XADD mystream * sensor-id 1234 temperature 19.8
This command adds a new entry with fields like sensor-id and temperature to the stream named 'mystream'.
Look for any repeated steps inside the XADD operation.
- Primary operation: Inserting the new entry at the end of the stream.
- How many times: This happens once per XADD call; no loops inside the command itself.
Adding one entry takes about the same time no matter how many entries are already in the stream.
| Input Size (entries in stream) | Approx. Operations |
|---|---|
| 10 | Constant time steps |
| 100 | Constant time steps |
| 1000 | Constant time steps |
Pattern observation: The time to add one entry stays roughly the same even as the stream grows larger.
Time Complexity: O(1)
This means adding an entry takes about the same amount of time no matter how many entries are already in the stream.
[X] Wrong: "Adding entries gets slower as the stream grows because Redis has to look through all previous entries."
[OK] Correct: Redis streams are designed to append entries efficiently, so adding one entry does not require scanning the whole stream.
Understanding that XADD runs in constant time helps you explain how Redis handles data efficiently, a useful skill when discussing real-time data systems.
"What if we added multiple entries at once using XADD in a pipeline? How would that affect the overall time complexity?"
Practice
XADD do?Solution
Step 1: Understand the purpose of XADD
XADD is used to add new entries to a Redis stream, each with a unique ID generated by Redis or specified by the user.Step 2: Compare with other commands
Deleting entries is done by XDEL, reading by XRANGE or XREAD, and creating databases is unrelated to XADD.Final Answer:
Adds a new entry to a Redis stream with a unique ID -> Option AQuick Check:
XADD adds entries = A [OK]
- Confusing XADD with XDEL or XRANGE
- Thinking XADD creates databases
- Assuming XADD reads data instead of adding
name and age to a Redis stream mystream using XADD?Solution
Step 1: Recall XADD syntax
The correct syntax is: XADD stream_name * field1 value1 field2 value2 ... The asterisk (*) tells Redis to generate the ID automatically.Step 2: Check each option
XADD mystream * name John age 30 matches the syntax exactly. Options A, C, and D have incorrect order or formatting.Final Answer:
XADD mystream * name John age 30 -> Option CQuick Check:
XADD syntax = XADD stream * fields [OK]
- Placing * after fields instead of immediately after stream name
- Using equal signs (=) in field-value pairs
- Adding parentheses around fields
XADD mystream MAXLEN 2 * temperature 22 humidity 60 executed three times in a row, how many entries will mystream contain afterwards?Solution
Step 1: Understand MAXLEN usage
The MAXLEN 2 option limits the stream length to 2 entries by trimming older entries when new ones are added.Step 2: Apply the command three times
After the first two adds, the stream has 2 entries. The third add triggers trimming, keeping only the latest 2 entries.Final Answer:
2 entries -> Option AQuick Check:
MAXLEN 2 limits stream to 2 entries [OK]
- Assuming all 3 entries remain without trimming
- Confusing MAXLEN with deleting all entries
- Thinking MAXLEN sets minimum length
XADD mystream MAXLEN 5 name John ageSolution
Step 1: Check field-value pairs
Fields and values must come in pairs. Here, 'age' has no value following it.Step 2: Verify MAXLEN position and support
MAXLEN is correctly placed after stream name and before fields. XADD supports MAXLEN.Final Answer:
Missing value for field 'age' -> Option DQuick Check:
Fields need values; 'age' missing one [OK]
- Forgetting to provide value for last field
- Misplacing MAXLEN option
- Thinking stream name is invalid
sensor_data but keep only the latest 100 entries to save memory. Which command correctly adds a new entry with fields temp and humidity and enforces this limit?Solution
Step 1: Understand MAXLEN usage and placement
MAXLEN must come immediately after the stream name, optionally with ~ for approximate trimming. The * must come after MAXLEN to auto-generate ID.Step 2: Analyze options
XADD sensor_data MAXLEN 100 * temp 25 humidity 40 correctly places MAXLEN 100 after stream name and before *, then fields. XADD sensor_data MAXLEN ~ 100 * temp 25 humidity 40 uses ~ (approximate) which is optional but not required. Options B and D have wrong order.Final Answer:
XADD sensor_data MAXLEN 100 * temp 25 humidity 40 -> Option BQuick Check:
MAXLEN 100 before * enforces limit [OK]
- Placing MAXLEN after * or fields
- Using ~ when exact trimming is needed
- Adding MAXLEN at the end of command
Ready to prove it?
Pass the test and this pattern moves unknown
